This is the code for my first game. So far Its supposed to be a circle that moves to wherever you click the mouse. The code compiles fine but when I run the applet the circle only moves along the y-axis, and not across. Could someone please tell me what’s wrong with it?
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class App extends Applet implements MouseListener, Runnable
{
Image offscreen;
Graphics buffer;
int posX, posY, clickX, clickY;
Thread th;
public void init()
{
offscreen = this.createImage(100,100);
buffer = offscreen.getGraphics();
addMouseListener(this);
}
public void paint(Graphics g)
{
buffer.setColor(Color.white);
buffer.fillRect(0,0,100,100);
buffer.setColor(Color.red);
buffer.fillOval(posX - 5,posY - 5,10,10);
g.drawImage(offscreen,1,1,this);
}
public void start()
{
if(th == null)
{
th = new Thread(this);
}
}
public void stop()
{
}
public void mouseClicked (MouseEvent me)
{
clickX = me.getX();
clickY = me.getY();
th.start();
}
public void run()
{
while(isActive())
{
try
{
//CLICK IS TO THE RIGHT...
if(clickX > posX)
{
//...and lower than ball
if(clickY > posY)
{
posX ++;
posY ++;
repaint();
}
//...and higher than ball
if(clickY < posY)
{
posX ++;
posY --;
repaint();
}
//...and equal height to the ball
if(clickY == posY)
{
posX++;
repaint();
}
}
//CLICK IS TO THE LEFT...
if(clickX > posX)
{
//...and lower than ball
if(clickY > posY)
{
posX --;
posY ++;
repaint();
}
//...and higher than ball
if(clickY < posY)
{
posX --;
posY --;
repaint();
}
//...and equal height to the ball
if(clickY == posY)
{
posX--;
repaint();
}
}
//CLICK IS SAME X-CO AS BALL...
if(clickX == posX)
{
//...and lower than ball
if(clickY > posY)
{
posY ++;
repaint();
}
//...and higher than ball
if(clickY < posY)
{
posY --;
repaint();
}
//...and equal height to the ball
if(clickY == posY)
{
repaint();
th.interrupt();
}
}
}
catch(Exception e)
{
}
}
}
public void mouseEntered (MouseEvent me) {}
public void mousePressed (MouseEvent me) {}
public void mouseReleased (MouseEvent me) {}
public void mouseExited (MouseEvent me) {}
}