• Post Reply Bookmark Topic Watch Topic
  • New Topic

using graphics class to paint to different JPanels  RSS feed

 
Christian Halme
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Iam making a program that works like a clock or timer.
The gui is on one JPanel and the analog clock on another.
My class handles three JPanels
the layout is similar to the below



I dont know how to choose which JPanel
to choose when using paintComponent method,
i want to paint to graphics JPanel.

please can someone help
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Christian

for all your panels that need some painting action, override the paintGraphics() method.

And when your timer fires (if any), then in the actionListener you do:


Greetings,
Piet
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:override the paintGraphics() method.

The paintComponent(...) method.

More here: Performing Custom Painting
 
Christian Halme
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everybody, its working now.
I have a new problem.
since there's no command pack for JPanel, how do
I size JPanel so it fits my painting from Graphics method.
Is there some similar command to as pack() for JFrame I could use.
I tried with

without any success.
My class for JPanel looks like this
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do I size JPanel so it fits my painting from Graphics method.


Did you read the tutorial link and look at the examples? It shows you how to override the getPreferredSize() method to return an appropriate value.

In your case I guess it would be (150, 180) since that is the size of the line you are drawing.

I also don't know what the getGraphics() method is for. Get rid of it. Custom painting is done in the paintComponent() method or a method you invoke from there.
 
Christian Halme
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, now I read a little more I understand how it works
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!