Hello. Please exscuze the wall of code that is about to incinerate your eyeballs. But im having trouble moving this little box. Basically if you hold down left it rotates the box anti clockwise and right clockwise. And then what ever angle it facing i want to to move forward in that direction with a given distance. How i designed it is that the box rotates around a center point and then im trying to move the center point along the angle, but for some reason its not working, if you compile and run youll see what i mean. I indicated in commented line where the problem is.
Code:
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.applet.*;
public class App extends Applet implements Runnable, KeyListener
{
Thread runner;
Image Buffer;
Graphics gBuffer;
//cx, cy = center of rotation
int angle, cx, cy;
//variables for rotation
double radians = 0.0;
public double cos = 1.0;
public double sin = 0.0;
boolean left, right;
public void init()
{
this.setSize(600,400);
//create graphics buffer, the size of the applet
Buffer=createImage(size().width,size().height);
gBuffer=Buffer.getGraphics();
cx= 200;
cy= 200;
this.addKeyListener(this);
}
public void start()
{
if (runner == null)
{
runner = new Thread (this);
runner.start();
}
}
public void stop()
{
if (runner != null)
{
runner.stop();
runner = null;
}
}
public void run()
{
while(true)
{
try {runner.sleep(20);}
catch (Exception e) { }
checkRotation();
moveForward();
repaint();
}
}
//rotates x coordinate from the square array
int rotate_x(int x, int y)
{
return ((int) (cx + x * cos - y * sin) );
}
//rotates y coordinate from the square array
int rotate_y(int x, int y)
{
return ((int) (cy + y * cos + x * sin) );
}
//setting angle to radians
void SetAngle(double a)
{
radians = Math.toRadians(angle);
cos = Math.cos(radians);
sin = Math.sin(radians);
}
//THIS IS THE PROBEMS !!! ITS NOOOOT WORKING!!
public void moveForward()
{
cx += 1 * cos;
cy += 1 * sin;
}
public void RotatePolygon(int x[], int y[], int n)
{
int new_x[] = new int [n];
int new_y[] = new int [n];
for(int i=0; i<n; i++)
{
new_x[i]=rotate_x(x[i], y[i]);
new_y[i]=rotate_y(x[i], y[i]);
}
gBuffer.fillPolygon(new_x, new_y, n);
}
void drawStuff()
{
//paint background black
gBuffer.setColor(Color.black);
gBuffer.fillRect(0,0,size().width,size().height);
int radius;
int x[];
int y[];
//drawing the square
radius=0;
x= new int[4];
y= new int[4];
x[0]=-20;
x[1]=20;
x[2]=20;
x[3]=-20;
y[0]=20;
y[1]=20;
y[2]=-40;
y[3]=-40;
gBuffer.setColor(Color.red);
RotatePolygon(x, y, 4);
//display the angle in degrees
gBuffer.setColor(Color.yellow);
gBuffer.setFont(new Font("Helvetica",Font.PLAIN,13));
gBuffer.drawString("Angle="+angle+"°",10,20);
gBuffer.drawString("Left " + left + " Right " + right, 200, 20);
gBuffer.drawString("cx " + cx + " cy " + cy, 200, 35);
//the center mark
gBuffer.setColor(Color.white);
gBuffer.drawOval(cx-2,cy-2, 4, 4);
SetAngle(angle);
}
//checks if left or right arrow key is pressed
public void checkRotation() {
if(left) {
if(angle>0)
angle -= 6;
else
angle=360;
}
if(right) {
if(angle<360)
angle += 6;
else
angle=0;
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
drawStuff();
g.drawImage (Buffer,0,0, this);
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch( keyCode ) {
case KeyEvent.VK_UP:
// handle up
break;
case KeyEvent.VK_DOWN:
// handle down
break;
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_RIGHT :
right = true;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
switch( keyCode ) {
case KeyEvent.VK_UP:
// handle up
break;
case KeyEvent.VK_DOWN:
// handle down
break;
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_RIGHT :
right = false;
break;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}