I have made this car but am wondering how I would get it to move across the screen either on load or by button click any suggestions?
import java.applet.Applet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NewCar extends JApplet implements ActionListener, Runnable
{
Container con = getContentPane();
JButton moveCar = new JButton("Click To Move Car");
public void init()
{
con.setLayout(new FlowLayout());
con.add(moveCar);
moveCar.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public void paint(Graphics g)
{
setBackground(Color.BLUE);
g.setColor(Color.RED);
g.fillRoundRect(200,250,450,100,50,50); // body of car
g.setColor(Color.BLACK);
g.fillOval(235,290,100,100); // front tire
g.fillOval(510,290,100,100); // back tire
g.setColor(Color.YELLOW);
g.fillRoundRect(610,265,40,30,20,10); // back light
g.fillRoundRect(205,270,45,20,30,10); // front light
g.setColor(Color.GRAY);
g.fillOval(250,305,70,70); // front tire rim
g.fillOval(525,305,70,70); // back tire rim
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,388,1500,10); // road
g.setColor(Color.RED);
// windshield (thickness)
g.drawLine(500,250,400,180);
g.drawLine(501,250,401,180);
g.drawLine(502,250,402,180);
g.drawLine(503,250,403,180);
g.drawLine(504,250,404,180);
g.drawLine(505,250,405,180);
g.drawLine(506,250,406,180);
g.drawLine(507,250,407,180);
g.drawLine(508,250,408,180);
g.drawLine(509,250,409,180);
g.drawLine(510,250,410,180);
g.setColor(Color.BLACK);
g.drawLine(500,350,500,250); // door line
g.drawLine(340,350,340,250); // door line
}
}