2014-06-22 130 views
-3

我遇到問題。這是我的程序。有使用繪畫方法繪製的風景。當你點擊Space按鈕時,背景會改變。但是無論何時你遇到空間,圖形都會閃爍。繼承人的代碼:Java - 我的圖形閃爍

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 


public class Scenery extends JFrame implements KeyListener 
{ 
int c1=1; 
JLabel bg = new JLabel(new ImageIcon("bg.png")); 
JLabel pattern1 = new JLabel(new ImageIcon("p1.jpg")); 
JLabel pattern2 = new JLabel(new ImageIcon("p2.jpg")); 
JLabel pattern3 = new JLabel(new ImageIcon("p3.jpg")); 
JLabel pattern4 = new JLabel(new ImageIcon("p4.jpg")); 
JLabel pattern5 = new JLabel(new ImageIcon("p5.jpg")); 
int f[] = new int[2]; 
int p[] = new int[3]; 
Container c = getContentPane(); 

public Scenery() 
{ 
    super("PRESS SPACE TO CHANGE BACKGROUND"); 
    c.setLayout(null); 
    setSize(800,600); 
    setVisible(true); 
    setLocationRelativeTo(null); 
    setResizable(false); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    addKeyListener(this); 
    getContentPane().setBackground(new Color(22,145,217)); 
    c.add(pattern1); pattern1.setBounds(0,0,800,600); pattern1.setVisible(false); 
    c.add(pattern2); pattern2.setBounds(0,0,800,600); pattern2.setVisible(false); 
    c.add(pattern3); pattern3.setBounds(0,0,800,600); pattern3.setVisible(false); 
    c.add(pattern4); pattern4.setBounds(0,0,800,600); pattern4.setVisible(false); 
    c.add(pattern5); pattern5.setBounds(0,0,800,600); pattern5.setVisible(false); 


} 
    public void paint (Graphics g) 
{ 
    super.paint(g); 
    g.setColor(Color.cyan); 
    g.fillRect(30,100,180,600); 
    g.setColor(Color.black); 
    g.drawRect(30,100,180,600); 
    g.setColor(Color.yellow); 
    g.fillRect(40,120,160,50); 
    g.fillRect(40,200,160,50); 
    g.fillRect(40,280,160,50); 
    g.fillRect(40,360,160,50); 
    g.fillRect(40,440,160,50); 
    g.fillRect(55,520,55,80); 
    g.fillRect(120,520,55,80); 
    g.setColor(Color.black); 
    g.drawRect(40,120,160,50); 
    g.drawRect(40,200,160,50); 
    g.drawRect(40,280,160,50); 
    g.drawRect(40,360,160,50); 
    g.drawRect(40,440,160,50); 
    g.drawRect(55,520,55,80); 
    g.drawRect(120,520,55,80);// 
    g.setColor(Color.red); 
    g.fillRect(410,190,370,20); 
    g.setColor(Color.black); 
    g.drawRect(410,190,370,20);// 
    g.setColor(Color.orange); 
    g.fillRect(420,210,350,200); 
    g.setColor(Color.black); 
    g.drawRect(420,210,350,200);// 
    g.setColor(Color.red); 
    g.fillRect(410,400,370,20); 
    g.setColor(Color.black); 
    g.drawRect(410,400,370,20);// 
    g.setColor(Color.orange); 
    g.fillRect(420,420,350,200); 
    g.setColor(Color.black); 
    g.drawRect(420,420,350,200);// 
    g.setColor(Color.white); 
    g.fillRect(440,230,50,50); 
    g.fillRect(510,230,50,50); 
    g.fillRect(580,230,50,50); 
    g.fillRect(650,230,50,50); 
    g.fillRect(710,230,50,50); 
    g.fillRect(440,320,50,50); 
    g.fillRect(510,320,50,50); 
    g.fillRect(580,320,50,50); 
    g.fillRect(650,320,50,50); 
    g.fillRect(710,320,50,50); 
    g.fillRect(440,450,50,50); 
    g.fillRect(510,450,50,50); 
    g.fillRect(580,450,50,50); 
    g.fillRect(650,450,50,50); 
    g.fillRect(710,450,50,50); 
    g.fillRect(440,520,50,50); 
    g.fillRect(510,520,50,50); 
    g.fillRect(580,520,50,50); 
    g.fillRect(650,520,50,50); 
    g.fillRect(710,520,50,50); 

    g.setColor(Color.black); 
    g.drawRect(440,230,50,50); 
    g.drawRect(510,230,50,50); 
    g.drawRect(580,230,50,50); 
    g.drawRect(650,230,50,50); 
    g.drawRect(710,230,50,50); 
    g.drawRect(440,320,50,50); 
    g.drawRect(510,320,50,50); 
    g.drawRect(580,320,50,50); 
    g.drawRect(650,320,50,50); 
    g.drawRect(710,320,50,50); 
    g.drawRect(440,450,50,50); 
    g.drawRect(510,450,50,50); 
    g.drawRect(580,450,50,50); 
    g.drawRect(650,450,50,50); 
    g.drawRect(710,450,50,50); 
    g.drawRect(440,520,50,50); 
    g.drawRect(510,520,50,50); 
    g.drawRect(580,520,50,50); 
    g.drawRect(650,520,50,50); 
    g.drawRect(710,520,50,50);// 

    g.setColor(Color.black); 
    g.fillRect(320,10,10,100); 

    g.setColor(Color.white); 
    g.drawRect(320,10,10,100); 

    g.setColor(Color.black); 
    g.fillRect(275,100,100,150); 

    g.setColor(Color.white); 
    g.drawRect(275,100,100,150); 

    g.setColor(Color.black); 
    g.fillRect(250,250,150,150); 

    g.setColor(Color.white); 
    g.drawRect(250,250,150,150); 

    g.setColor(Color.black); 
    g.fillRect(225,370,200,250); 

    g.setColor(Color.white); 
    g.drawRect(225,370,200,250); 

    g.fillRect(290,120,70,25); 
    g.fillRect(290,150,70,25); 
    g.fillRect(290,180,70,25); 
    g.fillRect(290,210,70,25); 

    g.fillRect(265,270,123,25); 
    g.fillRect(265,300,123,25); 
    g.fillRect(265,330,123,25); 

    g.fillRect(245,400,160,25); 
    g.fillRect(245,430,160,25); 
    g.fillRect(245,460,160,25); 
    g.fillRect(245,490,160,25); 
    g.fillRect(245,520,160,25); 
    g.fillRect(245,550,160,25); 

    g.setColor(new Color(153,76,0)); 
    g.fillRect(150,350,15,260); 
    g.fillRect(133,370,50,20); 

    g.setColor(Color.black); 
    g.drawRect(150,350,15,260); 
    g.drawRect(133,370,50,20); 

    g.setColor(new Color(153,76,0)); 
    g.fillRect(400,350,15,260); 
    g.fillRect(383,370,50,20); 

    g.setColor(Color.black); 
    g.drawRect(400,350,15,260); 
    g.drawRect(383,370,50,20); 

    g.setColor(new Color(153,76,0)); 
    g.fillRect(650,350,15,260); 
    g.fillRect(633,370,50,20); 

    g.setColor(Color.black); 
    g.drawRect(650,350,15,260); 
    g.drawRect(633,370,50,20); 

    g.setColor(Color.black); 
    g.drawArc(-50, 320, 200, 100, 180, 180); 
    g.drawArc(-50, 330, 200, 100, 180, 180); 
    g.drawArc(-50, 340, 200, 100, 180, 180); 

    g.drawArc(166,320,236,100,180,180); 
    g.drawArc(166,330,236,100,180,180); 
    g.drawArc(166,340,236,100,180,180); 

    g.drawArc(414,320,236,100,180,180); 
    g.drawArc(414,330,236,100,180,180); 
    g.drawArc(414,340,236,100,180,180); 

    g.drawArc(665,320,236,100,180,180); 
    g.drawArc(665,330,236,100,180,180); 
    g.drawArc(665,340,236,100,180,180); 

    g.setColor(Color.orange); 
    g.fillRect(20,470,170,20); 
    g.setColor(Color.black); 
    g.drawRect(20,470,170,20); 

    g.setColor(new Color(249,216,83)); 
    g.fillRect(32,490,150,120); 
    g.setColor(Color.black); 
    g.drawRect(32,490,150,120); 
    g.setColor(Color.cyan); 
    g.fillRect(40,505,30,30); 
    g.fillRect(90,505,30,30); 
    g.fillRect(40,555,30,30); 
    g.fillRect(90,555,30,30); 
    g.fillRect(135,520,40,90); 

    g.setColor(Color.black); 
    g.drawRect(40,505,30,30); 
    g.drawRect(90,505,30,30); 
    g.drawRect(40,555,30,30); 
    g.drawRect(90,555,30,30); 
    g.drawRect(135,520,40,90); 

    g.setColor(new Color(179,91,255)); 
    g.fillRect(225,470,170,20); 

    g.setColor(Color.black); 
    g.drawRect(225,470,170,20); 

    g.setColor(Color.magenta); 
    g.fillRect(236,490,150,120); 

    g.setColor(Color.black); 
    g.drawRect(236,490,150,120); 

    g.setColor(new Color(153,255,51)); 
    g.fillRect(249,505,30,30); 
    g.fillRect(249,555,30,30); 
    g.fillRect(289,505,30,30); 
    g.fillRect(289,555,30,30); 
    g.fillRect(335,520,40,90); 

    g.setColor(Color.black); 
    g.drawRect(249,505,30,30); 
    g.drawRect(249,555,30,30); 
    g.drawRect(289,505,30,30); 
    g.drawRect(289,555,30,30); 
    g.drawRect(335,520,40,90); 

    // 
    g.setColor(new Color(247,199,103)); 
    g.fillRect(445,470,170,20); 

    g.setColor(Color.black); 
    g.drawRect(445,470,170,20); 

    g.setColor(Color.green); 
    g.fillRect(455,490,150,120); 

    g.setColor(Color.black); 
    g.drawRect(455,490,150,120); 

    g.setColor(Color.darkGray); 
    g.fillRect(469,505,30,30); 
    g.fillRect(469,555,30,30); 
    g.fillRect(519,505,30,30); 
    g.fillRect(519,555,30,30); 
    g.fillRect(560,520,40,90); 

    g.setColor(Color.black); 
    g.drawRect(469,505,30,30); 
    g.drawRect(469,555,30,30); 
    g.drawRect(519,505,30,30); 
    g.drawRect(519,555,30,30); 
    g.drawRect(560,520,40,90); 


} 
public static void main(String args[]) 
{ 
    new Scenery(); 
} 
     public void keyPressed(KeyEvent e) 
    { 
     String key = e.getKeyText(e.getKeyCode()); 

     if (key.equals("Space")); 
     { 
      int n1 = (int) (Math.random()*5); 
      if (n1==1) 
      { 
       pattern1.setVisible(true); 
       pattern2.setVisible(false); 
       pattern3.setVisible(false); 
       pattern4.setVisible(false); 
       pattern5.setVisible(false); 
       n1=0; 
      } 
      if (n1==2) 
      { 
       pattern1.setVisible(false); 
       pattern2.setVisible(true); 
       pattern3.setVisible(false); 
       pattern4.setVisible(false); 
       pattern5.setVisible(false); 
       n1=0; 
      } 
      if (n1==3) 
      { 
       pattern1.setVisible(false); 
       pattern2.setVisible(false); 
       pattern3.setVisible(true); 
       pattern4.setVisible(false); 
       pattern5.setVisible(false); 
       n1=0; 
      } 
      if (n1==4) 
      { 
       pattern1.setVisible(false); 
       pattern2.setVisible(false); 
       pattern3.setVisible(false); 
       pattern4.setVisible(true); 
       pattern5.setVisible(false); 
       n1=0; 
      } 
      if (n1==1) 
      { 
       pattern1.setVisible(false); 
       pattern2.setVisible(false); 
       pattern3.setVisible(false); 
       pattern4.setVisible(false); 
       pattern5.setVisible(true); 
       n1=0; 
      } 
     }  

    } 

public void keyReleased(KeyEvent e) 
{ 

    repaint(); 
} 

public void keyTyped(KeyEvent e) 
{ 

} 

}

