Drawing on a JPanel

M

Mail987

Hiya, I think this is a fairly common JPanel related question and would
love any help,

I want to store the width,height,x/y coords of a rectangle in an
object, then draw it onto a JPanel. (I can get this far)
I also want to be able to change the width/height/x/y of the rectangle
then have it redrawn. (Im stuck at this point)

Do I have to call some kind of redraw command on the JPanel to have it
updated? Or do I do it on the rectangle itself? Or some other way
perhas?

Pretty stuck, any sample code would be great

Thanks,
Chris
 
A

Andrew Thompson

Hiya, I think this is a fairly common JPanel related question

What did your searches of the group/s reveal
(note that the rel GUI experts hang out at c.l.j.gui.
.... and would love any help, ..

Help searching? What are your current search terms?

Andrew T.
 
K

Knute Johnson

Hiya, I think this is a fairly common JPanel related question and would
love any help,

I want to store the width,height,x/y coords of a rectangle in an
object, then draw it onto a JPanel. (I can get this far)
I also want to be able to change the width/height/x/y of the rectangle
then have it redrawn. (Im stuck at this point)

You need to have some way of getting the parameters. Probably the
simplest is to use JTextFields to input the data.
Do I have to call some kind of redraw command on the JPanel to have it
updated? Or do I do it on the rectangle itself? Or some other way
perhas?

You draw your rectangle in the paintComponent() method. You need to
change the parameters and call repaint() on your JPanel or its parent to
cause your JPanel to be redrawn with the new data.
Pretty stuck, any sample code would be great

Show us the code you wrote to draw your rectangle and we can help you
from that start.
 
B

Ben Kraufmann

You draw your rectangle in the paintComponent() method. You need to
change the parameters and call repaint() on your JPanel or its parent to
cause your JPanel to be redrawn with the new data.

Be careful when using repaint(). You have to make sure that only the
Swing thread calls it, otherwise you can run into serious trouble.
So, call repaint() within an ActionListener (button pressed or
something) or use the SwingUtility-Methods invokeLater() or invokeAndWait().

Ben
 
B

Babu Kalakrishnan

Ben said:
Be careful when using repaint(). You have to make sure that only the
Swing thread calls it, otherwise you can run into serious trouble.
So, call repaint() within an ActionListener (button pressed or
something) or use the SwingUtility-Methods invokeLater() or invokeAndWait().

That's incorrect. A call to repaint() only queues up a request for the
component to be repainted, and swing will make sure that it performs
the actual painting task in the EDT. repaint() is one of the few
methods in Swing that have been documented to be safe to call from a
thread other than the EDT.

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html#exceptions

(Note that the first "exception" mentioned on that page has later been
marked as not being 100% safe) - See the section "Event-Dispatching
Thread" in the article
http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html

BK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top