Below is a snippet of code to correctly paint an ImageIcon from a URL:
private void getImageIconFromURL(String urlOfIcon) throws IOException {
ImageIcon imageIcon = null;
URL url = new URL(urlOfIcon );
URLConnection connection = url.openConnection();
int length = connection.getContentLength();
InputStream is = connection.getInputStream();
byte[] bytes = new byte[length];
int totalRead = 0;
while( totalRead length )
{
int numberRead = is.read( bytes, totalRead, length - totalRead );
totalRead += numberRead;
}
try {
imageIcon = new ImageIcon(bytes);
if (imageIcon != null) {
imageIcon.paintIcon(this.rootContainer, 10, 10);
}
} catch (ImageIconAppendException e) {
e.printStackTrace();
}
}
FAQ Subject
Faq Category