在此先感謝,如果有人迴應。

+1

嘗試啓用雙緩衝,看看是否有幫助 – sjr

+0

試試[這個答案](http://stackoverflow.com/a/4430551/2970947)。 –

+1

請注意,您已經提出了4個問題,並沒有打擾接受任何問題中的單個答案 - 爲什麼? –

回答

1

切勿直接在頂層窗口(如JFrame或JApplet)中進行繪製。相反,請在JPanel的paintComponent方法中繪製,因爲這些教程會告訴你,默認情況下這會給你雙緩衝。

此外,您可能希望將大部分這些幻數加載到文件中,因爲它們確實屬於數據而不是代碼。我會將繪圖的任何靜態部分放到BufferedImage上,然後通過g.drawImage(...)將該圖像顯示在我的paintComponent方法中。

此外,我會使用鍵綁定,而不是一個KeyListener,因爲這個網站上的其他類似問題已經很好地討論過了,我使用數組或列表,並嘗試重構代碼以使其更加簡化並且沒有多餘重複。我將使用ImageIcons來處理我的模式圖像,以及一個JLabel並簡單地交換圖標。如果你這樣做,那麼這段代碼:

 int n1 = (int) (Math.random()*5); 
     if (n1==1) 
     { 
      pattern1.setVisible(true); 
      pattern2.setVisible(false); 
      pattern3.setVisible(false); 
      pattern4.setVisible(false); 
      pattern5.setVisible(false); 
      n1=0; 
     } 
     if (n1==2) 
     { 
      pattern1.setVisible(false); 
      pattern2.setVisible(true); 
      pattern3.setVisible(false); 
      pattern4.setVisible(false); 
      pattern5.setVisible(false); 
      n1=0; 
     } 
     if (n1==3) 
     { 
      pattern1.setVisible(false); 
      pattern2.setVisible(false); 
      pattern3.setVisible(true); 
      pattern4.setVisible(false); 
      pattern5.setVisible(false); 
      n1=0; 
     } 
     if (n1==4) 
     { 
      pattern1.setVisible(false); 
      pattern2.setVisible(false); 
      pattern3.setVisible(false); 
      pattern4.setVisible(true); 
      pattern5.setVisible(false); 
      n1=0; 
     } 
     if (n1==1) 
     { 
      pattern1.setVisible(false); 
      pattern2.setVisible(false); 
      pattern3.setVisible(false); 
      pattern4.setVisible(false); 
      pattern5.setVisible(true); 
      n1=0; 
     } 
    }  

如果你把你的模式ImageIcons到名爲patternList一個ArrayList,那麼代碼可以是簡單的:

int randomIndex = (int) Math.random() * patternList.size(); 
myLabel.setIcon(patternList.get(randomIndex)); 

你告訴我哪一個更容易調試並保持。