0

I'm hope to make a very basic game for my end of year project (going though computer science in high school) but cant find any answers as to how to make the rectangle I am drawing to face the cursor. This site has been very useful in the past but this is my first question and i hope you guys can help.

dir = Math.toRadians(Math.atan2(yy1,xx1)/2*Math.PI);

I'm presuming the either this peice of code that is the problem or,

g2d.rotate(dir,xx,yy);

This. Should I use affine transformation and if so how?

Thanks in advance.

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.image.*;

public class W extends Applet implements KeyListener, MouseMotionListener {
int xx=100,yy=100,xx1,yy1,wid,hig;
String key;
Rectangle rect1;
double dir, trueing=0;

Image offImage;
Graphics offGraphics;

public void init() {
    setSize(800,600);
    addKeyListener(this);
    addMouseMotionListener(this);
}

public void update(Graphics g) {
     if (offGraphics == null) {
        offImage = createImage(800,600);
        offGraphics = offImage.getGraphics();
    }

    offGraphics.setColor(Color.WHITE);
    offGraphics.fillRect(0,0,800,600);

    Graphics2D g2d = (Graphics2D)offGraphics;
    offGraphics.setColor(Color.BLACK);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

    g2d.rotate(dir,xx,yy);

    g2d.draw(new Rectangle(xx, yy, 10, 10));

    offGraphics.setColor(getForeground());
    g.drawImage(offImage, 0, 0, this);

}

public void paint(Graphics g) {
    update(g);
}

public void Game(){

}

public void keyPressed(KeyEvent ke){
    if (ke.getKeyCode() == KeyEvent.VK_UP){
        yy--;
        key = "u";
        System.out.println(key+xx+"-"+yy);
        repaint();
    }
    if (ke.getKeyCode() == KeyEvent.VK_DOWN){
        yy++;
        key = "d";
        System.out.println(key+xx+"-"+yy);
        repaint();
    }
    if (ke.getKeyCode() == KeyEvent.VK_LEFT){
        xx--;
        key = "l";
        System.out.println(key+xx+"-"+yy);
        repaint();
    }
    if (ke.getKeyCode() == KeyEvent.VK_RIGHT){
        xx++;
        key = "r";
        System.out.println(key+xx+"-"+yy);repaint();
        repaint();
    }
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}

public void mouseMoved(MouseEvent e) {
    xx1=e.getX();
    yy1=e.getY();
    showStatus( "Mouse at (" + xx1 + "," + yy1 + ")" );
    dir = Math.toRadians(Math.atan2(yy1,xx1)/2*Math.PI);

    repaint();

}

public void mouseDragged(MouseEvent e) {
}

}

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) Why code an applet? If it is due due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson May 27 '13 at 02:45
  • What is the problem? Does it not work? Does it work incorrectly? – Zong May 27 '13 at 03:02
  • Thanks ill go learn swing I think, our teacher just kinda hand out the course work, and no it doesn't work the rectangle just spins when you move the mouse. – Jarra Taurian May 27 '13 at 06:40

1 Answers1

0

There are few things that I think are missing from your program.

You don't reset the rotation. After you do the rotation draw the object you want to rotate then reset the rotation using the AffineTransform

AffineTransform reset = new AffineTransform();
reset.rotate(0,0,0);

Then you can apply the transform to only rotate a single object.

g2d.rotate(dir,xx,yy);
g2d.draw(new Rectangle(xx, yy, 10, 10));
g2d.setTransform(reset);

When you are calculating the angle to which the object should rotate I would recommend using:

xx1 = e.getX();
yy1 = e.getY();
dir = Math.toRadians(Math.atan2(yy - yy1, xx - xx1));

I say this mostly because I don't understand what your math was trying to accomplish.

Hope this helps.

Andrew Cumming
  • 965
  • 6
  • 14