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

I would do it like this:

When the user clicks that button, a thread is started. That thread create the movement of the car, and will continuously move the car untill the button is clicked again. You would have to set a flag to know whether it should start, or stop. In the threads run() method, you could make it move, sleep for a second, then repeat the move, sleep for a second, and so forth. I think that's best way to do it. I don't like the idea of it just moving everytime you click the button, I think you can do better than that!

Need more help, let me know.

could you give an example of how to make one of the object move? I have tried and looked at examples in book but having troubles understanding it.
Here is the working code, I took out all the actionlistener stuff I was using trying to make it move

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NewCar extends JApplet
{
	Container con = getContentPane();
	JButton moveCar = new JButton("Click To Move Car");
	public void init()
	{
		con.setLayout(new FlowLayout());
		con.add(moveCar);
	}
	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
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.