Results 1 to 6 of 6
  1. #1
    Joey Gobert is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default Radians to Degrees issue?

    /* Here is all of my code for a simple program. I don't know where my problem is in my code, but I can tell you what the problem is. My rectangle is fine and displays if I do not use the method rotate( radians ). I have never rotated graphics objects before so this is very hard for me. I found another method to actually converts degrees to radians. As you can see, I established a variable and set the value for radians. As soon as I try to use the method, my rectangle remains much further away from the x and y coordinates of the users mouse. If you pull the mouse up alot and some to the right, while running the program, you can see the rectangle way off in the bottom left corner of the JFrame. Everything works fine except for this one little issue. Can someone help me, please? */



    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.MouseInfo;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseWheelListener;

    public class MyAnimation extends JFrame {

    public static void main( String[] args ) {

    MyAnimation a = new MyAnimation();

    a.go();

    }

    private int speed = 0;

    private int acceleration = 0;

    private int clickedOnClickedOff = 0;

    private int x;

    private int y;

    private int r;

    private int s;

    private int t;

    public void go() {

    setSize( 500, 500 );

    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    setVisible( true );

    MouseListenerClass m = new MouseListenerClass();

    MouseMotionListenerClass c = new MouseMotionListenerClass();

    MouseWheelListenerClass q = new MouseWheelListenerClass();

    addMouseListener( m );

    addMouseMotionListener( c );

    addMouseWheelListener( q );

    }

    class MouseListenerClass implements MouseListener {

    public void mouseExited( MouseEvent e ) {

    }

    public void mouseEntered( MouseEvent e ) {

    }

    public void mouseReleased( MouseEvent e ) {

    }

    public void mousePressed( MouseEvent e ) {

    }

    public void mouseClicked( MouseEvent e ) {

    clickedOnClickedOff+=1;

    if ( clickedOnClickedOff %2 != 0 ) {

    speed = 5;

    }

    else {

    speed = 0;

    }

    System.out.println( "X axis: " + MouseInfo.getPointerInfo().getLocation().x );

    System.out.println( "Y axis: " + MouseInfo.getPointerInfo().getLocation().y );

    }

    }

    class MouseMotionListenerClass implements MouseMotionListener {


    public void mouseDragged( MouseEvent e ) {

    x = MouseInfo.getPointerInfo().getLocation().x;

    y = MouseInfo.getPointerInfo().getLocation().y;

    s = 0;

    repaint();

    }

    public void mouseMoved( MouseEvent e ) {

    x = MouseInfo.getPointerInfo().getLocation().x;

    y = MouseInfo.getPointerInfo().getLocation().y;

    s = 1;

    repaint();

    }

    }


    class MouseWheelListenerClass implements MouseWheelListener {

    public void mouseWheelMoved( MouseWheelEvent e ) {

    x = MouseInfo.getPointerInfo().getLocation().x;

    y = MouseInfo.getPointerInfo().getLocation().y;

    t+= 1;

    }

    }

    public void paint( Graphics g ) {

    g.setColor( Color.BLUE );

    g.fillRect( 0, 0, this.getWidth(), this.getHeight() );

    if ( s == 1 ) {

    g.setColor( Color.RED );

    }

    else {

    g.setColor( Color.GREEN );

    }

    if ( t %2 == 0 ) {

    g.fillOval( x - 20, y - 20, 50, 50 );

    }

    else {

    double myDegrees = Math.toRadians( 45 );

    Graphics2D d = (Graphics2D) g;

    d.rotate( myDegrees );

    g.fillRect( x+100, y+100, 30, 70 );
    }

    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I wonder if your problem is due to the rotation being done without a translation, meaning the center of rotation, the point that you'll rotate around is 0, 0 which is the upper left point of the screen. My guess is that you want the center of rotation to be somewhere else, perhaps the center of the JPanel, and if so, you'll want to use the other rotate Graphics2D method, the one that takes 3 parameters.

    Also, you want to draw in the JPanel's paintComponent method, not its paint method.

  3. #3
    Joey Gobert is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Ok. I'm going to look around the java api and look up a couple of methods. I'll tell you if I have any more problems. Thankyou!

  4. #4
    Joey Gobert is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Ok. I've edited some of my code. Here's a portion of it. I need some more instruction to this. The code i wrote didn't work. Could you show me how to do what you were talking about?
    Also, i tried using the method paintComponent( Graphics ) but i doesn't work. When i try using that method, none of the graphics display on my JFrame. Here's how i tried to edit my code:

    public void paint( Graphics g ) {

    g.setColor( Color.BLUE );

    g.fillRect( 0, 0, this.getWidth(), this.getHeight() );

    if ( s == 1 ) {

    g.setColor( Color.RED );

    }

    else {

    g.setColor( Color.GREEN );

    }

    if ( t %2 == 0 ) {

    g.fillOval( x - 20, y - 20, 50, 50 );

    }

    else {

    int heightt = 50;

    int widthh = 25;

    double myDegrees = Math.toRadians( 45 );

    Graphics2D d = (Graphics2D) g;

    g.fillRect( x, y, widthh, heightt );

    int halfWidth = widthh / 2;

    int WidthMidPoint = x - halfWidth;

    int halfHeight = heightt /2;

    int heightMidPoint = y - halfHeight;

    d.translate( WidthMidPoint, heightMidPoint );

    d.rotate( myDegrees );


    }

    }

    }

  5. #5
    Joey Gobert is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Also.. My rotating still doesn't work

  6. #6
    Joey Gobert is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    /*I think one of my post got lost so I'm going to post again. When i add a button with a button listener, it changes the graphics object to a square. When I click the button, everything seems fine, but now i can't change my object using my keylisteners..*/

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

    public class MCPP extends JFrame {


    // TEST CODE \\


    public static void main( String[] args ) {

    MCPP m = new MCPP();

    m.go();

    }

    // Create JFrame and add mouse listeners \\

    public int x;

    public int y;

    private int changeColor;

    private int changeShape;

    private int changeWidth;

    private int changeHeight;

    private int goToAutoPilotMode;

    private int botOverride;

    private int paint;

    private int t = 0;


    public void go() {

    setSize( 500, 500 );

    setVisible( true );

    setLocation( 300, 300 );

    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    MMLC mmlc = new MMLC();

    MLC mlc = new MLC();

    KLC klc = new KLC();

    addMouseMotionListener( mmlc ); // mouse motion listener class

    addMouseListener( mlc ); // motion listener class

    addKeyListener( klc ); // key listener class

    JPanel panel = new JPanel();

    Button button = new Button( "Square" );

    getContentPane().add( panel );

    panel.add( button );

    BLC1 blc1 = new BLC1();

    button.addActionListener( blc1 );

    }

    //// Mouse Motion Listener Class \\\\


    class MMLC implements MouseMotionListener {

    public void mouseMoved( MouseEvent e ) {

    x = MouseInfo.getPointerInfo().getLocation().x;

    y = MouseInfo.getPointerInfo().getLocation().y;

    changeColor = 1;

    repaint();

    }

    public void mouseDragged( MouseEvent e ) {

    x = MouseInfo.getPointerInfo().getLocation().x;

    y = MouseInfo.getPointerInfo().getLocation().y;

    changeColor = 2;

    repaint();

    }

    }

    //// Mouse Listener Class \\\\


    class MLC implements MouseListener {

    public void mouseClicked( MouseEvent e ) {

    x = MouseInfo.getPointerInfo().getLocation().x;

    y = MouseInfo.getPointerInfo().getLocation().y;

    }

    public void mouseReleased( MouseEvent e ) {

    // irrelevent for now

    }

    public void mousePressed( MouseEvent e ) {

    // irrelevent for now

    }

    public void mouseEntered( MouseEvent e ) {

    System.out.println( "Mouse has entered the JFrame.. Changing x and y" );

    x = MouseInfo.getPointerInfo().getLocation().x;

    y = MouseInfo.getPointerInfo().getLocation().x;

    paint = 1;

    }

    public void mouseExited( MouseEvent e ) {

    System.out.println( "Mouse has exited the JFrame! ... X and Y are now zero" );

    x = 0;

    y = 0;

    paint = 0;

    }

    }

    //// Key Listener Class \\\\

    class KLC implements KeyListener {

    char c;

    public void keyPressed( KeyEvent e ) {

    }

    public void keyReleased( KeyEvent e ) {

    }

    public void keyTyped( KeyEvent e ) {

    c = e.getKeyChar();

    if ( c == 'c' ) {

    changeShape = 1;

    x = MouseInfo.getPointerInfo().getLocation().x;

    y = MouseInfo.getPointerInfo().getLocation().y;

    repaint();

    }

    if ( c == 'r' ) {

    changeShape = 2;

    x = MouseInfo.getPointerInfo().getLocation().x;

    y = MouseInfo.getPointerInfo().getLocation().y;

    repaint();

    }

    if ( c == 's' ) {

    changeShape = 3;

    x = MouseInfo.getPointerInfo().getLocation().x;

    y = MouseInfo.getPointerInfo().getLocation().y;

    repaint();

    }

    }

    }

    class BLC1 implements ActionListener {

    public void actionPerformed( ActionEvent e ) {

    changeShape = 3;

    repaint();

    }

    }

    public void firstTime() {

    changeShape = 1;

    changeColor = 1;

    t++;

    }

    public void paint( Graphics g ) {

    g.setColor( Color.YELLOW );

    g.fillRect( 0, 0, this.getWidth(), this.getHeight() );

    if ( changeColor == 1 ) {

    g.setColor( Color.BLUE );

    }

    if ( changeColor == 2 ) {

    g.setColor( Color.GREEN );

    }

    if ( changeShape == 1 ) {

    g.fillOval( x, y, 30, 30 );

    }

    if ( changeShape == 2 ) {

    g.fillRect( x, y, 30, 60 );

    }

    if ( changeShape == 3 ) {

    g.fillRect( x, y, 50, 50 );

    }

    }

    }

Similar Threads

  1. help w/ conversion of degrees(CtoF, FtoC)
    By clemsontigers in forum New To Java
    Replies: 11
    Last Post: 11-08-2010, 10:57 PM
  2. Angle to Radians and Calculate the Cosine
    By coldplay7588 in forum New To Java
    Replies: 7
    Last Post: 09-26-2010, 09:12 PM
  3. nio issue
    By mawandiadeepak in forum Networking
    Replies: 2
    Last Post: 03-17-2010, 06:23 AM
  4. JSF issue
    By premjo in forum New To Java
    Replies: 0
    Last Post: 02-14-2010, 03:19 PM
  5. [j2me] sprite rotation with degrees
    By Rooneyz in forum CLDC and MIDP
    Replies: 0
    Last Post: 07-06-2009, 04:40 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
  •