Draw character that bounces around screen

Draw character that bounces around screen 

Animation

Getting this code to work is simple. Adding parts in a nice way requires thinking:

software engineering. Try to think in terms of writing so that your code would be easy for someone else to modify/maintain.

Non-graded portions

Get Animation.java running. You must place the orc images in the correct directory structure for the file (see code). You may work with others to get it working.

Modify Animation.java:

_ See the three code sections marked TODO. Read the comments.

_ Keep orc from walking off screen

_ Make orc bounce off the edges. The orc will then travel in a new direction andyou will need to use different image sequences for the new direction. Use at leastfour different directions. Only read each .png file once (why?). 

Animation.java

import java.awt.*;

import java.awt.image.BufferedImage;

import java.awt.image.*;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Animation extends JPanel {

final int frameCount = 10;

int picNum = 0;

String queuedimgfile = “images/orc_forward_southeast.png”;

BufferedImage img;

BufferedImage[] pics;

int xloc = 0;

int yloc = 0;

final int xIncr = 8;

final int yIncr = 2;

final static int frameWidth = 500;

final static int frameHeight = 300;

final static int imgWidth = 165;

final static int imgHeight = 165;

boolean moveEast = true;

boolean moveSouth = true;

// Override this JPanel’s paint method to cycle through picture array and draw images

public void paint(Graphics g) {

picNum = (picNum + 1) % frameCount;

//          g.drawImage(pics[picNum], xloc += xIncr, yloc += yIncr, Color.gray, this);

if (xloc < 0) {

moveEast = false;

}

else if (xloc > frameWidth – imgWidth) {

moveEast = true;

}

if (xloc < 0) {

moveSouth = false;

}

else if (yloc > frameHeight – imgHeight) {

moveSouth = true;

}

if(moveEast) {

xloc -= xIncr;

queuedimgfile = “images/orc_forward_east.png”;

}

else if (!moveEast) {

xloc += xIncr;

queuedimgfile = “images/orc_forward_west.png”;

}

if(moveSouth) {

yloc -= yIncr;

queuedimgfile = “images/orc_forward_south.png”;

}

else if (!moveSouth){

yloc += yIncr;

queuedimgfile = “images/orc_forward_north.png”;

}

if (moveSouth && moveEast){

queuedimgfile = “images/orc_forward_southeast.png”;

}

if (!moveSouth && moveEast){

queuedimgfile = “images/orc_forward_northeast.png”;

}

if (!moveSouth && !moveEast){

queuedimgfile = “images/orc_forward_northwest.png”;

}

if (moveSouth && !moveEast){

queuedimgfile = “images/orc_forward_southwest.png”;

}

g.drawImage(pics[picNum], xloc, yloc, Color.gray, this);

// TODO: Keep the orc from walking off-screen, turn around when bouncing off walls.

// Be sure that animation picture direction matches what is happening on screen.

}

// Make frame, loop on repaint and wait

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.getContentPane().add(new Animation());

frame.setBackground(Color.gray);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(frameWidth, frameHeight);

frame.setVisible(true);

for (int i = 0; i < 1000; i++) {

frame.repaint();

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

// Constructor: get image, segment and store in array

public Animation() {

BufferedImage img = (createImage(new File(queuedimgfile)));

pics = new BufferedImage[frameCount];

for (int i = 0; i < frameCount; i++)

pics[i] = img.getSubimage(imgWidth * i, 0, imgWidth, imgHeight);

// TODO: Change this constructor so that at least eight orc animation pngs are loaded

}

// Read image from file and return

private BufferedImage createImage(File imagefile) {

BufferedImage bufferedImage;

try {

bufferedImage = ImageIO.read(imagefile);

return bufferedImage;

} catch (IOException e) {

e.printStackTrace();

}

return null;

// DONE: Change this method so you can load other orc animation bitmaps

}

} 

Solution

Animation.java

import java.awt.*;

import java.awt.image.BufferedImage;

import java.awt.image.*;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class Animation extends JPanel {

final int frameCount = 10;

int picNum = 0;

BufferedImage[] picsSE;

BufferedImage[] picsSW;

BufferedImage[] picsNE;

BufferedImage[] picsNW;

int xloc = 0;

int yloc = 0;

final int xIncr = 8;

final int yIncr = 2;

final static int frameWidth = 500;

final static int frameHeight = 300;

final static int imgWidth = 165;

final static int imgHeight = 165;

boolean moveEast = true;

boolean moveSouth = true;

// Override this JPanel’s paint method to cycle through picture array and draw images

public void paint(Graphics g) {

picNum = (picNum + 1) % frameCount;

if (xloc < 0) {

moveEast=true;

picNum=0;

}

else if (xloc > frameWidth – imgWidth) {

moveEast=false;

picNum=0;

}

if (yloc < 0) {

moveSouth=true;

picNum=0;

}

else if (yloc > frameHeight – imgHeight) {

moveSouth=false;

picNum=0;

}

if(moveSouth && !moveEast)    //SOUTHWEST

{

xloc -= xIncr;

yloc += yIncr;

g.drawImage(picsSW[picNum], xloc, yloc, Color.gray, this);

}

else if(moveSouth && moveEast)      //SOUTHEAST

{

xloc += xIncr;

yloc += yIncr;

g.drawImage(picsSE[picNum], xloc, yloc, Color.gray, this);

}

else if(!moveSouth && moveEast)     //NORTHEAST

{

xloc += xIncr;

yloc -= yIncr;

g.drawImage(picsNE[picNum], xloc, yloc, Color.gray, this);

}

else if(!moveSouth && !moveEast)    //NORTHWEST

{

xloc -= xIncr;

yloc -= yIncr;

g.drawImage(picsNW[picNum], xloc, yloc, Color.gray, this);

}

// TODO: Keep the orc from walking off-screen, turn around when bouncing off walls.

// Be sure that animation picture direction matches what is happening on screen.

}

// Make frame, loop on repaint and wait

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.getContentPane().add(new Animation());

frame.setBackground(Color.gray);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(frameWidth, frameHeight);

frame.setVisible(true);

for (int i = 0; i < 1000; i++) {

frame.repaint();

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

// Constructor: get image, segment and store in array

public Animation() {

BufferedImage img = (createImage(new File(“images/orc_forward_southeast.png”)));

picsSE = new BufferedImage[frameCount];

for (int i = 0; i < frameCount; i++)

picsSE[i] = img.getSubimage(imgWidth * i, 0, imgWidth, imgHeight);

img = (createImage(new File(“images/orc_forward_southwest.png”)));

picsSW = new BufferedImage[frameCount];

for (int i = 0; i < frameCount; i++)

picsSW[i] = img.getSubimage(imgWidth * i, 0, imgWidth, imgHeight);

img = (createImage(new File(“images/orc_forward_northeast.png”)));

picsNE = new BufferedImage[frameCount];

for (int i = 0; i < frameCount; i++)

picsNE[i] = img.getSubimage(imgWidth * i, 0, imgWidth, imgHeight);

img = (createImage(new File(“images/orc_forward_northwest.png”)));

picsNW = new BufferedImage[frameCount];

for (int i = 0; i < frameCount; i++)

picsNW[i] = img.getSubimage(imgWidth * i, 0, imgWidth, imgHeight);

xloc=0;

yloc=imgHeight/2;

// TODO: Change this constructor so that at least eight orc animation pngs are loaded

}

// Read image from file and return

private BufferedImage createImage(File imagefile) {

BufferedImage bufferedImage;

try {

bufferedImage = ImageIO.read(imagefile);

return bufferedImage;

} catch (IOException e) {

e.printStackTrace();

}

return null;

// DONE: Change this method so you can load other orc animation bitmaps

}

}