This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes drawing a simple rectangle in a panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "drawing a simple rectangle in a panel" Watch "drawing a simple rectangle in a panel" New topic
Author

drawing a simple rectangle in a panel

Malcolm White
Greenhorn

Joined: Nov 10, 2002
Posts: 15
Hello everyone,
I've got a class that extends JFrame with 2 panels inside it. I'd like to draw rectangles of varying heights in the right panel, however, everything that I have tried so far has failed. The rectangle is declared globally, and its properties are defined in a method as such:

paintComponet looks like this:

At present, nothing shows up on screen. What I'd like to do is specify the rectangle to paint inside the right panel.
Any pointers would be appreciated.
Thank you all.
[ November 23, 2003: Message edited by: Malcolm White ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Each component is responsible for painting its own self (by default, anyway.) If you were to move the paintComponent() method into the right JPanel (i.e., subclass JPanel and add your method) you'd find that it would work. As it is, when that JPanel is painted, it erases its region of the screen, obliterating your efforts. Alternatively, you could get this to work by again subclassing JPanel so that its paint() method did absolutely nothing -- but it would be fiddly. Easier to do it the right way.


[Jess in Action][AskingGoodQuestions]
Malcolm White
Greenhorn

Joined: Nov 10, 2002
Posts: 15
Thanks a lot Ernest for the amzingly quick response and for steering me in the direction. The program now works perfectly as expected.
Take care.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: drawing a simple rectangle in a panel
 
Similar Threads
dnd
AffineTransform Rectangle2D
Why does the FlowLayoutManager layout more space than needed?
Question about drawimage
mouseOver