Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8620881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:41:14+00:00 2026-06-12T06:41:14+00:00

The frame window is launching but the background and the foreground images are not

  • 0

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);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:41:16+00:00Added an answer on June 12, 2026 at 6:41 am

    I’m sorry to say this, but there are simply so many things wrong with your code…

    Lets start with:

    • DON’T override any of the paint methods of a top level container. To start with, none of the top level containers are double buffered.
    • Use light weight components instead of heavy weight components (use JFrame instead of Frame, use JPanel instead of Panel)…
    • Use JPanel as your painting surface instead of the Frame, for the same reasons as stated above…
    • As mentioned, you’re thread code was doing nothing (new Thread().start()).
    • You had no paint code to actually paint the fishies onto the screen (yes your fish had paint code, but nothing was calling it).
    • You random number generator doesn’t work. I got all the fish to swim exactly in the same place…
    • You’re image loading code is wrong. Use ImageIO instead
    • Don’t store the rectangle reference directly. The problem is, if the window is resized, the available bounds for the fish to swim in will change, even if they are contained to within the bounds of the aquarium, it’s possible for the location of the aquarium to have changed.
    • As mentioed, you image paths are incorrect. You never include src in any path. Once deployed, the src path will no longer exist.

    Here is my take…

    Fishies

    Aquarium

    public class Aquarium extends JPanel {
    
        public static final int NUMNBER_OF_FISHIES = 12;
    
        private BufferedImage masterFish;
        private Vector<Fish> fishes = new Vector<Fish>(NUMNBER_OF_FISHIES);
        private int sleepTime = 110;
    
        public Aquarium() {
    
            //set the title and assign tracker object
    
            try {
                masterFish = ImageIO.read(getClass().getResource("/gnome_panel_fish.png"));
            } catch (IOException iOException) {
                iOException.printStackTrace();
            }
    
            // This is a little cheat which means you only ever need one fish image
            // Basically it will flip the master along it's horizontal axies ;)
            BufferedImage flippedMaster = new BufferedImage(masterFish.getWidth(), masterFish.getHeight(), masterFish.getType());
            Graphics2D g2d = flippedMaster.createGraphics();
            g2d.setTransform(AffineTransform.getScaleInstance(-1, 1));
            g2d.drawImage(masterFish, -masterFish.getWidth(), 0, this);
            g2d.dispose();
    
            for (int index = 0; index < NUMNBER_OF_FISHIES; index++) {
                fishes.add(new Fish(masterFish, flippedMaster, this));
            }
    
            Thread background = new Thread(new Background());
            background.setDaemon(true);
            background.start();
    
        }
    
        public static void main(String ar[]) {
    
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
    
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException classNotFoundException) {
                    } catch (InstantiationException instantiationException) {
                    } catch (IllegalAccessException illegalAccessException) {
                    } catch (UnsupportedLookAndFeelException unsupportedLookAndFeelException) {
                    }
    
                    JFrame frame = new JFrame("The Aquarium ");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new Aquarium());
                    frame.setSize(400, 400);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                }
            });
        }
    
        @Override
        protected void paintComponent(Graphics g) {
    
            super.paintComponent(g);
    
            for (Fish fish : fishes) {
                fish.drawFishImage(g);
            }
    
        }
    
        protected class Background implements Runnable {
    
            @Override
            public void run() {
    
                while (true) {
    
                    for (Fish fish : fishes) {
                        fish.swim();
                    }
    
                    try {
                        Thread.sleep(sleepTime);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    repaint();
                }
            }
        }
    }
    

    Fish

    public class Fish {
    
        private Component tank;
        private Image image1, image2;
        private Point location, velocity;
        private Random random;
    
        public Fish(Image image1, Image image2, Component tank) {
            random = new Random();
            this.tank = tank;
            this.image1 = image1;
            this.image2 = image2;
            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);
            System.out.println(location);
        }
    
        public void swim() {
    
            Rectangle edges = tank.getBounds();
    
            //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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not asking about the Type of the current LayoutAwarePage Type type = ((Frame)Window.Current.Content).CurrentSourcePageType;
I'd like to change the window/frame size of my XEmacs based on the current
I need to animate a view frame size adding 100px of height, but I
I need to find window position and size, but I cannot figure out how.
My CMDIFrameWndEx derived main frame window uses a CMFCRibbonStatusBar to which I add a
In java, message dialogs open in middle of the Frame window by default in
import wx class bucky(wx.Frame): # Creating the outer window/frame def __init__(self,parent,id): wx.Frame.__init__(self,parent,id,'Karls Network Tool',
I am using an Ext.window.Window and i don't wanna the frame in my window.
(WindowClosing) when i am using window event for closing current frame, in that it
I have a window(say main window) with a frame which has a page in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.