Swing painting problems

E

Evan

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;
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);
}
 
P

Peter Duniho

[...]
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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top