Solved

Drawing outside the paint() method

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 69
java jdbc batch example 8 34
type mismatch (Object[] to double[] 4 24
Java Restore security prompts not working 10 13
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:

778 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