This is probably a simple concept that im missing. I basically want this java applet to utilize WASD keys for moving the oval around.
Here's my source code.
PathTest.java:
import java.awt.*;
import java.applet.*;
public class PathTest extends Applet
{
private Ball b;
public void init()
{
this.setSize(300,300);
b = new Ball(0,0,10,10);
}
//This method gets called when the applet is terminated
public void stop()
{
}
public void paint(Graphics g)
{
b.drawBall(g);
g.drawString("x: "+ b.getX() , 200, 50);
g.drawString("y: "+ b.getX() , 250, 50);
this.repaint();
}
}
Ball.java:
import java.awt.*;
public class Ball
{
private int x, y, sizex, sizey;
private InputListener il;
public Ball(int x, int y, int sizex, int sizey)
{
this.x = x;
this.y = y;
this.sizex = sizex;
this.sizey = sizey;
il = new InputListener(this);
}
public void drawBall(Graphics g)
{
g.fillOval(x, y, sizex, sizey);
}
public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this.y = y;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
}
InputListener.java:
The mouselistener is there for future use, as Im just using WASD for a base of input right now(been a while since ive done this).
import java.awt.event.*;
import java.awt.*;
public class InputListener implements KeyListener, MouseListener
{
private int x, y, sizex, sizey;
private Ball b;
public InputListener(Ball b)
{
this.b = b;
}
public void keyPressed( KeyEvent e )
{
if(e.getKeyCode() == e.VK_D)
{
if(x <= 298)
{
x += 2;
b.setX(x);
}
else
{
x += 0;
b.setX(x);
}
}
if(e.getKeyCode() == e.VK_A)
{
if(x >= 2)
{
x -= 2;
b.setX(x);
}
else
{
x -= 0;
b.setX(x);
}
}
if(e.getKeyCode() == e.VK_S)
{
if(y >= 2)
{
y += 2;
b.setY(y);
}
else
{
y += 0;
b.setY(y);
}
}
if(e.getKeyCode() == e.VK_W)
{
if(y <= 298)
{
y -= 2;
b.setY(y);
}
else
{
y -= 0;
b.setY(y);
}
}
}
public void keyReleased( KeyEvent e )
{
}
public void keyTyped( KeyEvent e )
{
}
public void mouseEntered( MouseEvent e )
{
}
public void mouseExited( MouseEvent e )
{
}
public void mousePressed( MouseEvent e )
{
}
public void mouseReleased( MouseEvent e )
{
}
public void mouseClicked( MouseEvent e )
{
}
}
[Edited by - agm_ultimatex on March 3, 2008 9:42:33 PM]