Extremely
Extremely

Reputation: 540

Java swing weird extra pixels in drawRect

I have stumbled on a weird issue with graphics drawRect - it adds two extra pixels at the bottom left and bottom right of the rectangular shape. Is this a VM problem? or something in my understanding is wrong? I am running this on windows 8.1 64bit java SE 1.8.0_25.

EDIT: I have edited the code to do proper override of paintComponent. Also I have noticed that when the window first appears, I do see the content as in the attached screenshot (with the extra pixels), but when I resize it to a smaller window and back (thus forcing the EDT to repaint the whole panel again) - the pixels vanish and do not reappear in subsequent window resizing.
I Even added fillRect to minimize the effect of artifacts (if any), but still, to no avail.

I don't know what to make of it.

EDIT 2: SSCCE modified to run from EDT - still same results

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GuiTest extends JFrame
{
    public GuiTest()
    {
        super( "Test" );

        setContentPane( new MyPanel() );
        setSize( new Dimension( 200, 200 ) );
        setVisible( true );
    }

    public static void main( String[] args )
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {
                new GuiTest();
            }
        } );
    }

    class MyPanel extends JPanel
    {
        @Override
        protected void paintComponent( Graphics g )
        {
            super.paintComponent( g );
            g.setColor( Color.WHITE );
            g.fillRect( 0, 0, getWidth(), getHeight() );
            g.setColor( Color.BLACK );
            g.drawRect( 10, 10, 100, 100 );
        }
    }
}

screenshot

Upvotes: 2

Views: 526

Answers (2)

Paul Morrison
Paul Morrison

Reputation: 1724

I am getting what I think is a related problem, but affecting width only, and only in the case of rectangles which are extremely shallow, but it lops some pixels off the width... Java Swing Graphics drawRect, drawRoundRect, etc. all drop some pixels from the right of the rectangle.

I tried @Extremely 's suggestion re hardware acceleration, and it didn't make any difference.

I have increased the width parameter by 12 (!) in these drawRoundRect and fillRoundRect calls, and it works perfectly! Yuk!

On second thoughts, I think this is a rather different problem - I will raise a separate question. Thanks!

Upvotes: 0

Extremely
Extremely

Reputation: 540

Ha!
I have just noticed that this is a hardware acceleration issue.
If I turn it off by using -Dsun.java2d.d3d=false then everything seems to paint normally - without the extra pixels. I have checked my display adapter (NVIDIA GeForce GTX 850M) driver (as per @Fast Snail suggested) and and it is up-to-date.
This is weird - yet still, with no answer, the origin of the problem is at least clear.

Upvotes: 1

Related Questions