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 6926279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:53:51+00:00 2026-05-27T10:53:51+00:00

Here is the setup: I have a class called Sprite that contains an image

  • 0

Here is the setup:

I have a class called Sprite that contains an image for a subclass called Animal which is part of an array of animals in the World class. Using the Canvas class that does all of the graphics, how can I get to the image?

Survival.class – Main class

package survival;
import javax.swing.*;

public class Survival {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Survival");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1000, 750);

        Canvas g = new Canvas();
        frame.add(g);

        frame.setVisible(true);

        World environment = new World(10, 25, 1000, 750);
        environment.step();
    }
}

Canvas.class – Handles all drawing

package survival;
import java.awt.*;
import javax.swing.*;

public class Canvas extends JPanel {
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2D = (Graphics2D) g;
    }
}

World.class – Creates animals

package survival;

public class World {
    private int width = 1000;
    private int height = 750;

    private Animal[] animals;
    private Plant[] plants;

    public World(int animalNumber, int plantNumber, int width, int height) {
        this.width = width;
        this.height = height;

        animals = new Animal[animalNumber];
        for (int i = 0; i < animalNumber; i++) {
            animals[i] = new Animal(0,0);
        }

        plants = new Plant[plantNumber];
        for (int i = 0; i < plantNumber; i++) {
            plants[i] = new Plant(0,0);
        }
    }

    public void step() {
        for (int i = 0; i < plants.length; i++) {
            plants[i].move();
        }
        for (int i = 0; i < animals.length; i++) {
            animals[i].move();
        }
    }    

    public int getWidth() {
        return width;
    }
    public int getHeight() {
        return height;
    }
}

Sprite.class – Superclass of animal, contains the image!

package survival;

public class Sprite {
    private Image; //IMAGE I NEED!!!
    private double x;
    private double y;

    public Sprite(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void move() {
        x += 1;
        y += 1;
    }

    public void setX(int x) {
        this.x = x;
    }
    public void setY(int y) {
        this.y = y;
    }
}

Animal.class – Subclass of animal

package survival;

public class Animal extends Sprite {
    public Animal(int x, int y) {
        super(x, y);
    }
}
  • 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-05-27T10:53:51+00:00Added an answer on May 27, 2026 at 10:53 am

    http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html

    or

    http://www.rgagnon.com/javadetails/java-0240.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom class in Obj-C called RouteManager which contains an array of
We have a setup here that is less than ideal, We have our webserver
I have a weird issue. Here is the setup: I have a NSOperationQueue which
Here's the setup. I have web site which is essentially a simple CMS. Here
Here is my setup. In my application delegate, I have a property called currentFoo.
I have an ORM class called Person, which wraps around a person table: After
I have a class that is getting called and for some reason one of
I have a library with a class called Recipient which has it's own fluent
Here's the setup. I have a C++ program which calls several functions, all of
Here's the setup I have in a vs2008 solution: Data layer in a project

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.