简体   繁体   中英

Java Swing, all images appear pixelated

Every image in my Java Swing Programs will appear in low quality as shown below: 在此处输入图像描述

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.

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 )

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM