0

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
superflash
  • 105
  • 1
  • 10
  • If the same program works properly on other machines, the usual guess/hint is to update the graphics card driver on the machine where you encounter the problem. (Really a guess, only...) – Marco13 May 28 '18 at 12:52

3 Answers3

1

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)

Mateusz Stefaniak
  • 856
  • 1
  • 9
  • 20
  • I applied the change to the JFreeChart and it made it worse. I don't think I can or should fix this through code because I don't want necessary code in my app that would fix problems that only my machine has. Thank you very much for the reply. – superflash May 21 '18 at 22:44
1

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

superflash
  • 105
  • 1
  • 10
0

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);
MbPCM
  • 457
  • 5
  • 12