sorry if its a bit long, this is as short as i could make it, thx again for taking a look:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;
public class SSCCE extends JPanel {
public static void main(String args[]){
SSCCE s = new SSCCE();
JFrame f = new JFrame("SSCCE");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(500,500);
f.add(s);
}
public SSCCE(){
Handler handler = new Handler();
addMouseMotionListener(handler);
addMouseListener(handler);
ballx =200;
bally=200;
stickx = ballx+6;
sticky = bally+5;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.fillOval(ballx, bally, 13, 13);
Graphics2D g2 = (Graphics2D)g;
// rotate with anchor point in the middle of the ball
g2.transform(AffineTransform.getRotateInstance(theta, ballx+6, bally+6));
//draw the stick at the center of the ball
g2.fillRect(stickx, sticky, 200, 3);
}
private class Handler implements MouseMotionListener, MouseListener{
private int MouseInX;
private int MouseInY;
private int hyp;
private int origCueX;
private int origCueY;
public void mouseMoved(MouseEvent e) {
o =-e.getY()+ ballx+6 ;
a =-e.getX() + bally+6 ;
theta = Math.atan2(o, a);
repaint();
//record where mouse stopped
MouseInX= e.getX();
MouseInY= e.getY();
origCueX = ballx+6;
origCueY = bally +5;
}
public void mouseDragged(MouseEvent e) {
hyp = (int) Math.hypot((MouseInX-e.getX()),(MouseInY-e.getY()));
stickx = (int) (origCueX +((hyp)*(Math.cos(theta))));
sticky = (int) (origCueY +((hyp)*(Math.sin(theta))));
repaint();
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent arg0) {
stickx = origCueX;
sticky= origCueY;
repaint();
}
}
double o; // used for stick rotation
double a; //used for stick rotation
double theta; // rotates stick
private int stickx;
private int sticky;
private int ballx;
private int bally;
}