Question
The Program: SpriteWorld When you run the original Java program, a window opens and three sprites appear; two begin to animate, the third is under
The Program: SpriteWorld When you run the original Java program, a window opens and three sprites appear; two begin to animate, the third is under user control. One of the sprites is highlighted; clicking the tab key causes the highlight to switch among the sprites.
The Changes
Our clients have supplied a fourth sprite that they would like to have included in Sprite World. However, this new sprite, known as RedSprite, does not follow the interface provided by the Sprite interface that is the basis for SpriteWorld. Our clients are rather proud of the code they wrote for that class, so they forbid us to change it in any way. You will need to use the Adapter pattern to incorporate RedSprite into SpriteWorld.
I need help figuring out how to apply the adapter pattern to incorporate the red sprite into the system. Note: You are not allowed to modify the RedSprite.java class in any way
Here is the code for the system
Canvas.java
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
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; } } }
}
Sprite.java
public abstract class Sprite {
public enum Direction { NONE, NORTH, EAST, SOUTH, WEST }
private int x; private int y; private Direction direction;
protected List
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); }
}
EastWestSprite.java
public class EastWestSprite extends Sprite {
public EastWestSprite(int x, int y) { super(x, y); images = new LinkedList
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; } }
}
NorthSouth.java
public class NorthSouthSprite extends Sprite {
public NorthSouthSprite(int x, int y) { super(x, y); images = new LinkedList
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; } }
}
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
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.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
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); } }
}
RedSprite.java
public class RedSprite {
public enum Direction { NORTH, EAST, SOUTH, WEST }
private Direction direction = Direction.NORTH; private int x; private int y; private int current; private List
public RedSprite(int x, int y) { this.x = x; this.y = y; current = 0; images = new LinkedList
public void moveMe(Canvas c) { Icon icon = images.get(current);
int iconHeight = icon.getIconHeight(); int iconWidth = icon.getIconWidth(); int canvasHeight = (int)c.getSize().getHeight(); int canvasWidth = (int)c.getSize().getWidth();
switch (direction) { case NORTH: y = y - 10; if (y < 0) { y = 0; direction = Direction.WEST; } break; case SOUTH: y = y + 10; if (y + iconHeight > canvasHeight) { y = (int)(canvasHeight - iconHeight); direction = Direction.EAST; } break; case EAST: x = x + 10; if (x + iconWidth > canvasWidth) { y = (int)(canvasWidth - iconWidth); direction = Direction.NORTH; } break; case WEST: x = x - 10; if (x < 0) { x = 0; direction = Direction.SOUTH; } break; }
}
public void animateMe(Canvas c) { switch (direction) { case NORTH: current = 0; break; case SOUTH: current = 1; break; case EAST: current = 2; break; case WEST: current = 3; break; } }
public void drawMe(Component c, Graphics g) { images.get(current).paintIcon(c, g, x, y); }
public void highlightMe(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);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started