Computer Science Canada

need help with my final project.

Author:  Notthebbq [ Wed Dec 14, 2011 1:51 pm ]
Post subject:  need help with my final project.

i have to animate a sprite for my final project and i can't figure out how to load the pictures into my code. i know i have to use string utilities but i don't know what to do with it. so im stuck at this dead end wall.
can anyone please help me figure out what im doing wrong.

my code for my animated sprite class
package SpritePackage;
import java.util.Random;
import java.awt.Point;

import javax.swing.JFrame;


public class AnimatedSprite extends Sprite {
int numimages;
int imagedisp;
int vx;
int vy;

//random object

public AnimatedSprite(String newImageName, Point topLeft, int width, int height, JFrame jFrame, int numberofimages) {
super(newImageName, topLeft, width, height, jFrame);
Random random = new Random();
vx = (random.nextInt(10)+1);
vy = (random.nextInt(10)+1);
numimages = numberofimages;
numimages = random.nextInt(10);


}
public void update() {

location.x += vx;
location.y += vy;
numimages++;
if (numimages >= 9) {
numimages = 0;
Stringutil str = new Stringutil();
str.Nextimagename = "IMG00000.jpg";

}
}


}

my string utilities class
package SpritePackage;



public class Stringutil {


public static String Nextimagename(String Oldimagename, int imagenum) {
int dimage = Oldimagename.lastIndexOf('.');
return Oldimagename.substring(0, dimage - 1) + imagenum + Oldimagename.substring(dimage);
}
}

my sprite class
package SpritePackage;

import javax.swing.JFrame;
import java.awt.*;

public class Sprite {
//top left corner of sprite when it's drawn
protected Point location;
//size of frame
private int frameWidth;
private int frameHeight;
//image to show in sprite
private Image image;
private String imageName;
private int imageWidth;
private int imageHeight;

public Point getLocation() {
return location;
}

public void setLocation(Point value) {
if (value.x < 0) {
location.x = 0;
} else if (value.x > frameWidth - imageWidth) {
location.x = frameWidth - imageWidth;
} else {
location.x = value.x;
}
//30 allows for frame's title bar
if (value.y < 30) {
location.y = 30;
} else if (value.y > frameHeight - imageHeight) {
location.y = frameHeight - imageHeight;
} else {
location.y = value.y;
}
}

public int getX() {
return location.x;
}

public void setX(int value) {
if (value < 0) {
location.x = 0;
} else if (value > frameWidth - imageWidth) {
location.x = frameWidth - imageWidth;
} else {
location.x = value;
}
}

public int getY() {
return location.y;
}

public void setY(int value) {
//30 allows for frame's title bar
if (value < 30) {
location.y = 30;
} else if (value > frameHeight - imageHeight) {
location.y = frameHeight - imageHeight;
} else {
location.y = value;
}
}

public int getRight() {
return location.x + imageWidth;
}

public int getBottom() {
return location.y + imageHeight;
}

public int getFrameWidth() {
return frameWidth;
}

public void setFrameWidth(int value) {
frameWidth = value;
}

public int getFrameHeight() {
return frameHeight;
}

public void setFrameHeight(int value) {
frameHeight = value;
}

public Image getImage() {
return image;
}

public void setImage(String imageName) {
if (imageName != null) {
image = Toolkit.getDefaultToolkit().getImage(imageName);
}
}

public String getImageName() {
return imageName;
}

public void setImageName(String imageName) {
this.imageName = imageName;
}

public int getImageWidth() {
return imageWidth;
}

public void setImageWidth(int imageWidth) {
this.imageWidth = imageWidth;
}

public int getImageHeight() {
return imageHeight;
}

public void setImageHeight(int imageHeight) {
this.imageHeight = imageHeight;
}

public Sprite(String newImageName, Point topLeft, int width, int height, JFrame jFrame) {
//non-animated sprite
image = Toolkit.getDefaultToolkit().getImage(newImageName);
imageName = newImageName;
setFrameWidth(jFrame.getWidth());
setFrameHeight(jFrame.getHeight());
//these must be called AFTER frameWidth and height
//are set since these use those values
location = new Point(topLeft);
imageWidth = width;
imageHeight = height;
}

public void draw(JFrame frame, Graphics g) {
g.drawImage(image, location.x, location.y, imageWidth, imageHeight, frame);
}

public Rectangle getBoundingRectangle() {
return new Rectangle(location.x, location.y, imageWidth, imageHeight);
}
}

and the tester class
import java.awt.*;
import javax.swing.JFrame;

import SpritePackage.Sprite;

public class SpriteTester extends JFrame{

Sprite sprite;
// sprite = AnimatedSprite;

public static void main(String[] args) {
SpriteTester spriteTester = new SpriteTester();
}

public SpriteTester() {
//some of the settings for jFrame
setTitle("Sprite Tester");
setLocationByPlatform(true);
setLayout(null);
setBounds(0, 25, 230, 300);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
//Initialise sprite by calling its constructor
sprite = new Sprite("IMG00000.jpg", new Point(50, 50), 125, 162, this);
//final settings for jFrame
//do these AFTER initializing sprites or paint will occasionally crash
//with nullPointerException because sprites aren't initialized
//yet and therefore can't have methods called on them yet
setVisible(true);
//use double buffering to stop flickering of images,
//2 means 2 buffers are used
createBufferStrategy(2);
}

public void paint(Graphics g) {
super.paint(g);
sprite.draw(this, g);
}
}


: