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 / March 2008

Tip: Looking for answers? Try searching our database.

Swing painting problems

Thread view: 
Evan - 08 Mar 2008 21:23 GMT
So, I'm writing a swing program that displays a graph on a JPanel (I
have created a custom "Canvas" class that extends JPanel) inside a
larger gui.

My Canvas class creates a VertexImage object, adds the VertexImage
object to itself. Then, since it knows about each of the vertices, it
draws the lines between them. The Vertices (class VertexImage extends
JComponent) paint themselves on the Canvas.

So, while I am having no problems painting the connections between the
vertices, I am having PLENTY of problems painting the VertexImages. It
shouldn't be too hard - but I'm obviously messing up somewhere. Here
are are my painting methods.

in my Canvas Class:

public void paintComponent(Graphics g) {

           Component[] components = getComponents();

           // Cycle through each component
           for (int i=0; i < components.length; i++){
               VertexImage vertex = (VertexImage) components[i];
               Point coordinates = vertex.getCoordinates();

               HashSet <Vertex> edges = vertex.getEdges();

               Iterator <Vertex> it = edges.iterator();

                // Draws all the edges to neighbor vertices
               while (it.hasNext()) {
                   Point toCoordinates =
it.next().getInitCoordinates();
                   g.drawLine(coordinates.x, coordinates.y,
                              toCoordinates.x, toCoordinates.y);
               }
           }
       }

In my VertexImage class:

public void paintComponent(Graphics g) {
       g.setColor(Color.blue);

       g.fillOval(coordinates.x,
                  coordinates.y,
                  imageWidth, imageHeight);

       g.drawOval(coordinates.x,
                  coordinates.y,
                  imageWidth, imageHeight);
   }
Peter Duniho - 08 Mar 2008 22:58 GMT
> [...]
> So, while I am having no problems painting the connections between the
> vertices, I am having PLENTY of problems painting the VertexImages. It
> shouldn't be too hard - but I'm obviously messing up somewhere.

Well, without a complete sample it's pretty much impossible to know for  
sure what might be wrong.

However, are you sure that your VertexImage instances have non-zero width  
and height?  If they don't, the paintComponent() method for that class  
might never even be called.

I'm assuming that at least the locations of the instances are correct,  
since you say the lines get drawn correctly.  But I'm curious what layout  
manager you're using to allow that.

You can use a debugger or the console output to trace what methods are  
actually getting called, which should help you at least understand whether  
it's a problem getting called or a problem in how you're actually  
drawing.  Beyond that, if you really expect your question to be answered,  
you should post a concise-but-complete sample of code that reliably  
demonstrates the problem.

Another thing to try is separate your custom JPanel from your custom  
JComponents.  Try adding built-in JComponents to your JPanel sub-class and  
see if those work.  Likewise, try adding your custom JComponent sub-class  
to a plain JPanel and see if that works.  Hopefully at least one or the  
other will, and then you can focus on getting the other to work.  Of  
course, if neither works then you have more than one problem to solve.  :)

Finally, there are a number of things I question in your choice of  
design.  The most significant is your decision to make the vertices  
individual JComponents.  Unless there's really something about them being  
JComponents that you need in your implementation (and being able to draw  
wouldn't qualify for that...you can draw data with or without it being an  
actual JComponent), it seems to me you're better off just drawing the  
vertices in your "Canvas" class.  Otherwise you're unnecessarily limiting  
the ability of your code to scale well as the number of vertices goes up  
and creating additional overhead in any case.

I also wonder why you're apparently drawing every connecting line twice.  
That seems inefficient.  Or are all of the links between vertices one-way?

You also have made the assumption that all of the Components inside your  
Canvas class are VertexImage instances, which seems unnecessarily  
dangerous (you could check the type before casting).  It also seems odd to  
make the paintComponent() method public.  Shouldn't you stick with  
protected, as in the base implementation?

Anyway, the basic issue is that there's nothing fundamentally impossible  
about what you're describing.  And generally speaking, you can add child  
components to a custom JPanel sub-class without any trouble, including  
custom JComponent sub-classes.  So, showing the paint methods isn't really  
going to help.  That doesn't tell us anything about the things that might  
cause problems with getting your custom JComponent sub-class instances to  
show up.

Pete


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



©2008 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.