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();
}
boolean firstFrame = true;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (firstFrame) {
t.start();
firstFrame = false;
}
g.setColor(new Color(173, 216, 230));
g.fillRect(0, 0, 800, 600);
g.setColor(Color.lightGray);
g.fillRect(0, 525, 800, 75);
g.fillRect(0, 0, 800, 30);
g.setColor(Color.black);
g.fillRect(0, 300, 800, 225);
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;
}
rx = rx2 = 10;
while(true){
g.fillRect(carx, 477, 60, 30);
}
}
}
I am trying to make an object move across the screen and when it gets to a certain point the program will copy itself and move across the screen with the other object still going. I am trying to figure out how to do this infinitely. I tried to use a while loop but that obviously didn't work. Any help is appreciated.