Solved

Drawing outside the paint() method

Posted on 1998-03-26
2
423 Views
Last Modified: 2012-05-04
I want to do the following:

After a Panel in a Panel is drawn with its Components I want to draw the borders of that inner Panel. I tried to do the following:

public void drawLabeledBounds(Container container, String label)
{
      Graphics gc = container.getGraphics();
      gc.setColor(Color.gray);

      FontMetrics fm = gc.getFontMetrics();

      int length = container.size().width;
      int height =container.size().height;

gc.draw3DRect(.....);             //doesn'twork
//and even this doesn't work:
gc.drawLine(1,1,container.size().width-1,container.size().height-1);
...}// end drawLabeledBounds

How can I realize to draw the bounds of that inner Panel (I want to put a String into the upper edge later!).

Presently it is painted but then taken away (perhaps through the validate() method I have to call because the size of the Components change during initialization).

By the way, all the other Panels (including the outer Panel of the Panel we've talked about!) have a 3D-Border and I realized it by invoking the method responsible for drawing the borders out of the paint() method.
But doing this the same way with the inner Panel it doesn't work!!! Why???
0
Comment
Question by:dirku
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
jmcarney earned 20 total points
ID: 1233802


One of your problems will be that Component objects contained in your Panel are drawing over your border... Another problem is that you are performing drawing outside of the paint() method. This is not a reliable method of drawing objects. It's also unwieldy!

To overcome the first problem, override your Panel's getInsets() method. The second problem is overcome by overriding paint() and invoking your border paint before invoking the ancestral paint() method.

The class skeleton below outlines the kind of structure you need.

class MyPanel
  {

  public Insets getInsets ()
    {
    // Compute border size...
    return new Insets (...) ;
    }

  public void paintBorder (Graphics g)
    {
    // Draw border...
    }

  public void paint (Graphics g)
    {
    paintBorder (g) ;
    // Invoke ancestral paint method to draw Components.
    super.paint (g) ;
    }

  }

Cheers,
John Carney
0
 

Expert Comment

by:jmcarney
ID: 1233803
PS:

overriding getInsets() creates a margin around a Container in which components will not be placed so that they won't draw over your border.

It may also be excluded in the Container's clip region - I can't remember off-hand whether this is so. If you try my solution and it still doesn't work, then it is probably because your border is clipped. To get around this, you need to extend the clip region to cover the entire Panel. The following code should do the trick...

  public void paintBorder (Graphics g)
    {
    Shape oldClip = g.getClip () ; // Save current clip region.

    // Extend clip region to cover entire object.
    g.setClip (0, 0, getSize ().width, getSize.height) ;

    // Paint border...

    g.setClip (oldClip) ; // Restore original clip region.
    }

Cheers,
John Carney

0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android development question 2 109
Java: anonymous class 4 54
Java pass by reference 3 74
How does initial implicit objects  are set up in JSP? 7 40
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question