Java - 我的图形闪烁

时间:2014-06-22 16:16:12

标签: java swing jframe paint

我遇到了问题。这是我的计划。有使用绘画方法绘制的风景。点击Space按钮后,背景将会改变。但每当你遇到空间时,图形就会闪烁。继承人代码:

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 个答案:

答案 0 :(得分:1)

永远不要直接在顶级窗口中绘制,例如JFrame或JApplet。而是在JPanel的paintComponent方法中绘制,因为教程会告诉你,因为这将默认为你提供双缓冲。

此外,您可能希望将大多数魔法数字卸载到文件中,因为它们实际上属于数据而不是代码。我将绘图的任何静态部分放到BufferedImage上,然后通过g.drawImage(...)在paintComponent方法中显示该图像。

另外,我会使用Key Bindings而不是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));

你告诉我哪个更容易调试和维护。