Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help modifying the following Java code. When the Test.java file is compiled and run, you will see sprites move around the screen. You

I need help modifying the following Java code. When the Test.java file is compiled and run, you will see sprites move around the screen. You can move the blue sprite around by pressing the directional keys. Hitting tab makes a square highlight go around the various sprites. I need to modify this so that when the highlight is over a sprite, you can now control it and move it around based on what directional key is pressed on the keyboard. Here is the code:

Sprite.java

import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.util.LinkedList; import java.util.List; import javax.swing.Icon;

public abstract class Sprite {

public enum Direction { NONE, NORTH, EAST, SOUTH, WEST }

private int x; private int y; private Direction direction;

protected List images; protected int current;

public Sprite(int x, int y) { this.x = x; this.y = y; current = 0; }

public void draw(Component c, Graphics g) { images.get(current).paintIcon(c, g, x, y); }

public void highlight(Component c, Graphics g) { Icon icon = images.get(current); int height = icon.getIconHeight(); int width = icon.getIconWidth();

g.setColor(Color.red); g.draw3DRect(x, y, width, height, true);

}

public abstract void move(Canvas c); public abstract void animate(Canvas c);

public int getX() { return x; }

public void setX(int x) { this.x = x; }

public int getY() { return y; }

public void setY(int y) { this.y = y; }

public Direction getDirection() { return direction; }

public void setDirection(Direction direction) { this.direction = direction; }

public Icon getCurrentImage() { return images.get(current); }

}

Canvas.java

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.Graphics; import java.util.LinkedList; import java.util.List; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.Timer;

public class Canvas extends JComponent implements ActionListener, KeyListener {

private static Canvas canvas;

public static Canvas getCanvas() { if (canvas == null) { canvas = new Canvas(); } return canvas; }

private List sprites; private Timer timer; private JFrame frame; private int highlighted = 0;

private Canvas() { super();

frame = new JFrame("Monitor"); frame.setSize(800, 800); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add( this );

sprites = new LinkedList();

timer = new Timer(25, this); timer.start();

setFocusTraversalKeysEnabled(false); addKeyListener(this);

frame.setVisible(true); }

public synchronized void actionPerformed(ActionEvent evt) { for (Sprite s : sprites) { s.move(this); s.animate(this); } repaint(); }

public synchronized void addSprite(Sprite sprite) { sprites.add(sprite); }

public synchronized void paint(Graphics g) { for (Sprite s : sprites) { s.draw(this, g); } if (sprites.size() > highlighted) { Sprite s = sprites.get(highlighted); s.highlight(this, g); } }

public void keyTyped(KeyEvent e) { }

public void keyPressed(KeyEvent e) { }

public void keyReleased(KeyEvent e) { if (e.getKeyCode() == e.VK_TAB) { highlighted = highlighted + 1; if (highlighted == sprites.size()) { highlighted = 0; } } }

}

NorthSouthSprite.java

import java.util.LinkedList; import java.util.List; import javax.swing.Icon; import javax.swing.ImageIcon;

public class NorthSouthSprite extends Sprite {

public NorthSouthSprite(int x, int y) { super(x, y); images = new LinkedList(); images.add(new ImageIcon("images/north.png")); images.add(new ImageIcon("images/south.png")); setDirection(Sprite.Direction.NORTH); }

public void move(Canvas c) { switch (getDirection()) { case NORTH: setY(getY() - 10); if (getY() < 0) { setY(0); setDirection(Sprite.Direction.SOUTH); } break; case SOUTH: setY(getY() + 10); Icon icon = getCurrentImage(); if (getY() + icon.getIconHeight() > c.getSize().getHeight()) { setY((int)(c.getSize().getHeight() - icon.getIconHeight())); setDirection(Sprite.Direction.NORTH); } break; } }

public void animate(Canvas c) { switch (getDirection()) { case NORTH: current = 0; break; case SOUTH: current = 1; break; } }

}

EastWestSprite.java

import java.util.LinkedList; import java.util.List; import javax.swing.Icon; import javax.swing.ImageIcon;

public class EastWestSprite extends Sprite {

public EastWestSprite(int x, int y) { super(x, y); images = new LinkedList(); images.add(new ImageIcon("images/east.png")); images.add(new ImageIcon("images/west.png")); super.setDirection(Sprite.Direction.EAST); }

public void move(Canvas c) { switch (getDirection()) { case EAST: setX(getX() + 10); Icon icon = getCurrentImage(); if (getX() + icon.getIconWidth() > c.getSize().getWidth()) { setX((int)(c.getSize().getWidth() - icon.getIconWidth())); setDirection(Sprite.Direction.WEST); } break; case WEST: setX(getX() - 10); if (getX() < 0) { setX(0); setDirection(Sprite.Direction.EAST); } break; } }

public void animate(Canvas c) { switch (getDirection()) { case EAST: current = 0; break; case WEST: current = 1; break; } }

}

BlueSprite.java

import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.LinkedList; import java.util.List; import javax.swing.Icon; import javax.swing.ImageIcon;

public class BlueSprite extends Sprite implements KeyListener {

public BlueSprite(int x, int y) { super(x, y); images = new LinkedList(); images.add(new ImageIcon("images/BlueNorth.png")); images.add(new ImageIcon("images/BlueSouth.png")); images.add(new ImageIcon("images/BlueEast.png")); images.add(new ImageIcon("images/BlueWest.png")); setDirection(Sprite.Direction.NONE); }

public void move(Canvas c) { Icon icon = getCurrentImage();

int iconHeight = icon.getIconHeight(); int iconWidth = icon.getIconWidth(); int canvasHeight = (int)c.getSize().getHeight(); int canvasWidth = (int)c.getSize().getWidth();

switch (getDirection()) { case NORTH: setY(getY() - 10); if (getY() < 0) { setY(0); } break; case SOUTH: setY(getY() + 10); if (getY() + iconHeight > canvasHeight) { setY((int)(canvasHeight - iconHeight)); } break; case EAST: setX(getX() + 10); if (getX() + iconWidth > canvasWidth) { setX((int)(canvasWidth - iconWidth)); } break; case WEST: setX(getX() - 10); if (getX() < 0) { setX(0); } break; }

}

public void animate(Canvas c) { switch (getDirection()) { case NONE: break; case NORTH: current = 0; break; case SOUTH: current = 1; break; case EAST: current = 2; break; case WEST: current = 3; break; } }

public void keyTyped(KeyEvent e) { }

public void keyReleased(KeyEvent e) { if (e.getKeyCode() != e.VK_TAB) { setDirection(Sprite.Direction.NONE); } }

public void keyPressed(KeyEvent e) { if (e.getKeyCode() == e.VK_UP) { setDirection(Sprite.Direction.NORTH); } if (e.getKeyCode() == e.VK_DOWN) { setDirection(Sprite.Direction.SOUTH); } if (e.getKeyCode() == e.VK_RIGHT) { setDirection(Sprite.Direction.EAST); } if (e.getKeyCode() == e.VK_LEFT) { setDirection(Sprite.Direction.WEST); } }

}

Test.java

import java.util.LinkedList; import java.util.List; import javax.swing.Icon; import javax.swing.ImageIcon;

public class Test {

public static void main(String[] args) { Canvas c = Canvas.getCanvas();; c.requestFocus();

c.addSprite(new NorthSouthSprite(400, 400)); c.addSprite(new EastWestSprite(200, 200));

BlueSprite b = new BlueSprite(200, 200); c.addKeyListener(b); c.addSprite(b);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions