Solved

Drawing outside the paint() method

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to install java on RHEL image on EC2 4 41
ejb example issues 3 28
Convert from a json string array to a Java object 3 54
Selenium docs api java index 3 48
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

830 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