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

  • Home
  • SEARCH
  • 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 8047813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:13:22+00:00 2026-06-05T06:13:22+00:00

my goal is to draw some bufferedimage onto another. then all this stuff draw

  • 0

my goal is to draw some bufferedimage onto another. then all this stuff draw onto some other bufferedimage and so on. And finally draw this on top of a panel.
For now i’m trying to draw bufferedimage onto panel and nothing works. My bufferedimage looks completely white:

public class Main2 {
    public static void main(String[] args) {
        JFrame frame = new JFrame("asdf");
        final JPanel panel = (JPanel) frame.getContentPane();
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        panel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                somepaint(panel);
            }
        });
    }

    private static void somepaint(JPanel panel) {
        BufferedImage image = new BufferedImage(200,200,BufferedImage.TYPE_INT_ARGB);
        image.getGraphics().setColor(Color.red);
        image.getGraphics().fillRect(0, 0, 200, 200);

        Graphics2D graphics = (Graphics2D) panel.getGraphics();
        graphics.setColor(Color.magenta);
        graphics.fillRect(0, 0, 500, 500);
        graphics.drawImage(image, null, 0, 0); // draws white square instead of red one
    }
}

thanks

  • 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-05T06:13:24+00:00Added an answer on June 5, 2026 at 6:13 am

    Re:

    private static void somepaint(JPanel panel) {
        BufferedImage image = new BufferedImage(200,200,BufferedImage.TYPE_INT_ARGB);
        image.getGraphics().setColor(Color.red);
        image.getGraphics().fillRect(0, 0, 200, 200);
    
        Graphics2D graphics = (Graphics2D) panel.getGraphics();
    

    This is not how you draw inside of a JPanel or JComponent.

    Don’t call getGraphics() on a component as the Graphics object returned will be short-lived, and anything drawn with it will not persist. Instead do your JPanel’s drawing inside of its paintComponent(Graphics G) method override. You will need to create a class that extends JPanel in order to override paintComponent(...).

    Most importantly, to see how to do Swing graphics correctly, don’t guess. You’ll want to read the Swing Graphics Tutorials first as it will require you to toss out some incorrect assumptions (I know that this is what I had to do to get it right).

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

Sidebar

Related Questions

My goal is to draw a semi-transparent curve. User moves cursor and I draw
The title explains all... I have this snippet of code in my application: String
I've started developing a small app which main goal is to draw a map
I am very new to OpenGL and this is what my goal is... Load
My end goal is to draw a report that looks like MS Word using
My goal is to mark all visible misspelled words in an UITextView. The inefficient
I'm considering MongoDB right now. Just so the goal is clear here is what
Every Christmas we draw names for gift exchanges in my family. This usually involves
In DelphiXE, I'm using a tFileOpenDialog to select a folder and then listing all
Right now my goal is to have anti-aliased text on my Labels. If my

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.