1

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:

enter image description here 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.

christopher
  • 26,815
  • 5
  • 55
  • 89

1 Answers1

4

"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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Ah Excellent! I simply renamed `paint` to `paintComponent` and called `super.paintComponent` and it's removed the issue entirely. Nice answer, thank you for the links :) +1! – christopher Apr 11 '13 at 17:22
  • Also, the Graphics context is shared during a paint process, so you are likely to have what ever the previous component painted still within the Graphics. One of the jobs paintComponent does is to prepare the component painting – MadProgrammer Apr 11 '13 at 19:01
  • For reference, @MadProgrammer has addressed similar anomalies using the term ["paint chain"](http://stackoverflow.com/search?q=user%3A992484+%22paint+chain%22). – trashgod Apr 11 '13 at 19:51