-3

I'm having a problem. Here's my program. There is a scenery drawn using the paint method. When you hit the Space button, the background will change. But whenever you hit space, the graphics flickers. heres the code:

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)
{

}

}

Thanks in advance if someone would respond.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3226012
  • 1
  • 1
  • 4

1 Answers1

1

Never draw directly in a top level window such as a JFrame or JApplet. Instead draw in a JPanel's paintComponent method as the tutorials will tell you as this will give you double buffering by default.

Also, you will likely want to off load most of those magic numbers into a file as they really belong with data and not with code. I'd do any static parts of my drawing onto a BufferedImage and then display that image in my paintComponent method via g.drawImage(...).

Also, I would use Key Bindings and not a KeyListener as has been well discussed in other similar questions on this site, and I use arrays or lists and would try to refactor the code to make it more streamline and with less needless repetition. And I would use ImageIcons for my pattern images, and one JLabel and simply swap icons. If you did this, then this code:

        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;
        }
    }     

If you put your pattern ImageIcons into an ArrayList called patternList, then the code could be simply:

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

You tell me which is easier to debug and maintain.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373