14 Replies - 21027 Views - Last Post: 21 April 2010 - 03:36 PM Rate Topic: -----

#1 Jwd41190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 10

Java Racing Car Applet

Posted 16 April 2010 - 07:12 PM

I am working on this java applet that has moving cars on it, but I have run into three problems and can't seem to fix any of them and I was hoping I could get some help with at least 1 of these problems.

Problem 1: The cars run off the screen once they go across the applet once. How do I make them to continuously go around and around?

Problem 2: My applet flickers really bad. I know about the double buffering idea and I have implemented it, but not sure if I have done it right as it doesn't seem to be working correctly.

Problem 3: My stop button isn't working for some reason, I want it when its press, that the cars stop moving at there current locations.

Here is my code if ya'll think ya'll can help me and give me some advice. Also, I have attached the images that I am using. Thanks!

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.applet.*;
import javax.swing.*;

public class RacingApplet extends JApplet{
	private JLabel Car1JLabel, Car2JLabel, Car3JLabel, Car4JLabel;
	private JTextField Car1JTextField, Car2JTextField, Car3JTextField, Car4JTextField;
	private JButton StartJButton, StopJButton;
	private Image MarioImage, LuigiImage, YoshiImage, BowserImage;
	private Timer timer;
	private Image dbImage;
	private Graphics dbg;
	private int Car1;
	private int Car2;
	private int Car3;
	private int Car4;
	private int MarioX = 4, LuigiX = 4, YoshiX = 4, BowserX = 4;

	public void init(){

		Container background = getContentPane();
		background.setBackground(Color.white);
		setLayout(new FlowLayout());

		StartJButton = new JButton("Start");
		add(StartJButton);

		Car1JLabel = new JLabel("Car 1: ");
		add(Car1JLabel);
		Car1JTextField = new JTextField("", 3);
		add(Car1JTextField);
		Car2JLabel = new JLabel("Car 2: ");
		add(Car2JLabel);
		Car2JTextField = new JTextField("", 3);
		add(Car2JTextField);
		Car3JLabel = new JLabel("Car 3: ");
		add(Car3JLabel);
		Car3JTextField = new JTextField("", 3);
		add(Car3JTextField);
		Car4JLabel = new JLabel("Car 4: ");
		add(Car4JLabel);
		Car4JTextField = new JTextField("", 3);
		add(Car4JTextField);

		StopJButton = new JButton("Stop");
		add(StopJButton);

		MarioImage = getImage(getDocumentBase(), "Mario.gif");
		LuigiImage = getImage(getDocumentBase(), "Luigi.gif");
		YoshiImage = getImage(getDocumentBase(), "Yoshi.gif");
		BowserImage = getImage(getDocumentBase(), "Bowser.gif");
		timer = new Timer(50, new TimerHandler());

		JOptionPane.showMessageDialog(null, "To make the cars move please enter a\n" +
                        "number between 1 and 5 in each text box.\n " + "\n"+
                        "1 = Slowest Speed   5 = Fastest Speed\n " + "\n" +
                        "                   IMPORTANT NOTE* \n\n" +
                        "*If you enter a number lower than 1,\n" +
                        "your number will automatically become 1.\n " + "\n" +
                        "*If you enter a number higher than 5,\n" +
                        "your number will automatically become 5. ");
	}

	public void start(){

		StartJButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{

				Car1 = Integer.parseInt(Car1JTextField.getText());
				Car2 = Integer.parseInt(Car2JTextField.getText());
				Car3 = Integer.parseInt(Car3JTextField.getText());
				Car4 = Integer.parseInt(Car4JTextField.getText());

				if (Car1 > 5)
				{
					Car1 = 5;
				}
				else if (Car1 < 1)
					Car1 = 1;

				if (Car2 > 5)
				{
					Car2 = 5;
				}
				else if (Car2 < 1)
					Car2 = 1;

				if (Car3 > 5)
				{
					Car3 = 5;
				}
				else if (Car3 < 1)
					Car3 = 1;

				if (Car4 > 5)
				{
					Car4 = 5;
				}
				else if (Car4 < 1)
					Car4 = 1;

				if(timer == null)
                                {
					timer.start();

				}
				else
				{
					if (!timer.isRunning())
						timer.restart();
				}
                        }
		}
	);
		StopJButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{

				timer.stop();
			}
		}
	);
	}



	public void paint(Graphics g){

		super.paint(g);

		g.drawRect(0, 35, 1000, 50);
		g.drawRect(0, 85, 1000, 50);
		g.drawRect(0, 135, 1000, 50);
		g.drawRect(0, 185, 1000, 50);

		g.drawImage(MarioImage, MarioX, 52, this);
		g.drawImage(LuigiImage, LuigiX, 102, this);
		g.drawImage(YoshiImage, YoshiX, 152, this);
		g.drawImage(BowserImage, BowserX, 202, this);

		MarioX = MarioX + (Car1);
		LuigiX = LuigiX + (Car2);
		YoshiX = YoshiX + (Car3);
		BowserX = BowserX + (Car4);
}



	public void update(Graphics g){

            //Double Buffer
            if (dbImage == null)
                {

                dbImage = createImage(this.getSize().width, this.getSize().height);
                dbg = dbImage.getGraphics();

                }

                // clear screen in background
                dbg.setColor(getBackground());
                dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

                // draw elements in background
                dbg.setColor(getForeground());
                paint(dbg);

                // draw image on the screen
                g.drawImage(dbImage, 0, 0, this);
           }

	public void stop(){

	}

	public void destroy(){

	}

	private class TimerHandler implements ActionListener{

		public void actionPerformed(ActionEvent actionEvent){
			repaint();
                        }
                
             
	}

	}



