0

I am working on a visual sorting algorithm project and I am drawing them in a JPanel:

public void paint(Graphics g) {
    super.paint(g);
    g.setColor(Color.WHITE);
    for (int i = 0; i < array.length; i++) {
        g.fill3DRect(i * barWidth, height, barWidth, -array[i], true);
    }
    g.dispose();
}

I compiled my program and ran the jar on my Windows 7 computer and I get the bars drawn correctly:

good

I tested this on another Windows 7 computer and a Mac and it drew the bars incorrectly:

bad

Why is this happening? How can I fix this problem?

Community
  • 1
  • 1
  • 2
    As an aside, "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#callbacks). – trashgod Sep 01 '12 at 00:04
  • What is the incorrection? Are the bars gray? Are thay not at same height/width ratio? Filling whole screen with that color? – huseyin tugrul buyukisik Sep 01 '12 at 00:04
  • 1
    Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe. Also consider [tag:jfreechart]. – trashgod Sep 01 '12 at 00:06

1 Answers1

3
g.dispose();

Only call dispose() on graphics instances that your code creates. This one is supplied by the JRE.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433