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