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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:48:58+00:00 2026-06-06T23:48:58+00:00

I’m making a video game, and I need to add an image to a

  • 0

I’m making a video game, and I need to add an image to a JPanel and then add that JPanel to a JFrame. All of the tutorials I’ve seen say to add it to a JLabel, but the image needs to be able to move around. Unless you can move a JLabel around, I can’t do that. How would I be able to add an Image directly to a JPanel. This is what I have so far, but it is just like it was on a JApplet

class Penguin extends JPanel {

    public Image image;

    public void paintComponent( Graphics g ) {

        image = getImage( getDocumentBase(), "Penguins.jpg" );
        g.drawImage( image, 0, 0, this );
    }
}
  • 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-06T23:48:59+00:00Added an answer on June 6, 2026 at 11:48 pm

    You can easily move a JLabel around, so this is still a viable option, and there are tricks available that can let you do that including using a JLayeredPane, or the glass pane. But having said that, still it is not difficult to paint an image in a JPanel (and I’m betting that your Google search has shown you how to do this). The key is draw it with a Graphics’ object’s drawImage(...) method in the JPanel’s paintComponent(...) method override.

    We can likely give you much better and more specific advice if you desire, if you give us more details about your current code and your current problems.

    Edit:
    Regarding your posted code: first off, do not read in a file within a paintComponent(...) method. This will slow down your GUI’s drawing and make your GUI responsiveness terrible. Also, why have your code re-read in the same image file each time a repaint occurs? No, instead read the file in once and save it in the Image variable. Perhaps you should read it in using ImageIO.read(...).

    Next, you still need to move the file into the jar file as has been recommended to you previously.

    Edit 2:
    For example with code like so:

    Penguin.java

    package myPackage;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.io.IOException;
    import java.io.InputStream;
    
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class Penguin extends JPanel {
       private Image image;
    
       public Penguin(String imageResource) throws IOException {
          InputStream inStream = getClass().getResourceAsStream(imageResource);
          image = ImageIO.read(inStream);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          if (image != null) {
             g.drawImage(image, 0, 0, this);
          }
       }
    }
    

    PenguinApplet.java

    package myPackage;
    
    import java.awt.BorderLayout;
    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class PenguinApplet extends JApplet {
       @Override
       public void init() {
          try {
             SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                   createAndShowGui();
                }
             });
          } catch (InterruptedException e) {
             e.printStackTrace();
          } catch (InvocationTargetException e) {
             e.printStackTrace();
          }
       }
    
       public void createAndShowGui() {
          String penguinImgResource = "images/MyImage.jpg";
          try {
             Penguin penguinPanel = new Penguin(penguinImgResource );
             getContentPane().add(penguinPanel, BorderLayout.CENTER);
          } catch (IOException e) {
             e.printStackTrace();
          }
       }
    }
    

    You could have the images in the images package, off of the package with the class files:

    enter image description here

    The key question here is where are the images stored relative to the class files? In my example the class files are held in the myPackage, and the image is in myPackage/images, and so the relative path is simply images. So any images in the image folder would have to be referenced with this path, here “images/MyImage.jpg”, and obtained as a resource, not a file.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I have a text area in my form which accepts all possible characters from

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.