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

The Archive Base Latest Questions

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

trying to get an image to print into a window. Everything runs without errors,

  • 0

trying to get an image to print into a window. Everything runs without errors, and it also works if I replace the drawImage with another graphics class. However, the window is missing the image, and i’m not sure why. Again, the JFrame stuff and Graphics work fine with drawing other graphics, but only doesn’t draw the image here. Thanks.

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class GraphicsMovement2 extends JApplet{
    BufferedImage image = null;

    public static void main(String args[]){
        BufferedImage image = null;
        try {
            File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
            ImageInputStream imgInpt = new FileImageInputStream(file);
            image = ImageIO.read(file);
        }
        catch(FileNotFoundException e) {
            System.out.println("x");
        }
        catch(IOException e) {
            System.out.println("y");
        }


        JApplet example = new GraphicsMovement2();
        JFrame frame = new JFrame("Movement");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(example);
        frame.setSize(new Dimension(1366,768));       //Sets the dimensions of panel to appear when run
        frame.setVisible(true);
    }
    public void paint (Graphics page){
    page.drawImage(image, 100, 100, 100, 100, Color.RED, 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-14T10:12:55+00:00Added an answer on June 14, 2026 at 10:12 am

    You’ve defined image twice…

    BufferedImage image = null;
    
    public static void main(String args[]){
        BufferedImage image = null;
    

    This essentially means that by the time you get to the paint method, it is null as you haven’t initialized the instance variable.

    Another problem you will have is the fact that you are trying to load the image from a static reference but the image isn’t declared as static. Better to move this logic into the constructor or instance method.

    Don’t use JApplet as your container when you’re adding to a JFrame, you’re better of using something like JPanel. It will help when it comes to adding things to the container.

    YOU MUST CALL super.paint(g)…in fact, DON’T override the paint method of top level containers like JFrame or JApplet. Use something like JPanel and override the paintComponent method instead. Top level containers aren’t double buffered.

    The paint methods does a lot of important work and it’s just easier to use JComponent#paintComponent … but don’t forget to call super.paintComponent

    UPDATED

    You need to define image within the context it is going to be used.

    Because you declared the image as an instance field of GraphicsMovement2, you will require an instance of GraphicsMovement2 in order to reference it.

    However, in you main method, which is static, you also declared a variable named image.

    The paint method of GraphicsMovement2 can’t see the variable you declared in main, only the instance field (which is null).

    In order to fix the problem, you need to move the loading of the image into the context of a instance of GraphicsMovement2, this can be best achived (in your context), but moving the image loading into the constructor of GraphicsMovement2

    public GraphicsMovement2() {
        try {
            File file = new File("C:\\Users/Jonheel/Google Drive/School/10th Grade/AP Computer Science/Junkbin/MegaLogo.png");
            ImageInputStream imgInpt = new FileImageInputStream(file);
            image = ImageIO.read(file);
        }
        catch(FileNotFoundException e) {
            System.out.println("x");
        }
        catch(IOException e) {
            System.out.println("y");
        }
    }
    

    The two examples below will produce the same result…

    enter image description here

    The Easy Way

    public class TestPaintImage {
    
        public static void main(String[] args) {
            new TestPaintImage();
        }
    
        public TestPaintImage() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new ImagePane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class ImagePane extends JPanel {
    
            public ImagePane() {
                setLayout(new BorderLayout());
                ImageIcon icon = null;
                try {
                    icon = new ImageIcon(ImageIO.read(new File("/path/to/your/image")));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                add(new JLabel(icon));
            }
    
        }
    }
    

    The Hard Way

    public class TestPaintImage {
    
        public static void main(String[] args) {
            new TestPaintImage();
        }
    
        public TestPaintImage() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new ImagePane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class ImagePane extends JPanel {
    
            private BufferedImage background;
    
            public ImagePane() {
                try {
                    background = ImageIO.read(new File("/path/to/your/image"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            @Override
            public Dimension getPreferredSize() {
                return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (background != null) {
                    int x = (getWidth() - background.getWidth()) / 2;
                    int y = (getHeight() - background.getHeight()) / 2;
                    g.drawImage(background, x, y, this);
                }
            }
        }
    }
    

    Take the time to read through the tutorials

    • Creating a GUI With JFC/Swing
    • Performing Custom Painting
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a System.Drawing.Image (generated in a .NET dll) into a picture
Im trying to get an image from a wcf rest service like so: [ServiceContract]
I'm trying to get an image sitting at a URL that is protected by
I'm trying to get an image off the internet from an URL in java.
I'm trying to get an image from URL and save it to the isolated
I am trying to get an image from URL in Android and set it
I am trying to get an image from an HTTP server using Perl. I
I'm trying to get the image.size.h and image.size.w of the UIImage inside of an
I am trying to get an image to display on a page using a
Trying to get my arrow image to stick to the right of the anchor

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.