Results 1 to 3 of 3
  1. #1
    Yarin1312 is offline Member
    Join Date
    Jul 2012
    Posts
    8
    Rep Power
    0

    Default 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:
    PHP 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 :)

  2. #2
    totalspelnerd is offline Member
    Join Date
    Mar 2012
    Location
    Sweden
    Posts
    88
    Rep Power
    0

    Default Re: How can i make a wheels rotate/flip

    In case you haven't found it out yet, what you are doing now is rotating it in the upper-right corner and you want to to rotate in the middle if I understood you correctly.

    g2d.rotate has 2 different parameters options. One with only rotation and one with rotation,x,y. The x and y is the position that it is going to be rotated at.

    Java Code:
    g2d.rotate(Math.toRadians(rotation),boxX+boxWidth/2,boxY+boxHeight/2);
    That is what you want if I understood you.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,229
    Rep Power
    19

    Default Re: How can i make a wheels rotate/flip

    Please don't unnecessarily bump old dead threads up to the top of the forum listing.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How do I flip this image?
    By antnas in forum New To Java
    Replies: 2
    Last Post: 11-08-2012, 08:30 PM
  2. Flip int value(1234 becomes 4321)
    By Pojahn_M in forum New To Java
    Replies: 2
    Last Post: 09-27-2012, 11:47 PM
  3. Flip Pictures on DrJava
    By siddanth1 in forum New To Java
    Replies: 2
    Last Post: 02-10-2012, 02:08 AM
  4. coin flip program
    By chandrasekhar.melam in forum New To Java
    Replies: 5
    Last Post: 09-26-2011, 05:30 PM
  5. Othello/Reversi flip checkers
    By chielt in forum Java Applets
    Replies: 10
    Last Post: 01-02-2010, 11:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •