Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Drawing on a JPanel

Thread view: 
Mail987@gmail.com - 25 Aug 2006 01:43 GMT
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
Andrew Thompson - 25 Aug 2006 02:32 GMT
> 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.
Knute Johnson - 25 Aug 2006 05:37 GMT
> Hiya, I think this is a fairly common JPanel related question and would
> love any help,
[quoted text clipped - 3 lines]
> 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.

Signature

Knute Johnson
email s/nospam/knute/

Ben Kraufmann - 25 Aug 2006 11:13 GMT
>> 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
[quoted text clipped - 3 lines]
> 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
Babu Kalakrishnan - 25 Aug 2006 12:03 GMT
> >> 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
[quoted text clipped - 8 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.