superflash
superflash

Reputation: 125

Java Swing, all images appear pixelated

Every image in my Java Swing Programs will appear in low quality as shown below: enter image description here

As you can see from the image, it is not just images I am having problem with, it's graphics in general displaying like this. In the right portion you can see the problem reflected to a JFreeChart Graphic. Has anyone else had this problem, is it related to the graphics card or maybe windows DPI? The same programs will render perfectly fine in other machines so I am pretty sure it is not related to the code.

Upvotes: 0

Views: 2147

Answers (3)

MbPCM
MbPCM

Reputation: 497

even if you are not using g2d, just add g2d instance to make it work. (i had similar problem, i tried JVM options, nothing worked. and then this worked even though i draw image using just graphics(not graphics2D). isn't it weired(notice last line of code which is using g instead of g2) its in paintComponent(graphics g) method.

Graphics2D g2 = (Graphics2D) g;
            //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
           g.drawImage(scaled, x, y, null);

Upvotes: 0

superflash
superflash

Reputation: 125

The problem in my case was that I had scaling turned on at 125% on Windows and images and frames had to be zoomed in order to fit the size they were given. All you have to do is go to Display Settings and at section Scale and layout set zoom to 100%.

If you don't fancy turning down scaling you could add this parameter to the VM on your IDE by adding this parameter:

-Dsun.java2d.dpiaware=false

Alternatively, try this if the first one doesn't work.

-Dsun.java2d.uiScale=1.0

This only works for Swing

Upvotes: 3

Mateusz Stefaniak
Mateusz Stefaniak

Reputation: 905

If you are using g2d, try enabling antialiasing.

g2d.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);

(more info: Java2D Graphics anti-aliased)

In JFreeChart try the following code:

chart.setRenderingHints(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON));

(source: http://www.jfree.org/forum/viewtopic.php?t=7958)

Upvotes: 1

Related Questions