The program moves an image from left to right. Once it gets to the right side, I want it to go down. So is there a way can I call the repaint function again only with -y in the second column of gg.fillOval?
import java.awt.*;
import javax.swing.*;
public class moving extends JFrame implements Runnable{
private int x = 0;
private int y;
private Image img;
private Graphics gg;
public static void main(String[] args){
Thread th = new Thread(new moving());
th.start();
}
public moving(){
this.setSize(500, 500);
this.setTitle("Moving");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
}
public void run(){
for(i = 0; i <= 300; ++i){
repaint();
++x;
try { Thread.sleep(5); } catch(Exception e) { }
}
}
public void paint(Graphics g){
img = createImage(500,500);
gg = img.getGraphics();
gg.setColor(Color.white);
gg.fillRect(0,0,500,500);
gg.setColor(Color.blue);
gg.fillOval(100+x, 100, 40, 40);
g.drawImage(img, 0, 0, this);
}
}