HTML CODE

<html>
  <head>
    <title>Josh's Car Racing Applet</title>
  </head>
  <body>
      <applet code=RacingApplet.class width=1000 height=240>
      </applet>
  </body>
</html>




*My other topic by guest_Tom can be deleted sorry about that.
I decided to register I like this place. Thanks again.

Attached image(s)

  • Attached Image
  • Attached Image
  • Attached Image
  • Attached Image

This post has been edited by Jwd41190: 16 April 2010 - 08:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java Racing Car Applet

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: Java Racing Car Applet

Posted 16 April 2010 - 08:05 PM

Can you edit your post to properly indent your code? It is hardly possible for us to follow the logic with your current indentation and spacing conventions. Thanks for helping us help you! :)
Was This Post Helpful? 1
  • +
  • -

#3 Jwd41190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 10

Re: Java Racing Car Applet

Posted 16 April 2010 - 08:25 PM

View Postmacosxnerd101, on 16 April 2010 - 07:05 PM, said:

Can you edit your post to properly indent your code? It is hardly possible for us to follow the logic with your current indentation and spacing conventions. Thanks for helping us help you! :)


Ok, thanks I fixed it now I think :)
Was This Post Helpful? 0
  • +
  • -

#4 Jwd41190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 10

Re: Java Racing Car Applet

Posted 17 April 2010 - 08:21 AM

I am still having problems with this and still can't figure it out. My main thing is I need to know how to make the cars go around continuously. I figured there is some sort of loop I need to use, but not sure where to put it and how to use it. Any help would be greatly appreciated. Thanks so much.
Was This Post Helpful? 0
  • +
  • -

#5 Jwd41190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 10

Re: Java Racing Car Applet

Posted 17 April 2010 - 03:15 PM

Anyone have any ideas?
Was This Post Helpful? 0
  • +
  • -

#6 Jwd41190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 10

Re: Java Racing Car Applet

Posted 18 April 2010 - 09:46 AM

Please someone give me some suggestions someone. Thanks
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,033
  • Joined: 15-July 08

Re: Java Racing Car Applet

Posted 18 April 2010 - 10:04 AM

Bumping your posts makes the experts and other DICs here think that you solved your issue, and it's really rude. I think that you need to, in your timer loop, do an update where if the right side of an image goes past 0, to move the left side of the image to the right side of the screen (screen width). If the left side of the image moves past the screen width, then move the right side to 0.

Then do a similar things to the y's. That should make it wrap.
Was This Post Helpful? 0
  • +
  • -

#8 Jwd41190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 10

Re: Java Racing Car Applet

Posted 18 April 2010 - 10:19 AM

View PostDogstopper, on 18 April 2010 - 09:04 AM, said:

Bumping your posts makes the experts and other DICs here think that you solved your issue, and it's really rude. I think that you need to, in your timer loop, do an update where if the right side of an image goes past 0, to move the left side of the image to the right side of the screen (screen width). If the left side of the image moves past the screen width, then move the right side to 0.

Then do a similar things to the y's. That should make it wrap.



I'm sorry I didn't realize that.

You mean in this part of the code:

if(timer == null)
                                {
                                        timer.start();

                                }
                                else
                                {
                                        if (!timer.isRunning())
                                                timer.restart();
                                }
                        }

Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,033
  • Joined: 15-July 08

Re: Java Racing Car Applet

Posted 18 April 2010 - 10:58 AM

No...I meant in the actionListener(). Hey...look, the first condition will never be true here:
if(timer == null)
                                {
                                        timer.start();

                                }



