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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:02:35+00:00 2026-05-27T20:02:35+00:00

I have been searching for a reason for this behavior in my code for

  • 0

I have been searching for a reason for this behavior in my code for quite some time now. I do not want to dive too deeply into the Swing API to figure why this occurs. I would appreciate any information regarding what causes this problem.

This is a simplified version of the application I am writing, the problems seems to be when I click draw the first time, the image will not paint onto the panel, but when I click it the second time, it would paint the image perfectly. Any drawing done after will work correctly, but the initial paint problem is annoying me greatly. Thanks for any help! 🙂

public class ImageTester {

public static void main(String[] args) {
    final JFrame window = new JFrame("Image Tester");
    final JPanel pane = new JPanel();
    JButton draw = new JButton("Draw");
    JButton paint = new JButton("Paint");

    Toolkit k = Toolkit.getDefaultToolkit();
    final Image i = k.createImage("tester.jpg");
    //pane.getGraphics().drawImage(i, 22, 15, null);

    draw.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            System.out.println(pane.getGraphics());
            pane.getGraphics().drawRect(5, 5, 15, 15);
            pane.getGraphics().drawImage(i, 15, 15, null);
            System.out.println("Performance");
        }
    });

    paint.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

        }
    });

    pane.add(draw);
    pane.add(paint);
    window.add(pane);
    window.setVisible(true);
    window.setSize(new Dimension(400, 400));
    window.setLocationRelativeTo(null);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
  • 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-27T20:02:36+00:00Added an answer on May 27, 2026 at 8:02 pm

    Besides the advice of camickr..

    Images load asynchronously using Toolkit.createImage(). Either use ImageIO.read(URL/File/InputStream) or add a MediaTracker.


    E.G.

    On first run I see.

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.net.URL;
    import javax.imageio.ImageIO;
    
    public class ImageTester {
    
    public static void main(String[] args) throws Exception {
        final JFrame window = new JFrame("Image Tester");
        JButton draw = new JButton("Draw");
        JButton paint = new JButton("Paint");
    
        final Image i = ImageIO.read(new URL(
            "http://pscode.org/media/citymorn2.jpg"));
    
        ImagePanel gui = new ImagePanel();
        gui.setImage(i);
        draw.addActionListener(new ActionListener() {
    
            public void actionPerformed(ActionEvent arg0) {
            }
        });
    
        paint.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
    
        gui.add(draw);
        gui.add(paint);
        window.add(gui);
        window.setVisible(true);
        window.setSize(new Dimension(400, 400));
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
    
    class ImagePanel extends JPanel {
    
        Image i;
    
        public void setImage(Image image) {
            i = image;
        }
    
        public void paintComponent(Graphics g) {
            //System.out.println(pane.getGraphics());
            super.paintComponent(g);
            g.drawRect(5, 5, 15, 15);
            g.drawImage(i, 15, 15, null);
            System.out.println("Performance");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been having trouble searching for this answer because I am not quite sure
I've been searching for some time now in here and other places and can't
I have been searching for info on this to no avail. The context of
I have been searching for a way to insert linebreaks in my code for
I have been searching forever. Sorry, I am pretty desperate at this point so
I have been searching everywhere but I could not find an answer. I need
So I have been searching everywhere for a solution to this problem. I have
I have been searching through here and google for a few days now, trying
I have been searching for the answer and have not been able to find
Ok, I've been searching for two days and have not found anything. I think

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.