Reputation: 27336
Brief description of Program
Hi guys. I got bored this morning and decided to write a graphing program. Eventually i'll be able to run things like Dijksta's Algorithm on this software.
When anything changes on screen, a call to the repaint
method of the JPanel
where everything is painted to is made. This is the JPanel
paint method:
public void paint(Graphics g)
{
for(Node node : graph.getNodes()){
node.paint(g);
}
for(Link link : graph.getLinks()){
link.paint(g);
}
}
It simply cycles through each element in the lists, and paints them.
The paint method for the node class is:
public void paint(Graphics g)
{
g.setColor(color);
g.drawOval(location.x, location.y, 50, 50);
g.setColor(Color.BLACK);
g.drawString(name, location.x + 20, location.y + 20);
}
And for the link it is:
public void paint(Graphics g)
{
Point p1 = node1.getLocation();
Point p2 = node2.getLocation();
// Grab the two nodes from the link.
g.drawLine(p1.x + 20, p1.y + 20, p2.x + 20, p2.y + 20);
// Draw the line between them.
int midPointX = ((p1.x + p2.x) / 2) + (100 / (p2.x - p1.x));
int midPointY = ((p1.y + p2.y) / 2) + 30;
// Compute the mid point of the line and get it closer to the line.
g.setColor(Color.BLACK);
g.drawString(String.valueOf(weight), midPointX, midPointY);
}
The Problem
The issue I am having arises when I use the JOptionPane
class. When I select the option to add a new node, and select where to place it, an inputDialog
pops up, asking for the node's name.
The nodes are added fine, for this behaviour occurs:
Is this a common problem; an issue with paint
or repaint
perhaps?
Nonetheless, here is the code that calls the inputDialog
:
Function addNode = functionFac.getInstance(state);
String name = "";
while(!name.matches("[A-Za-z]+")) {
name = JOptionPane.showInputDialog("Please enter the name of the node.", null);
}
addNode.execute(stage, new NodeMessage(arg0.getPoint(), name));
PS: Function is an interface type that I have written.
Upvotes: 1
Views: 53
Reputation: 205775
"Swing programs should override paintComponent()
instead of overriding paint()
."—Painting in AWT and Swing: The Paint Methods.
"If you do not honor the opaque property you will likely see visual artifacts."—JComponent
See also this Q&A that examines a related issue.
Upvotes: 4