Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Drawing outside the paint() method

Posted on 1998-03-26
2
Medium Priority
?
426 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 40 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

670 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