ActionListener works but painting doesn't

76 views Asked by At
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Game extends JComponent implements ActionListener {
    Timer t = new Timer(5, this);
    int wx;
    int rx = 10;
    int rx2 = 10;
    int carx = 10;
    int velX = 2;

    public static void main(String[] args) {
        JFrame window = new JFrame("Frogger");
        window.add(new Game());
        window.pack();
        window.setSize(800, 600);
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        window.setVisible(true);


    }
    public void actionPerformed(ActionEvent e){
        carx+=velX;
        repaint();
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        repaint();
        g.setColor(new Color(173, 216, 230));
        g.fillRect(0, 0, 800, 600); //background
        g.setColor(Color.lightGray);
        g.fillRect(0, 525, 800, 75); //start
        g.fillRect(0, 0, 800, 30); //end
        g.setColor(Color.black);
        g.fillRect(0, 275, 800, 250); //street
        g.setColor(Color.white);
        for (int n = 0; n < 16; n++) {
            g.fillRect(rx, 450, 20, 10);
            rx += 50;
        }
        for (int n = 0; n < 16; n++) {
            g.fillRect(rx2, 375, 20, 10);
            rx2 += 50;
        }
        g.fillRect(carx, 477, 60, 30);
        t.start();

    }

}

I am trying to make a Frogger game and am having trouble creating the traffic. I am able to make a car move across the street but the lines separating the lanes are shown for a millisecond and then disappear after I run the program. The street, river, start, and end all show up as I want them to. How do I make the lane lines not disappear?

1

There are 1 answers

0
rodit On BEST ANSWER

There are a couple of problems with what you are doing.

Firstly, you are calling t.start() every time the component is drawn. This is unnecessary. Instead of doing this, create a boolean value determining whether it is the first frame and start it then. Here is how you can do this:

boolean firstFrame = true;

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    if(firstFrame){
        t.start();
        firstFrame = false;
    }
    //rest of render code...
}

Now for your question of how to stop the lines disappearing. You seem to store the integers rx and rx2 as members of the Game class. This means when you add to them, they stay added until reset. Therefore, every frame, you must reset rx and rx2 to 10.

At the end of paintComponent, add this.

rx = rx2 = 10;

This will set both rx and rx2 back to their starting value of 10.