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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:36:37+00:00 2026-06-11T20:36:37+00:00

I have a panel on which I want to draw stuff. Painting on it

  • 0

I have a panel on which I want to draw stuff.
Painting on it when it is beeing created is no problem.

canvas = new Panel() {
    public void paint(Graphics g) {
        g.setColor(Color.WHITE);
        g.drawLine(0, 0, 10, 10);
    }
};

But then I want to draw on it during runtime.
By instinct, I’ve created something like this:

Graphics g = canvas.getGraphics();
g.setColor(Color.GREEN);
g.drawLine(10, 10, 20, 20);
canvas.paint(g);

Sadly, this doesn’t work.
This is probably a simple question but I cannot find a satisfying result by searching.
So how can I do what I want to do?


Sorry for the question above.
I just added the paint code on a button click event and it works.
It just doesn’t work on the windowOpened event of the parent frame.
Any ideas why?

  • 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-11T20:36:38+00:00Added an answer on June 11, 2026 at 8:36 pm

    The problem is that the paint() method can be called at any time whenever the window system (or OS) decides that the particular graphical component needs to be repainted on screen. This may happen at any moment (most often when resizing, moving, switching windows, etc). To see how often it happens just add a log message at the beginning of paint() method. If you paint something on canvas just once it’s very likely that it’s painted, but then another repaint request comes from OS/window system and your green line gets “overdrawn” by object’s paint() .
    So the answer is that any custom painting should be done in paint(). You can add extra attributes to your subclass (eg. boolean drawGreenLine), check it in paint() and take any appropriate action, eg:

    class MyPanel extends JPanel {
    
        boolean drawGreenLine;
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.WHITE);
            g.drawLine(0, 0, 10, 10);
            if (drawGreenLine) {
                g.setColor(Color.GREEN);
                g.drawLine(10, 10, 20, 20);
    
            }  
        }
    };
    

    EDIT: As suggested by @MadProgrammer the example has been changed to override paintComponent(). This way the component is only responsible for drawing itself (and not any children or borders).

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

Sidebar

Related Questions

I have a panel with transparent background which i use to draw an image.
I have a Panel which I want to extend and override MeassureOverride and Arrange
I have a sprite which I draw a white rectangle on (below) and want
I have a form with a panel in which I want to display an
I have an asp.net panel control which I want to appear on every other
I have a frame on which i have created a main panel. On that
I have a custom control which contains a panel, of which I want to
We have a panel which displays a form with an arbitrary number of JTextFields
I have a panel which consists of a toolbar and tabs. On each tab
I have a stack panel for which AllowDrop property is true and I have

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.