How can i make a wheels rotate/flip
look i'm attempt to make a car moving, and i'm tring to use g2d to rotate a rectangle for example just to understands how can i make the wheels rotate/flip, so look at that code:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RotateTest extends JPanel {
Timer timer;
int x = 1;
public RotateTest(){
ActionListener task = new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
x += 1;
repaint();
}};
timer = new Timer(25, task);
timer.start();
}
@Override
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(150, 150, 150));
g2d.fillRect(20, 20, 80, 50);
g2d.translate(180, -50);
g2d.rotate(Math.PI/x);
g2d.fillRect(80, 80, 80, 50);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Rotation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new RotateTest());
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Now someone car help me with this, beacuse when you run that code the right rectangle is flip in circle and not in his specific place without change no x or y,
Can you fix the code for me? or give me an another option to make this?
thanks :)