"actionloop" is never used locally
why is the square not moving? it doesn't move when a user pressed an arrow key; as it should. plus im getting a warning saying "actionloop" (my method?) is never used locally.. do i have to call it? i thought would run automaticly.
Code:
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JApplet;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class DrawingStuff extends JApplet {
public void init() {
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
private void createGUI() {
getContentPane().add(new DrawingStuffPanel());
setSize(600, 600);
}
}
@SuppressWarnings("serial")
class DrawingStuffPanel extends JPanel {
protected static final int DELTA = 4;
private int x = 100;
private int y = 100;
public DrawingStuffPanel() {
// if using a key listener, then the component, here the JPanel must have focus
setFocusable(true);
requestFocusInWindow();
}
boolean leftKey = false;
boolean rightKey = false;
boolean upKey = false;
boolean downKey = false;
boolean fireKey = false;
int speed = 1;
public void keyPressed(KeyEvent e)
{
switch(e.getKeyCode())
{
case KeyEvent.VK_LEFT: leftKey = true;
case KeyEvent.VK_RIGHT: rightKey = true;
case KeyEvent.VK_UP: upKey = true;
case KeyEvent.VK_DOWN: downKey = true;
case KeyEvent.VK_SPACE: fireKey = true;
}
}
public void keyReleased(KeyEvent e)
{
switch(e.getKeyCode())
{
case KeyEvent.VK_LEFT: leftKey = false;
case KeyEvent.VK_RIGHT: rightKey = false;
case KeyEvent.VK_UP: upKey = false;
case KeyEvent.VK_DOWN: downKey = false;
}
}
private void actionLoop()
{
if(leftKey && downKey){
x = x - speed;
y = y + speed;
}
else if(leftKey && upKey){
y = y - speed;
x = x - speed;
}
else if(leftKey){
x = x - speed;
}
else if(rightKey && downKey){
x = x + speed;
y = y + speed;
}
else if(rightKey && upKey){
x = x + speed;
y = y - speed;
}
else if(rightKey){
x = x + speed;
}
else if(downKey){
y = y + speed;
}
else if(upKey){
y = y - speed;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.green);
g2.fill3DRect(x, y, 55, 55, true);
}
}