Timer doesn't equal null, and if it was, you wouldn't want to start it! Either make it quit if null or change == to !=
Was This Post Helpful? 0
  • +
  • -

#10 Jwd41190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 10

Re: Java Racing Car Applet

Posted 18 April 2010 - 11:53 AM

View PostDogstopper, on 18 April 2010 - 09:58 AM, said:

No...I meant in the actionListener(). Hey...look, the first condition will never be true here:
if(timer == null)
                                {
                                        timer.start();

                                }



Timer doesn't equal null, and if it was, you wouldn't want to start it! Either make it quit if null or change == to !=


Ok, I fixed that problem and changed it to != Thanks. So for the other thing you are talking about here:

private class TimerHandler implements ActionListener{

		public void actionPerformed(ActionEvent actionEvent){
			repaint();
                        }


Thanks for your help. :)
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,033
  • Joined: 15-July 08

Re: Java Racing Car Applet

Posted 18 April 2010 - 02:57 PM

Yep, at that spot you would want to update the location of the vehicle (or call a method to do so)
Was This Post Helpful? 0
  • +
  • -

#12 Jwd41190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 10

Re: Java Racing Car Applet

Posted 18 April 2010 - 03:39 PM

View PostDogstopper, on 18 April 2010 - 01:57 PM, said:

Yep, at that spot you would want to update the location of the vehicle (or call a method to do so)



Ok thanks. I understand what you are saying sort of just not sure how to implement it. It is something like this:

if(g.drawImage = 1000)
                        {
                g.drawImage(MarioImage, 0, 52, this);
		g.drawImage(LuigiImage, 0, 102, this);
		g.drawImage(YoshiImage, 0, 152, this);
		g.drawImage(BowserImage, 0, 202, this);
                        }
                        else
                            repaint();



1000 is the width of my window. Thanks
Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,033
  • Joined: 15-July 08

Re: Java Racing Car Applet

Posted 18 April 2010 - 09:13 PM

What you should do is have a set of variables, x and y, that on each actionPerformed() call, either increment, decrement, or do nothing to that x and y and then call repaint. But only repaint using those variables.

You really should read my article here on frame rate; once you understand that, you're in good shape:
http://www.dreaminco...e-rate-in-java/
Was This Post Helpful? 0
  • +
  • -

#14 Jwd41190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 10

Re: Java Racing Car Applet

Posted 20 April 2010 - 02:02 PM

Ok I got everything working now but one thing. It blinks like crazy on my computer. I have done the double buffering but not sure I did it right can someone help me out. Here is my new code:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.applet.*;
import javax.swing.*;

public class RacingApplet extends JApplet{
	private JLabel Car1JLabel, Car2JLabel, Car3JLabel, Car4JLabel;
	private JTextField Car1JTextField, Car2JTextField, Car3JTextField, Car4JTextField;
	private JButton StartJButton, StopJButton;
	private Image MarioImage, LuigiImage, YoshiImage, BowserImage;
	private Timer timer;
	private Graphics dbg;
	private int Car1;
	private int Car2;
	private int Car3;
	private int Car4;
	private int MarioX = 0, LuigiX = 0, YoshiX = 0, BowserX = 0;

	public void init(){

		Container background = getContentPane();
		background.setBackground(Color.white);
		setLayout(new FlowLayout());

		StartJButton = new JButton("Start");
		add(StartJButton);

		Car1JLabel = new JLabel("Car 1: ");
		add(Car1JLabel);
		Car1JTextField = new JTextField("", 3);
		add(Car1JTextField);
		Car2JLabel = new JLabel("Car 2: ");
		add(Car2JLabel);
		Car2JTextField = new JTextField("", 3);
		add(Car2JTextField);
		Car3JLabel = new JLabel("Car 3: ");
		add(Car3JLabel);
		Car3JTextField = new JTextField("", 3);
		add(Car3JTextField);
		Car4JLabel = new JLabel("Car 4: ");
		add(Car4JLabel);
		Car4JTextField = new JTextField("", 3);
		add(Car4JTextField);

		StopJButton = new JButton("Stop");
		add(StopJButton);

		MarioImage = getImage(getDocumentBase(), "Mario.gif");
		LuigiImage = getImage(getDocumentBase(), "Luigi.gif");
		YoshiImage = getImage(getDocumentBase(), "Yoshi.gif");
		BowserImage = getImage(getDocumentBase(), "Bowser.gif");

		timer = new Timer(50, new TimerHandler());

		JOptionPane.showMessageDialog(null, "To make the cars move please enter a\n" +
                        "number between 1 and 5 in each text box.\n " + "\n"+
                        "1 = Slowest Speed   5 = Fastest Speed\n " + "\n" +
                        "                   IMPORTANT NOTE* \n\n" +
                        "*If you enter a number lower than 1,\n" +
                        "your number automatically becomes 1.\n " + "\n" +
                        "*If you enter a number higher than 5,\n" +
                        "your number automatically becomes 5. ");

	}

