0
votes

I'm using OpenJDK 11:

openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)

Trying to make an app drawing something in JPanel I noticed some annoying inaccuracies.

The following piece of code is supposed to draw a series of lines one below the other (swapping colors are to demonstrate what's happening):

class TestPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    setBackground(Color.WHITE);

    g.setColor(Color.RED);
    g.drawLine(301, 1, 350, 1);
    g.setColor(Color.BLUE);
    g.drawLine(301, 2, 350, 2);
    g.setColor(Color.GREEN);
    g.drawLine(301, 3, 350, 3);
    g.setColor(Color.RED);
    g.drawLine(301, 4, 350, 4);
    g.setColor(Color.BLUE);
    g.drawLine(301, 5, 350, 5);
    g.setColor(Color.GREEN);
    g.drawLine(301, 6, 350, 6);
    g.setColor(Color.RED);
    g.drawLine(301, 7, 350, 7);
    g.setColor(Color.BLUE);
    g.drawLine(301, 8, 350, 8);
    g.setColor(Color.GREEN);
    g.drawLine(301, 9, 350, 9);
    g.setColor(Color.RED);
    g.drawLine(301, 10, 350, 10);
}

}

However instead of getting lines drawn one below the other there are gaps between some lines: Lines drawn with gaps

I suppose this might be caused by something like transformation between a virtual and physical coordinate system.

However how to make it pixel-perfect when preciseness really matters?

1
Does that image represent the code you've posted? Is this the actual code you're using? The image contains 12 lines but I'd say that if there were precision issues you'd still have 10 (or maybe 11) lines with 1 or 2 colored lines missing. Another question: did you try that with other JDKs/versions? Do you get different results?Thomas
As for pixel-perfection: you could try to build a rasterized image (set the pixel values yourself or draw to it) and draw that image to the panel.Thomas
@Thomas: Yes, this is an actual and working code. And yes, this is an issue that these gaps produce extra lines. Simply some coordinates seem to be rounded (ex. requested (1,5) is translated to (1,6)). I didn't try it with other Java versions yet.Michał B.
I tried it on Oracle JDK 8 and it works perfectly... It could be a bug with OpenJDK.BackSlash

1 Answers

1
votes

I tested it on several jdk releases. Results are: OpenJDK Runtime Environment (build 1.8.0_40-b25): Works correctly
OpenJDK Runtime Environment (build 9+181): Wrong behavior
OpenJDK Runtime Environment 18.3 (build 10.0.2+13): Wrong behavior
OpenJDK Runtime Environment 18.9 (build 11.0.1+13): Wrong behavior

Thank you all for your time and help.