The frame window is launching but the background and the foreground images are not loading and the window frame size is also very very small. Please help me to fix the error.
here is the code posted
Aquarium.java
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.Vector;
public class Aquarium extends Frame implements Runnable
{
Image aquariumImage,memoryImage;
BufferedImage bImage;
Graphics memoryGraphics;
Image [] fishImages=new Image[2];
MediaTracker tracker;
int numberFishes=12;
Vector<Fish> fishes=new Vector<Fish>();
Thread thread;
boolean runOk=true;
int sleepTime=110;
Fish fish;
Aquarium()
{
//set the title and assign tracker object
setTitle("The Aquarium ");
tracker=new MediaTracker(this);
//add images to the tracker object to trace it
fishImages[0]=Toolkit.getDefaultToolkit().getImage("src\fish1.gif");
tracker.addImage(fishImages[0], 0);
System.out.println("fish 1 size "+Toolkit.getDefaultToolkit().getImage("src\fish1.gif").getWidth(null));
fishImages[1]=Toolkit.getDefaultToolkit().getImage("src\fish2.gif");
tracker.addImage(fishImages[1], 0);
aquariumImage =Toolkit.getDefaultToolkit().getImage("src\bubbles.gif");
tracker.addImage(aquariumImage,0);
setResizable(true);
setVisible(true);
//assign memory to the graphics and anotherImage object
int dx=getSize().width;
int dy=getSize().height;
System.out.println("x value is "+dx+ " dy value is "+ dy);
bImage=new BufferedImage(dx, dy,BufferedImage.TYPE_INT_ARGB);
try
{
memoryGraphics=bImage.getGraphics();
}
catch(Exception exc)
{
System.out.println(exc.getCause());
System.out.println(exc.getStackTrace());
}
//create a new thread and start the thread
thread=new Thread();
thread.start();
try
{
tracker.waitForID(0);
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
setSize(aquariumImage.getWidth(this),aquariumImage.getHeight(this));
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we)
{
System.exit(0);
runOk=false;
}
});
}
public static void main(String ar[])
{
new Aquarium();
}
@Override
public void run()
{
//draw 4 edges of rectangle
Rectangle edges=new Rectangle(0+getInsets().left,0+getInsets().top,
getSize().width-(getInsets().left+getInsets().right),
getSize().height-(getInsets().top+getInsets().bottom));
//add fishes to the fishes vector
for (int loopIndex=0; loopIndex<numberFishes;loopIndex++)
{
fishes.add(new Fish(fishImages[0],fishImages[1],edges,this));
try
{
Thread.sleep(20);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
while(runOk)
{
for(int loopIndex=0; loopIndex < numberFishes; loopIndex++)
{
fish=(Fish)fishes.elementAt(loopIndex);
fish.swim();
}
try
{
Thread.sleep(sleepTime);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
repaint();
}
}
public void update(Graphics g)
{
memoryGraphics.drawImage(aquariumImage, 0, 0, this);
for(int loopIndex=0;loopIndex<numberFishes;loopIndex++)
{
((Fish)fishes.elementAt(loopIndex)).drawFishImage(memoryGraphics);
}
g.drawImage(bImage, 0, 0, this);
}
}
fish.java
import java.awt.*;
import java.util.Random;
public class Fish {
Component tank;
Image image1,image2;
Point location,velocity;
Rectangle edges;
Random random;
public Fish(Image image1,Image image2, Rectangle edges,Component tank) {
random = new Random(System.currentTimeMillis());
this.tank=tank;
this.image1=image1;
this.image2=image2;
this.edges=edges;
this.location=new Point(100+ (Math.abs(random.nextInt())%300),
100+Math.abs(random.nextInt())%100));
this.velocity=new Point(random.nextInt()%8,random.nextInt()%8);
}
public void swim() {
//determine the optimum velocity to make the fish swim
if(random.nextInt()%7<=1) {
velocity.x += random.nextInt() % 4 ;
velocity.x = Math.min(velocity.x, 8);
velocity.x = Math.max(velocity.x, -8);
velocity.y += random.nextInt() % 4;
velocity.y = Math.min(velocity.y, 8);
velocity.y = Math.max(velocity.y, -8);
}
//add the velocity to the location of the fish to make it move
location.x += velocity.x;
location.y += velocity.y;
if(location.x < edges.x) {
location.x=edges.x;
velocity.x = -velocity.x;
}
if((location.x + image1.getWidth(tank)) > (edges.x + edges.width)) {
location.x = edges.x + edges.width - image1.getWidth(tank);
velocity.x = -velocity.x;
}
if(location.y < edges.y) {
location.y = edges.y;
velocity.y = -velocity.y;
}
if((location.y + image1.getHeight(tank)) > (edges.y + edges.height)) {
location.y = edges.y + edges.height - image1.getHeight(tank);
velocity.y = -velocity.y;
}
}
public void drawFishImage(Graphics g) {
if(velocity.x < 0) {
g.drawImage(image1,location.x,location.y,tank);
}
else {
g.drawImage(image2, location.x, location.y, tank);
}
}
}