Simple Animation
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
}
}
Solution
Controller.java
public class Controller implements Runnable{
private Model model;
private View view;
public Controller (Model model, View view) {
this.model = model;
this.view = view;
}
public void updateView(int x, int y, boolean east, boolean south, boolean changeDirection) {
view.repaint(x, y, east, south, changeDirection);
}
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
try {
Thread.sleep(100);
model.move();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Model model = new Model();
View view = new View();
Controller controller = new Controller(model, view);
model.setController(controller);
controller.run();
}
}
Model.java
public class Model {
private Controller controller;
public void setController(Controller controller) {
this.controller = controller;
}
final int xIncr = 8;
final int yIncr = 2;
int xloc;
int yloc;
boolean moveEast;
boolean moveSouth;
boolean changeDirection;
public Model() {
xloc = 0;
yloc = View.imgHeight/2;
moveEast = true;
moveSouth = true;
}
public void move() {
if (xloc < 0) {
moveEast=true;
changeDirection = true;
}
else if (xloc > View.frameWidth – View.imgWidth) {
moveEast=false;
changeDirection = true;
}
if (yloc < 0) {
moveSouth=true;
changeDirection = true;
}
else if (yloc > View.frameHeight – View.imgHeight) {
moveSouth=false;
changeDirection = true;
}
if(moveSouth && !moveEast) //SOUTHWEST
{
xloc -= xIncr;
yloc += yIncr;
changeDirection = false;
}
else if(moveSouth && moveEast) //SOUTHEAST
{
xloc += xIncr;
yloc += yIncr;
changeDirection = false;
}
else if(!moveSouth && moveEast) //NORTHEAST
{
xloc += xIncr;
yloc -= yIncr;
changeDirection = false;
}
else if(!moveSouth && !moveEast) //NORTHWEST
{
xloc -= xIncr;
yloc -= yIncr;
changeDirection = false;
}
controller.updateView(xloc, yloc, moveEast, moveSouth, changeDirection);
}
}
View.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class View extends JFrame{
final static int frameWidth = 500;
final static int frameHeight = 300;
final static int imgWidth = 165;
final static int imgHeight = 165;
final int frameCount = 10;
int currFrame;
private JPanel panel;
private BufferedImage[] picsSE;
private BufferedImage[] picsSW;
private BufferedImage[] picsNE;
private BufferedImage[] picsNW;
public View() {
panel = new JPanel();
panel.setBackground(Color.gray);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(frameWidth, frameHeight);
setVisible(true);
loadAnimation();
}
public void repaint(int x, int y, boolean east, boolean south, boolean changeDirection) {
Graphics g = panel.getGraphics();
currFrame = (currFrame + 1) % frameCount;
if (changeDirection)
currFrame = 0;
if (east && south) {
g.drawImage(picsSE[currFrame], x, y, Color.gray, this);
}
if (!east && south) {
g.drawImage(picsSW[currFrame], x, y, Color.gray, this);
}
if (east && !south) {
g.drawImage(picsNE[currFrame], x, y, Color.gray, this);
}
if (!east && !south) {
g.drawImage(picsNW[currFrame], x, y, Color.gray, this);
}
}
private void loadAnimation() {
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);
// 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
}
}