This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths 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 Head First Android 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

drawing a simple rectangle in a panel

Malcolm White

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

Joined: Jul 08, 2003
Posts: 24189

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

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.
I agree. Here's the link:
subject: drawing a simple rectangle in a panel
jQuery in Action, 3rd edition