How to rotate something upwards based on mouse position in Java

100 views Asked by At

Good afternoon guys, I'm trying to rotate an polygon based on my mouse position, but I can't figure out how to rotate the polygon upwards based on my mouse y. I'm using MouseMotionListener. I've tried to do this until now:

    public void mouseMoved(MouseEvent m){
             int yantes= m.getY();
             while (true){
                int y = m.getY();
                repaint();
                if (y - yantes > 0){
                  rotation++;
                  if (rotation > 360) rotation = 0; 
                  repaint();
                  break;    
                } else {
                    rotation--;
                    if (rotation < 0) rotation = 359; 
                    repaint();
                    break;  
                }
            }

        }

The yantes variable tries to calculate the y before the move, and y the y in after the movement.

1

There are 1 answers

0
Abra On

In your comment, you wrote:

I am using a book that used Java 6

Are you referring to the book Beginning Java SE 6 Game Programming, Third Edition ?

A java applet is a Container and so is a JPanel, so you can achieve the same results by extending class JPanel rather than extending Applet. That means you can write a regular java application without the need for HTML or a Web browser.

Instead of overriding method paint() in Applet, you need to override method paintComponent() in class JPanel.

The below code demonstrates rotating a square by moving the mouse. It rotates the square around the center point of the square. When you place the mouse inside the JPanel and move it to the left, the square rotates anti-clockwise. When you move the mouse to the right, the square rotates clockwise. If you move the mouse up and down, i.e. parallel to the y-axis, the square does not rotate.

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Rotating extends JPanel implements Runnable, MouseMotionListener {
    private int  theta;
    private int  lastX = Integer.MIN_VALUE;
    private BasicStroke  stroke;
    private JFrame  frame;

    /**
     * Creates and returns instance of this class.
     */
    public Rotating() {
        stroke = new BasicStroke(2.0f);
        addMouseMotionListener(this);
        setPreferredSize(new Dimension(600, 600));
    }
/* Start 'MouseMotionListener' interface methods. */
    @Override // javax.swing.event.MouseInputListener
    public void mouseDragged(MouseEvent mousEvnt) {
        // Do nothing.
    }

    @Override // javax.swing.event.MouseInputListener
    public void mouseMoved(MouseEvent mousEvnt) {
        int newX = mousEvnt.getX();
        if (lastX == Integer.MIN_VALUE) {
            lastX = newX;
        }
        if (newX < lastX) {
            theta--;
            if (theta < 0) {
                theta = 359;
            }
        }
        else if (newX > lastX) {
            theta++;
            if (theta > 360) {
                theta = 0;
            }
        }
        lastX = newX;
        repaint();
    }
/* End 'MouseMotionListener' interface methods. */
/* Start 'Runnable' interface methods. */
    @Override
    public void run() {
        showGui();
    }
/* End 'Runnable' interface methods. */
    @Override // javax.swing.JComponent
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (g instanceof Graphics2D) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.rotate(Math.toRadians(theta), 300, 300);
            g2d.setStroke(stroke);
            g2d.drawRect(200, 200, 200, 200);
        }
    }

    private void showGui() {
        frame = new JFrame("Rotating");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(this, BorderLayout.CENTER);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Rotating());
    }
}

Note that when overriding method paintComponent() you nearly always need to first call method paintComponent() in the super class.