Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help animating!

Status
Not open for further replies.

Unlucky4Ever

Technical User
May 27, 2010
3
US
I need this ASAP!

I need to know how to animate this...

Code:
// This program draws a pentagon with the <drawPolygon> method.
// Methods <drawPolygon> and <fillPolygon> take a Polygon object as
// parameter.  The polygon object stores a set of points.  Points can 
// be added to a Polygon object with the <addPoint> method.


import java.awt.*;
import java.applet.*;


public class Example1 extends Applet
{
	public void paint(Graphics g)
	
	{
		//road
		g.setColor(Color.gray);
		
		g.fillRect(0,620,1050,620);
		g.setColor(Color.yellow);
		g.drawLine(0,650,1050,650);
		g.setColor(Color.black);
		g.drawLine(0,675,1050,675);
	
	
	//sky
	g.setColor(Color.blue);
	g.drawRect(0,0,1050,100);
	g.fillRect(0,0,1050,100);
	g.setColor(Color.gray);
	g.fillOval(325,85,50,50);
	g.fillOval(350,75,85,70);
	g.fillOval(410,85,50,50);
	
	g.fillOval(525,45,50,50);
	g.fillOval(550,35,85,70);
	g.fillOval(610,45,50,50);
	
	g.fillOval(725,75,50,50);
	g.fillOval(750,65,85,70);
	g.fillOval(810,75,50,50);
	
	
	//sun 
	g.setColor(Color.orange); 
	g.fillOval(-30,-30,185,185);
	g.drawLine(200,20,230,45);


		//car1		
		g.setColor(Color.darkGray);
		g.fillRect(590,600,40,20);
		Polygon penta = new Polygon();
		
		g.setColor(Color.blue);
		penta.addPoint(600,540);
		penta.addPoint(940,540);
		penta.addPoint(940,540);
		penta.addPoint(980,580);
		penta.addPoint(980,620);
		penta.addPoint(620,620);
		penta.addPoint(600,600);
		penta.addPoint(600,540);
	
		g.fillPolygon(penta);
		g.setColor(Color.yellow);
		g.fillRect(600,540,30,20);

	
		//windsheild1
		Polygon wind = new Polygon();
		wind.addPoint(680,540);
		wind.addPoint(700,470);
		wind.addPoint(720,470);
		wind.addPoint(700,540);
		g.setColor(Color.lightGray);
		g.fillPolygon(wind);
		
		//wheel1
		
		g.drawPolygon(penta);
		g.setColor(Color.black);	
		penta.addPoint(680,640);
		penta.addPoint(740,480);
		penta.addPoint(740,540);
		
		//rim/tire1
		g.setColor(Color.black);
		g.fillOval(660, 580, 80, 80);
		g.setColor(Color.yellow);
		g.fillOval(670, 590, 60, 60);
		
		//rim/tire2
		g.setColor(Color.black);
		g.fillOval(880, 580, 80, 80);
		g.setColor(Color.yellow);
		g.fillOval(890, 590, 60, 60);
		
		
		
		
		
		
		//car2		
		g.setColor(Color.darkGray);
		g.fillRect(190,600,40,20);
		Polygon penta1 = new Polygon();
		
		g.setColor(Color.blue);
		penta1.addPoint(100,540);
		penta1.addPoint(440,540);
		penta1.addPoint(440,540);
		penta1.addPoint(480,580);
		penta1.addPoint(480,620);
		penta1.addPoint(120,620);
		penta1.addPoint(100,600);
		penta1.addPoint(100,540);
	
		g.fillPolygon(penta1);
		g.setColor(Color.yellow);
		g.fillRect(100,540,30,20);
		g.setColor(Color.lightGray);
		
		
		//wheel2
		
		g.drawPolygon(penta);
		g.setColor(Color.black);	
		penta.addPoint(180,640);
		penta.addPoint(240,480);
		penta.addPoint(240,540);
		
		//rim/tire2
		g.setColor(Color.black);
		g.fillOval(160, 580, 80, 80);
		g.setColor(Color.yellow);
		g.fillOval(170, 590, 60, 60);
		
		//rim/tire2
		g.setColor(Color.black);
		g.fillOval(380, 580, 80, 80);
		g.setColor(Color.yellow);
		g.fillOval(390, 590, 60, 60);

	
		
	}	
}

There is the entire code, and I want to make the cars move left off of the screen.
 
Currently it isn't animated, I was hoping someone would animate it for me =/

When I try to animate it, just the wheels move, and they form giant lines...

And by ASAP, I meant within the next few hours.
 
If you take a job, you can't solve, that's your problem. We aren't your hired workers, so go to rent-a-coder, if you need a solution in hours.

But let's see what we can do.

a) If you don't have a strong argument, use java.swing.JApplet instead of Applet.
b) Refactor your code. Instead of your comments, I made the code between them to methods, which looks like this:

Code:
import java.awt.*;
import javax.swing.*;

public class AnimationApplet extends JApplet
{
	public void paint (Graphics g)
	{
		road (g);
		sky (g);
		sun (g);
		car1 (g);
		windshield1 (g);
		wheel (g, 1);
		rimTire1car1 (g);
		rimTire2car1 (g);
		car2 (g);
		wheel (g, 2);
		rimTire1car2 (g);
		rimTire2car2 (g);
	}

	private void rimTire2car2 (Graphics g)
	{
		g.setColor (Color.black);
		g.fillOval (380, 580, 80, 80);
		g.setColor (Color.yellow);
		g.fillOval (390, 590, 60, 60);
	}

	private void rimTire1car2 (Graphics g)
	{
		g.setColor (Color.black);
		g.fillOval (160, 580, 80, 80);
		g.setColor (Color.yellow);
		g.fillOval (170, 590, 60, 60);
	}
/*and so on ... */

Now you can place similar code, like for wheel1 and wheel2 close together, to extract the sameness, and drive the differences by parameters:

Code:
	private void wheel (Graphics g, int wheel)
	{
		Polygon penta = new Polygon ();
		int x = (wheel-1) * 500;
		g.drawPolygon (penta);
		g.setColor (Color.black);
		penta.addPoint (x + 180, 640);
		penta.addPoint (x + 240, 480);
		penta.addPoint (x + 240, 540);
	}
I guess you get the point and may modify your code accordingly, which leads you to following main paint-method:
Code:
	public void paint (Graphics g)
	{
		road (g);
		sky (g);
		sun (g);
		car (g, 1);
		windshield1 (g);
		wheel (g, 1);
		rimTire (g, 1, 1);
		rimTire (g, 1, 2);
		car (g, 2);
		wheel (g, 2);
		rimTire (g, 2, 1);
		rimTire (g, 2, 2);
	}

There is no code animating anything, not even ugly wheels. You need to start a loop, which pauses for short intervalls, and moves the objects along the x-Axis.

So you first need to make your methods parametrized from an x-offset, which changes from step to step.

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top