PDA

View Full Version : [JAVA] Help


Lawn Gnome
10-15-2005, 11:38 PM
Ok i have this code its an applet.



//Direction.java

import javax.swing.*;

public class Direction extends JApplet
{
public void init()
{
getContentPane().add (new DirectionPanel(this));
}
}


and this one



//DirectionPanel.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DirectionPanel extends JPanel
{
private final int WIDTH = 300, HEIGHT = 200;
private final int JUMP = 10;

private final int IMAGE_SIZE = 31;

private ImageIcon up, down, right, left, currentImage;
private int x, y;

public DirectionPanel (JApplet applet)
{
applet.addKeyListener (new DirectionListener());

x = WIDTH / 2;
y = HEIGHT / 2;

up = new ImageIcon ("arrowUp.gif");
down = new ImageIcon ("arrowDown.gif");
left = new ImageIcon ("arrowLeft.gif");
right = new ImageIcon ("arrowRight");

currentImage = right;

setBackground (Color.black);
setPreferredSize (new Dimension(WIDTH, HEIGHT));
}
public void paintComponent (Graphics page)
{
super.paintComponent (page);
currentImage.paintIcon (this, page, x, y);
}
private class DirectionListener implements KeyListener
{
public void KeyPressed (KeyEvent event)
{
switch (event.getKeyCode())
{
case KeyEvent.VK_UP:
currentImage = up;
if (y > 0)
y -= JUMP;
break;
case KeyEvent.VK_DOWN:
currentImage = down;
if (y < HEIGHT-IMAGE_SIZE)
y += JUMP;
break;
case KeyEvent.VK_LEFT:
currentImage = left;
if (x > 0)
x -= JUMP;
break;
case KeyEvent.VK_RIGHT:
currentImage = right;
if (x < WIDTH-IMAGE_SIZE)
x += JUMP;
break;
}
repaint();
}
public void KeyTyped (KeyEvent event) {}
public void KeyReleased (KeyEvent event) {}
}
}


it might just be my spelling i dont know. a little help would be nice thx :D

gsoft
10-15-2005, 11:42 PM
Yes a little help would be nice, what is the error?

Lawn Gnome
10-16-2005, 12:01 AM
dose it work for u???

it dont work for me. what compiler u using?