Solved

Drawing outside the paint() method

Posted on 1998-03-26
2
417 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now