//*Armando Madrid*//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SpacePanel extends JPanel
{
private final int WIDTH = 500, HEIGHT = 500;
private final int IMAGE_SIZE = 40;
private ImageIcon ship, space;
private int x, y, moveX, moveY;
public SpacePanel()
{
addKeyListener (new MoveListener());
ship = new ImageIcon ("ship.gif");
space = new ImageIcon ("url.jpg");
x = 250;
y = 250;
moveX = moveY = 2;
setPreferredSize (new Dimension (WIDTH, HEIGHT));
//page.setBackground (space);
setFocusable (true);
}
public void paintComponent (Graphics page)
{
super.paintComponent (page);
ship.paintIcon (this, page, x, y);
ship.paintIcon (this, page, x - WIDTH, y);
ship.paintIcon (this, page, x - WIDTH, y - HEIGHT);
ship.paintIcon (this, page, x, y - HEIGHT);
x += moveX;
y += moveY;
if (x == 0)
x += WIDTH;
x = x % WIDTH;
if (y == 0)
y += HEIGHT;
y = y % HEIGHT;
}
private class MoveListener implements KeyListener
{
public void keyPressed (KeyEvent event){}
public void keyReleased (KeyEvent event)
{
switch (event.getKeyCode())
{
case KeyEvent.VK_UP:
moveY += -1;
break;
case KeyEvent.VK_DOWN:
moveY += 1;
break;
case KeyEvent.VK_LEFT:
moveX += 1;
break;
case KeyEvent.VK_RIGHT:
moveX += -1;
break;
repaint();
}
}
}
}
This post has been edited by no2pencil: 04 December 2012 - 03:14 PM
Reason for edit:: Corrected Code Tags

New Topic/Question
Reply



MultiQuote




|