Java Object Oriented Programming Sprites Objectives Learn how
Java Object Oriented Programming Sprites
Objectives: • Learn how Sprites are used in computer graphics. Lab 21 -2
Sprites § In computer graphics, a sprite is a twodimensional image or animation that is integrated into a larger scene. § In object oriented programming a sprite is a class with properties and behaviors defined by the class. § In more specific terms, a sprite is an abstract data type that represents an image or animation in a GUI application. Lab 21 -3
Java Object Oriented Programming Top Down Design
Sprites • Start JCreator. • Create a new file called “Sprite. java” • Save “Sprite. java” in your Lab 21/Tanks folder. Lab 21 -5
Java Object Oriented Programming The Sprite Class
Sprite Import statements. package tanks; import java. awt. Rectangle; import java. awt. Graphics; import java. awt. image. Buffered. Image; import javax. swing. JPanel; Lab 21 -7
Sprite Class declaration. class Sprite { } Lab 21 -8
Sprite has package-private access. class Sprite { } Lab 21 -9
Sprite Constants class Sprite { public static final byte UP = 0; public static final byte RIGHT = 1; public static final byte DOWN = 2; public static final byte LEFT = 3; } Lab 21 -10
Sprite Fields class Sprite { public static final byte UP = 0; public static final byte RIGHT = 1; public static final byte DOWN = 2; public static final byte LEFT = 3; private Buffered. Image image; private Rectangle bounds; private int direction; private boolean moving; } Lab 21 -11
Sprite Constructor Sprite(Buffered. Image image, int x, int y) { image = img; bounds = new Rectangle(x, y, image. get. Width(), image. get. Height()); direction = LEFT; } Lab 21 -12
Sprite Accessors (get the location) public int get. X() { return bounds. x; } public int get. Y() { return bounds. y; } Lab 21 -13
Sprite Accessors (get the size) public int get. Width() { return bounds. width; } public int get. Height() { return bounds. height; } Lab 21 -14
Sprite Accessor (get the direction) public int get. Direction() { return direction; } Lab 21 -15
Sprite Accessor (get the bounds) public Rectangle get. Bounds() { return bounds; } Lab 21 -16
Sprite Accessor (is the tank moving? ) public int is. Moving() { return moving; } Lab 21 -17
Sprite Mutators (set the location) public void set. X(int x) { bounds. x = x; } public void set. Y(int y) { bounds. y = y; } Lab 21 -18
Sprite Mutator (set the direction) public void set. Direction(int dir) { direction = dir; } Lab 21 -19
Sprite Mutator (set moving) public void set. Moving(int move) { moving = move; } Lab 21 -20
Sprite draw. Sprite (draw the sprite) public void draw. Sprite(Graphics g) { Utility. rotate. Image(g, image, direction, bounds. x, bounds. y); } Lab 21 -21
Sprite act – intended to be overridden. public void act(JPanel panel, Sprite[] sprites) { } Lab 21 -22
Java Object Oriented Programming The Tank Class
Tank Import statements package tanks; import java. awt. image. Buffered. Image; import javax. swing. JPanel; Lab 21 -24
Tank Class declaration. class Tank extends Sprite { } Lab 21 -25
Tank Constructor public Tank(Buffered. Image image, int x, int y) { super(image, x, y); } Lab 21 -26
Tanks Implement the act method. @Override public void act(JPanel panel, Sprite[] sprites) { } Lab 21 -27
Tanks Implement the act method. @Override public void act(JPanel panel, Sprite[] sprites) { if (is. Moving()) { } } Lab 21 -28
Tanks int x = get. X(); int y = get. Y(); Lab 21 -29
Tanks int x = get. X(); int y = get. Y(); switch (get. Direction()) { } Lab 21 -30
Tanks int x = get. X(); switch (get. Direction()) { case RIGHT: if (x < panel. get. Width() – get. Width()) x += 2; break; Lab 21 -31
Tanks case LEFT: if (x > 0) x -= 2 break; Lab 21 -32
Tanks case UP: if (y > 0) x -= 2 break; Lab 21 -33
Tanks case DOWN: if (y < panel. get. Height() – get. Height()) y += 2 break; } set. X(x); set. Y(y); Lab 21 -34
Tanks set. X(x); set. Y(y); Sprite s = collision(sprites); if (s != null) { } } // if (is. Moving()) } Lab 21 -35
Tanks if (s != null) { switch (get. Direction()) { } } Lab 21 -36
Tanks case LEFT: set. X(x + 2); break; Lab 21 -37
Tanks case RIGHT: set. X(x - 2); break; Lab 21 -38
Tanks case UP: set. Y(y + 2); break; Lab 21 -39
Tanks case DOWN: set. Y(y - 2); break; Lab 21 -40
Tanks private boolean collision(Sprite[] sprites) { for (int i = 0; i < sprites. length; i++) { if (sprites[i] != this) return get. Bounds(). intersects(sprites[i]. get. Bounds()); } return false; } Lab 21 -41
Java Object Oriented Programming The Tanks Class (the main class)
Tanks Import statements package tanks; import java. awt. *; import javax. swing. *; import java. awt. event. *; Lab 21 -43
Tanks Class declaration. public class Tanks extends JFrame { } Lab 21 -44
Java Object Oriented Programming The Nested Tank. Panel Class
Tanks Nested Class declaration. public class Tanks extends JFrame { private Tank. Panel extends JPanel { } } Lab 21 -46
Tank. Panel Constructor Tank. Panel() { set. Background(new Color(0, 64, 0)); set. Preferred. Size(new Dimension(800, 600)); } Lab 21 -47
Tank. Panel @Override public void paint. Component(Graphics g) { } Lab 21 -48
Tank. Panel @Override public void paint. Component(Graphics g) { super. paint. Component(g); } Lab 21 -49
Tank. Panel @Override public void paint. Component(Graphics g) { super. paint. Component(g); for (int i = 0; i < sprites. length; i++) sprites[i]. draw. Sprite(g); } Lab 21 -50
Tanks Fields public class Tanks extends JFrame { private class Tank. Panel extends JPanel { /* Code Not Shown */ } private Tank. Panel tank. Panel; private Sprite[] sprites; private Timer timer; } Lab 21 -51
Tanks The main method. public static void main(String args[]) { new Tanks(). set. Visible(true); } Lab 21 -52
Tanks Constructor – Initialize the JFrame Tanks() { set. Title(“Tanks"); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); set. Icon. Image(Utility. load. Image("icon. gif")); Lab 21 -53
Tanks Instantiate an array of Sprites of size 1. Tanks() { set. Title(“Tanks"); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); set. Icon. Image(Utility. load. Image("icon. gif")); sprites = new Sprite[1]; Lab 21 -54
Tanks Instantiate a Tanks() { set. Title(“Tanks"); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); set. Icon. Image(Utility. load. Image("icon. gif")); sprites = new Sprite[1]; sprites[0] = new Tank(Utility. load. Image(“tank. gif"), 200, 50); Lab 21 -55
Tanks Instantiate a Tanks() { set. Title(“Tanks"); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); set. Icon. Image(Utility. load. Image("icon. gif")); sprites = new Sprite[1]; sprites[0] = new Tank(Utility. load. Image(“tank. gif"), 200, 50); sprites[0]. set. Direction(Sprite. RIGHT); Lab 21 -56
Tanks Instantiate a Tank. Panel. Tanks() { set. Title(“Tanks"); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); set. Icon. Image(Utility. load. Image("icon. png")); sprites = new Sprite[1]; sprites[0] = new Ball(Utility. load. Image("ball. png"), 0, 50); sprites[0]. set. Direction(Sprite. RIGHT); tank. Panel = new Tank. Panel(); set. Content. Pane(tank. Panel); Lab 21 -57
Tanks Add a Key. Listener to the panel. set. Content. Pane(tank. Panel); add. Key. Listener(new Key. Adapter() { }); Lab 21 -58
Tanks set. Content. Pane(tank. Panel); tank. Panel. add. Key. Listener(new Key. Adapter() { public void key. Pressed(Key. Event e) { } }); Lab 21 -59
Tanks set. Content. Pane(tank. Panel); tank. Panel. add. Key. Listener(new Key. Adapter() { public void key. Pressed(Key. Event e) { switch (e. get. Key. Code()) { } } }); Lab 21 -60
Tanks case Key. Event. VK_LEFT: sprites[0]. set. Direction(Sprite. LEFT); sprites[0]. set. Moving(true); break; Lab 21 -61
Tanks case Key. Event. VK_RIGHT: sprites[0]. set. Direction(Sprite. RIGHT); sprites[0]. set. Moving(true); break; Lab 21 -62
Tanks case Key. Event. VK_UP: sprites[0]. set. Direction(Sprite. UP); sprites[0]. set. Moving(true); break; Lab 21 -63
Tanks case Key. Event. VK_DOWN: sprites[0]. set. Direction(Sprite. DOWN); sprites[0]. set. Moving(true); break; Lab 21 -64
Tanks set. Content. Pane(tank. Panel); tank. Panel. add. Key. Listener(new Key. Adapter() { public void key. Pressed(Key. Event e) { /** Code Not Shown **/ } public void key. Released(Key. Event e) { } }); Lab 21 -65
Tanks public void key. Released(Key. Event e) { switch (e. get. Key. Code()) { } } Lab 21 -66
Tanks case Key. Event. VK_LEFT: if (sprites[0]. get. Direction() == Sprite. LEFT) sprites[0]. set. Moving(false); break; Lab 21 -67
Tanks case Key. Event. VK_RIGHT: if (sprites[0]. get. Direction() == Sprite. RIGHT) sprites[0]. set. Moving(false); break; Lab 21 -68
Tanks case Key. Event. VK_UP: if (sprites[0]. get. Direction() == Sprite. UP) sprites[0]. set. Moving(false); break; Lab 21 -69
Tanks case Key. Event. VK_DOWN: if (sprites[0]. get. Direction() == Sprite. DOWN) sprites[0]. set. Moving(false); break; Lab 21 -70
Tanks Initialize the timer = new Timer(5, new Action. Listener() { }); } Lab 21 -71
Tanks timer = new Timer(5, new Action. Listener() { public void action. Performed(Action. Event e) { } }); } Lab 21 -72
Tanks timer = new Timer(5, new Action. Listener() { public void action. Performed(Action. Event e) { for (int i = 0; i < sprites. length; i++) sprites[i]. act(tank. Panel, sprites); } }); } Lab 21 -73
Tanks timer = new Timer(5, new Action. Listener() { public void action. Performed(Action. Event e) { for (int i = 0; i < sprites. length; i++) sprites[i]. act(tank. Panel); tank. Panel. repaint(); } }); } Lab 21 -74
Tanks timer = new Timer(20, new Action. Listener() { public void action. Performed(Action. Event e) { for (int i = 0; i < sprites. length; i++) sprites[i]. act(ball. Panel); ball. Panel. repaint(); } }); timer. start(); } Lab 21 -75
Tanks Initialize the timer = new Timer(20, new Action. Listener() { public void action. Performed(Action. Event e) { for (int i = 0; i < sprites. length; i++) sprites[i]. act(ball. Panel); ball. Panel. repaint(); } }); timer. start(); pack(); } Lab 21 -76
Tanks Run Tanks. java. Lab 21 -77
Why did we use an array of sprites instead of a single Sprite or even a Tank object? Lab 21 -78
How hard would it be to add a second Tank? Lab 21 -79
Add a second Tank to the program. The second Tank should begin at location (400, 300) Lab 21 -80
Tanks Redimension the array. Tanks() { set. Title(“Tanks”); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); sprites = new Sprite[2]; sprites[0] = new Tank(Utility. load. Image(“tank. gif”), 200, 50); tank. Panel = new Tank. Panel(sprites); set. Content. Pane(tank. Panel); Lab 21 -81
Tanks Instantiate a second Ball. Tanks() { set. Title(“Tanks”); set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); sprites = new Sprite[2]; sprites[0] = new Ball(Utility. load. Image(“tank. gif”), 200, 50); sprites[1] = new Ball(Utility. load. Image(“tank. gif”), 400, 300); tank. Panel = new Tank. Panel(sprites); set. Content. Pane(tank. Panel); Lab 21 -82
Java Object Oriented Programming Questions?
Java Object Oriented Programming Begin Lab 21
- Slides: 84