rotate help

ok ive got

Car car =new Car(100,100, 0.0);car.draw(g2);

Car car1 =new Car(100,110, 0.5);car1.draw(g2);

Car car2 =new Car(100,120, 0.25);car2.draw(g2);

Car car3 =new Car(100,130, 1);car3.draw(g2);

and

import java.awt.Graphics2D;

import java.awt.Color;

import java.awt.Rectangle;

import java.lang.Math;

publicclass Car

{

privateint x;

privateint y;

double rotate;

public Car(int x,int y,double rotate)

{

this.x = x;

this.y = y;

this.rotate = rotate;

}

publicvoid draw(Graphics2D g2)

{

Rectangle body =new Rectangle(x+1,y,5,7);

Rectangle wheel1 =new Rectangle(x,y+1,1,2);

Rectangle wheel2 =new Rectangle(x+6,y+1,1,2);

Rectangle wheel3 =new Rectangle(x,y+5,1,2);

Rectangle wheel4 =new Rectangle(x+6,y+5,1,2);

g2.rotate(rotate * Math.PI, x+4, y+4);

g2.setColor(Color.RED);

g2.fill(body);

g2.setColor(Color.BLACK);

g2.fill(wheel1);

g2.fill(wheel2);

g2.fill(wheel3);

g2.fill(wheel4);

}

}

and the problem is after 2nd rotation it starts to not work... putting them in different places.

its like they are using the rotate from before in some way?

any ideas?

like they are not in a line going down?

[2371 byte] By [Didge1987a] at [2007-11-27 5:14:28]
# 1

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

public class SpinningCars extends JPanel

{

Car[] cars;

public SpinningCars()

{

cars = new Car[4];

cars[0] = new Car(100,100, 0.0);

cars[1] = new Car(100,110, 0.5);

cars[2] = new Car(100,120, 0.25);

cars[3] = new Car(100,130, 1);

}

protected void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

for(int j = 0; j < cars.length; j++)

cars[j].draw(g2);

}

public static void main(String[] args)

{

SpinningCars test = new SpinningCars();

JFrame f = new JFrame();

f.getContentPane().add(test);

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

class Car

{

private int x;

private int y;

double rotate;

public Car(int x, int y, double rotate)

{

this.x = x;

this.y = y;

this.rotate = rotate;

}

public void draw(Graphics2D g2)

{

Rectangle body = new Rectangle(x+1,y,5,7);

Rectangle wheel1 = new Rectangle(x,y+1,1,2);

Rectangle wheel2 = new Rectangle(x+6,y+1,1,2);

Rectangle wheel3 = new Rectangle(x,y+5,1,2);

Rectangle wheel4 = new Rectangle(x+6,y+5,1,2);

AffineTransform orig = g2.getTransform();

g2.rotate(rotate * Math.PI, x+4, y+4);

g2.setColor(Color.RED);

g2.fill(body);

g2.setColor(Color.BLACK);

g2.fill(wheel1);

g2.fill(wheel2);

g2.fill(wheel3);

g2.fill(wheel4);

// Leave it as you found it.

g2.setTransform(orig);

}

}

crwooda at 2007-7-12 10:36:25 > top of Java-index,Security,Cryptography...
# 2

Yeah, the rotate method rotates the entire Graphics context around the point you specify, so your program just keeps turning it around and drawing in odd places.

What you need to do is turn it to the angle you want, draw the image, and then turn it back.

public void draw(Graphics2D g2)

{

Rectangle body = new Rectangle(x+1,y,5,7);

Rectangle wheel1 = new Rectangle(x,y+1,1,2);

Rectangle wheel2 = new Rectangle(x+6,y+1,1,2);

Rectangle wheel3 = new Rectangle(x,y+5,1,2);

Rectangle wheel4 = new Rectangle(x+6,y+5,1,2);

g2.rotate(rotate * Math.PI, x+4, y+4);

g2.setColor(Color.RED);

g2.fill(body);

g2.setColor(Color.BLACK);

g2.fill(wheel1);

g2.fill(wheel2);

g2.fill(wheel3);

g2.fill(wheel4);

g2.rotate(-(rotate * Math.PI), x+4, y+4); //put it back where you found it

}

The solution the person above me gave does the same thing and is slightly more elegant, so you might want to use that.

RedUnderTheBeda at 2007-7-12 10:36:25 > top of Java-index,Security,Cryptography...