	public void start(){

		StartJButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{

				Car1 = Integer.parseInt(Car1JTextField.getText());
				Car2 = Integer.parseInt(Car2JTextField.getText());
				Car3 = Integer.parseInt(Car3JTextField.getText());
				Car4 = Integer.parseInt(Car4JTextField.getText());

				if (Car1 > 5)
				{
					Car1 = 5;
				}
				else if (Car1 < 1)
					Car1 = 1;

				if (Car2 > 5)
				{
					Car2 = 5;
				}
				else if (Car2 < 1)
					Car2 = 1;

				if (Car3 > 5)
				{
					Car3 = 5;
				}
				else if (Car3 < 1)
					Car3 = 1;

				if (Car4 > 5)
				{
					Car4 = 5;
				}
				else if (Car4 < 1)
					Car4 = 1;

				timer.start();
            }
		}
	);
		StopJButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				Car1 = 0;
				Car2 = 0;
				Car3 = 0;
				Car4 = 0;
			}
		}
	);
	}

	public void paint(Graphics g){
		super.paint(g);

		g.drawRect(0, 35, 1000, 50);
		g.drawRect(0, 85, 1000, 50);
		g.drawRect(0, 135, 1000, 50);
		g.drawRect(0, 185, 1000, 50);

		g.drawImage(MarioImage, MarioX, 52, this);
		g.drawImage(LuigiImage, LuigiX, 102, this);
		g.drawImage(YoshiImage, YoshiX, 152, this);
		g.drawImage(BowserImage, BowserX, 202, this);

		MarioX = MarioX + (Car1);
		LuigiX = LuigiX + (Car2);
		YoshiX = YoshiX + (Car3);
		BowserX = BowserX + (Car4);

		if (MarioX > 1000)
		{
			MarioX = 0;
		}
		if (LuigiX > 1000)
		{
			LuigiX = 0;
		}
		if (YoshiX > 1000)
		{
			YoshiX = 0;
		}
		if (BowserX > 1000)
		{
			BowserX = 0;
		}
	
	}

	public void update(Graphics g){

				//Double Buffer
				if (MarioImage == null)
                {

                MarioImage = createImage(this.getSize().width, this.getSize().height);
                dbg = MarioImage.getGraphics();

                }

                // clear screen in background
                dbg.setColor(getBackground());
                dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

                // draw elements in background
                dbg.setColor(getForeground());
                paint(dbg);

                // draw image on the screen
                g.drawImage(MarioImage, 0, 0, this);


				//Double Buffer
				if (LuigiImage == null)
				{

				LuigiImage = createImage(this.getSize().width, this.getSize().height);
				dbg = LuigiImage.getGraphics();

				}

				// clear screen in background
				dbg.setColor(getBackground());
				dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

				// draw elements in background
				dbg.setColor(getForeground());
				paint(dbg);

				// draw image on the screen
				g.drawImage(LuigiImage, 0, 0, this);


				//Double Buffer
				if (YoshiImage == null)
				{

				YoshiImage = createImage(this.getSize().width, this.getSize().height);
				dbg = YoshiImage.getGraphics();

				}

				// clear screen in background
				dbg.setColor(getBackground());
				dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

				// draw elements in background
				dbg.setColor(getForeground());
				paint(dbg);

				// draw image on the screen
				g.drawImage(YoshiImage, 0, 0, this);


				//Double Buffer
				if (BowserImage == null)
				{

				BowserImage = createImage(this.getSize().width, this.getSize().height);
				dbg = BowserImage.getGraphics();

				}

				// clear screen in background
				dbg.setColor(getBackground());
				dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

				// draw elements in background
				dbg.setColor(getForeground());
				paint(dbg);

				// draw image on the screen
				g.drawImage(BowserImage, 0, 0, this);
           }

	public void stop(){

	}

	public void destroy(){

	}

	private class TimerHandler implements ActionListener{

		public void actionPerformed(ActionEvent actionEvent){
			repaint();

	}
    }
	}

Was This Post Helpful? 0
  • +
  • -

#15 Jwd41190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 16-April 10

Re: Java Racing Car Applet

Posted 21 April 2010 - 03:36 PM

Its been over 24 hours since I bumped and not sure about the bumping rules....just wondering if anyone thinks they can help me out in my last post above this one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1