Does Java have any known issues with Images and displaying them

懵懂的女人 提交于 2019-12-12 05:27:58

问题


I am new to Java but not new to graphics I'm trying to expand my understanding with a project in which I'm displaying many photos of items using JLabels and Icons to display them,

The items pictures are garnished from various locations on the net and so many sources.

I've converted these into PNG files as they have transparent backgrounds to them.

Thing is I wasted a whole weekend recently checking and re-checking some code because one item just would not display, following the logic to its conclusion told me it was the file that was at fault, even though the file displayed correctly in photoshop?

I went into the file to see what was unique about it, but apart from being 8 bit instead of 32 bit there was no difference I could find.

...and so to my question, does Java have problems with displaying any particular types of file formats that perhaps I should know prior to actually finding out the hard way.

Sorry for no code as the question does not warrant it.

I think the file that caused me problems was pulled from here http://upload.wikimedia.org/wikipedia/en/d/d5/XXXX_Logo.png


回答1:


As far as I understand it, Java should be able to handle PNG just fine. There are some formats that Java doesn't support including vector image formats such as SVG. But for most of these, there are outside libraries that can help.

With regards to the image example that you've linked to,

, I have no problem getting Java to read and display it:
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Foo {

   public static void main(String[] args) {
      String spec = "http://upload.wikimedia.org/wikipedia/"
            + "en/d/d5/XXXX_Logo.png";
      URL url;
      try {
         url = new URL(spec);
         BufferedImage img = ImageIO.read(url);
         ImageIcon icon = new ImageIcon(img);
         JOptionPane.showMessageDialog(null, icon);
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }     
}

Regarding your statement,

Sorry for no code as the question does not warrant it.

I beg to differ. I think that showing code and a reference to the offending image that causes your problem is key towards solving it, and for example if my answer doesn't help you, I strongly urge you to show us just what isn't working in a way that we can reproduce, similar to the code I've posted above.



来源:https://stackoverflow.com/questions/20623264/does-java-have-any-known-issues-with-images-and-displaying-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!