Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing Rectangle in Java Shows Pixel Anomaly

Tags:

java

swing

I have a very simple java program that draws a rectangle but when I closely examine the rendered shape, I see two extra pixels that shouldn't be there ...

enter image description here You can see one extra pixel at below left and one at below right.

I am using Windows 7 Professional 64-BIT using JDK 1.8.0. Here is the program ...

    import java.awt.Graphics;
    import java.io.IOException;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class JavaBug {

    public JavaBug() throws IOException {
        JFrame frame = new JFrame();        
        frame.add( new JPanel() {
        private static final long serialVersionUID = 1L;

                public void paintComponent( Graphics g ) {
                    super.paintComponent(g);
                    g.drawRect(50, 50, 20, 20); 
                }
            });

            frame.setSize(400, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible( true );
        }

        public static void main(String [] args) throws IOException {
            new JavaBug();
        }
    }
like image 564
Constantin Avatar asked Feb 27 '14 04:02

Constantin


2 Answers

For sake of an answer in case anyone comes across this question

The problem seemed to be with the Java 8 pre-release version. This code works fine with Java 7.

Note: This conclusion was taken from the comments section and I did not contribute to the answer. :-)

like image 186
dARKpRINCE Avatar answered Sep 19 '22 00:09

dARKpRINCE


Confirmed. I've tested. It's a Java 8 Bug.

like image 32
Carlos.Cândido Avatar answered Sep 22 '22 00:09

Carlos.Cândido