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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:26:09+00:00 2026-05-31T17:26:09+00:00

Hi Experts: I have a class which extends JPanel now ShapePanel (Sp) this gets

  • 0

Hi Experts: I have a class which extends JPanel now ShapePanel (Sp) this gets added to a normal JPanel (Container) which inturn gets Added To JFrame (Window). In the paintComponent method i draw a shape…. but the shape never persists after rendering. In the debugger i can see that it gets re-drawn every time i resize or something like that.

Now i overloaded the JPanel’s (Sp’s) paintComponent and added this

    super.paintComponent(g);        

    ShapeDrawerGui SdG = new ShapeDrawerGui((Graphics2D)this.getGraphics());
    //for(Shape s : ArrayOfShapes)
    {
        if(s instanceof Rectangle)
                SdG.Paint((Rectangle)s);
            else if(s instanceof Triangle)
                SdG.Paint((Triangle)s);
            else
                SdG.Paint((Circle)s);
    } //s is a custom-shape object

the graphics object comes from (obviously) the JPanel….

But the drawn image never stays on the panel – you see it for a split second and its gone…

I have done some searching but to no avail.

The actual Drawing

public ShapeDrawerGui(Graphics2D _g)
{
    g = _g;
    g.setColor(Color.Black);
}

@Override
public void Paint(Circle c) 
{
    g.drawArc(0, 0, 50, 50, 0, 360);
}

@Override
public void Paint(Triangle t) 
{        
    Polygon p = new Polygon();
    p.addPoint(0, 25);
    p.addPoint(0 , 50);
    p.addPoint(50, 50);       
    g.drawPolygon(p);
}

@Override
public void Paint(Rectangle r) 
{
    g.drawRect(0, 0, 50, 50);
}

Kind regards

Markus

  • 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-31T17:26:10+00:00Added an answer on May 31, 2026 at 5:26 pm

    ..what is the difference between this.getGraphics and the g ..?

    A GUI should repaint when it is told to do so, and use the Graphics object supplied to the paint(Graphics) or paintComponent(Graphics) method. That method could be called at any time by the GUI, to repaint after being behind another window, for example.

    When you make a call to getGraphics() you are provided with a non-persistent graphics object that will disappear next paint.

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

Sidebar

Related Questions

I have a doctrine class User which is of this hierarchy Doctrine_Record -> BaseUser
This is a simple htaccess question for experts but I have been trying to
Right now, I have a domain entity named StyleBundle. This StyleBundle takes a list
So I've a collection of C++ classes which I now have converting fine with
I have a Silverlight 4 application which talks to a 'manager' class, which talks
I have this class, I m sending this class via RMI and via kryonet
I want extend class which have final constructor (in my case it's SimpleXMLElement), but
I have a class which expects a stream that contains an XML file. I
If I have a javascript class which cannot be instantiated what should the constructor
So I have the following situation. I have a class DataTypes which has the

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.