I made a rudimentary “game” that seems to be only working on some computers. Newer computers run it, older computers give me the “class not found” error when I try to load the applet. Here’s my code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.*;
import java.awt.image.*;
public class VideoGame extends Applet implements Runnable, KeyListener {
Thread animation;
Graphics offscreen;
static final int REFRESH_RATE = 35; // in ms
int width, height;
AudioClip music;
MediaTracker t;
Image image,images[][];
boolean[][] grid;
Layer2 layer2;
Hero hero;
int amtSprites=8;
Sprite[] sprite=new Sprite[amtSprites];
public String debugString;
int spriteNumber;
//INHERITED METHODS
public void init() {
this.addKeyListener(this);
showStatus("Loading Images");
width=bounds().width; height=bounds().height;
setBackground(Color.black); // applet background
image = createImage(width,height); // make offscreen buffer
offscreen = image.getGraphics();
Font timesNew = new Font("TimesRoman",Font.PLAIN,18);
offscreen.setFont(timesNew);
grid=new boolean[width][height];
music = getAudioClip(getDocumentBase(),"Music/Tsz_02.mid");
loadImages();
music.play();
for (int j=0; j<width-1; j++) //set the borders as obstructions
{grid[j][0]=true; grid[j][height-1]=true;}
for (int j=0; j<height-1; j++)
{grid[0][j]=true; grid[width-1][j]=true;}
for (int i=0; i<amtSprites; i++){
sprite[i].updateMap(grid, amtSprites);
sprite[i].updatePosition();}
hero.updateMap(grid, amtSprites);
layer2.setObstruction(grid);
}
public void start() {
showStatus("Starting Game!");
animation = new Thread(this);
if (animation != null) {
animation.start();
}
}
public void update(Graphics g) {// override update so it doesn't erase screen
paint(g);
}
public void paint(Graphics g) {
offscreen.setColor(Color.black);
offscreen.fillRect(0,0,width,height); // clear buffer
updateImages();
layer2.paint();
hero.paint(offscreen);
for (int i=0; i<amtSprites; i++){
sprite[i].paint();
if (sprite[i].messageNumber!=-4)
spriteNumber=i;
}
sprite[spriteNumber].say(" The completed argument to the end method tells whether or not the I/O operation actually completed, that is, whether it had any effect that would be visible to the invoker. In the case of an operation that reads bytes, for example, this argument should be true if, and only if, some bytes were actually transferred into the invoker's target buffer.A concrete channel class must also implement the implCloseChannel method in such a way that if it is invoked while another thread is blocked in a native I/O operation upon the channel then that operation will immediately return, either by throwing an exception or by returning normally. If a thread is interrupted or the channel upon which it is blocked is asynchronously closed then the channel's end method will throw the appropriate exception. ");
g.drawImage(image,0,0,this);
}
public void run() {
while (true) {
repaint();
try {
Thread.sleep (REFRESH_RATE);
} catch (Exception exc) { };
}
}
public void stop() {
showStatus("Game Stopped");
if (animation != null) {
animation.stop();
animation = null;
}
}
//HANDLE IMAGES
public void updateImages(){
for (int i=0; i<amtSprites; i++)
{
if(sprite[i].messageNumber==-4)
sprite[i].update(sprite, i);
sprite[i].updatePosition();
}
hero.update(sprite);
}
public void extractImages(Image strip,Image images[][],int num_images,int croppedWidth,int croppedHeight, int spriteSet)
{
ImageProducer producer;
ImageProducer source = strip.getSource();
int startingY=0;
if (spriteSet>4){
spriteSet=spriteSet%4; startingY=4*croppedHeight;}
int startingX=(spriteSet*(3*croppedWidth))-(3*croppedWidth);
for (int i = 0; i<num_images/4; i++) {
for (int j=0;j<num_images/3; j++){
ImageFilter extractFilter = new CropImageFilter(startingX+(i*croppedWidth),startingY+(j*croppedHeight),croppedWidth,croppedHeight);
producer = new FilteredImageSource(source,extractFilter);
// extract the subimage
images[i][j] = createImage(producer);
t.addImage(images[i][j], 0);
}
}
}
public void loadImages() {
t = new MediaTracker(this);
Image strip;
int num_images=12;
//Load Layer1
strip=getImage(getCodeBase(), "sprites/layer.png");
t.addImage(strip, 0);
layer2=new Layer2(this, strip, offscreen);
//Load Hero
strip = getImage(getCodeBase(),"sprites/Chara1.png");
t.addImage(strip, 0);
// define array of constituent images
images = new Image[num_images/4][num_images/3];
extractImages(strip,images,num_images,24,32, 1);
hero = new Hero(600,400,images,this);
//Load Enemies
//Load NPCs
for (int i=0; i<amtSprites; i++){
strip = getImage(getCodeBase(),"sprites/Chara1.png");
t.addImage(strip, 0);
images = new Image[num_images/4][num_images/3];
extractImages(strip,images,num_images,24,32, (int)(Math.random()*7+1));
sprite[i] = new Sprite(10,300,images,this, offscreen, hero);
Image face=getImage(getCodeBase(), "Faces/hero1.png");
ImageProducer producer;
ImageProducer source = face.getSource();
ImageFilter extractFilter = new CropImageFilter(0,0,47,47);
producer = new FilteredImageSource(source,extractFilter);
// extract the subimage
face = createImage(producer);
t.addImage(face, 0);
sprite[i].face=face;
}
//Wait for all images to finish loading
try {
t.waitForAll();
} catch (InterruptedException e) {}
if (t.isErrorAny()) {
showStatus("Error Loading Images!");
}
else if (t.checkAll()) {
showStatus("Images successfully loaded");
}
}
public int[] spritex=new int[amtSprites];
public int[] spritey=new int[amtSprites];
//HANDLE INPUT
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e)
{
hero.key[e.getKeyCode()]=false;
hero.enableActionKey=true;
}
public void keyPressed(KeyEvent e)
{hero.key[e.getKeyCode()]=true;}
}
for the main game loop. If you need me to list my sprite and hero class code, please say so… I figured this class is my problem since the error is “error finding class VideoGame”.
Any ideas as to what I should change to make this more compatible with other machines? I’ve tried downloading the latest JRE in all instances, and again, it works sometimes and not other times. I hear that bufferedimage causes some compatilibity issue and that you need to get a screen context, do you need to do something similar with images?
Also, I’ve implemented obstructions based on boolean values that are set in layer2 if the region is not transparent. This works perfectly on my computer, but when I save it to a jump drive it doesn’t work on any other computer on which the game DOES work (including my own, actually). Do I need to set bufferedImage up in some way to ensure compatibility? here is my layer2 code for setting up the 2d boolean array for true/false regions (on the true regions, the characters can’t walk):
import java.awt.*;
import java.applet.Applet;
import java.awt.image.BufferedImage;
public class Layer2
{
Applet applet;
BufferedImage bimage;
int width, height;
int alpha;
Graphics g;
Image image;
String debugString;
public Layer2(Applet a, Image i, Graphics g)
{ this.g=g;
this.applet=a;
width=a.getWidth(); height=a.getHeight();
bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image=i;
g = bimage.createGraphics();
g.drawImage(image, 0, 0, applet);
}
public void setObstruction(boolean[][] grid)
{for (int i=0; i<width; i++)
{for (int j=0; j<height; j++)
{alpha = (bimage.getRGB(i,j) >>> 24);
if (alpha==255)
grid[i][j]=true;
}}}
public void paint()
{
g.drawImage(image, 0,0, applet);
}
}
Long post, cootos to whoever has the patience to bear through this one.