Paradox68 Posted January 8, 2016 Share Posted January 8, 2016 I had it but I lost the snippet for getting an image from URL and putting it into onPaint Quote Link to comment Share on other sites More sharing options...
LoudPacks Posted January 8, 2016 Share Posted January 8, 2016 I had it but I lost the snippet for getting an image from URL and putting it into onPaint private static Image getImage(String url) { try { return ImageIO.read(new URL(url)); } catch (IOException e) { } return null; } Quote Link to comment Share on other sites More sharing options...
Explv Posted January 8, 2016 Share Posted January 8, 2016 (edited) -Deleted duplicate- Edited January 8, 2016 by Explv Quote Link to comment Share on other sites More sharing options...
Acerd Posted January 8, 2016 Share Posted January 8, 2016 Method private Image getImage(String url) { try { return ImageIO.read(new URL(url)); } catch (IOException e) { log(e); } return null; } Quote Link to comment Share on other sites More sharing options...
Explv Posted January 8, 2016 Share Posted January 8, 2016 (edited) - Edited January 8, 2016 by Explv Quote Link to comment Share on other sites More sharing options...
Paradox68 Posted January 8, 2016 Author Share Posted January 8, 2016 You should return BufferedImage not Image why is bufferedImage better for scripting than just using an Image? Quote Link to comment Share on other sites More sharing options...
Explv Posted January 8, 2016 Share Posted January 8, 2016 (edited) why is bufferedImage better for scripting than just using an Image? deleted answer Ignore the stupid shit I said Edited January 8, 2016 by Explv Quote Link to comment Share on other sites More sharing options...
FrostBug Posted January 8, 2016 Share Posted January 8, 2016 (edited) Image is the superclass for all Image classes. BufferedImage extends Image and is what you want to use when drawing. ImageIO.read(); returns a BufferedImage, therefore your getImage() method should also return a BufferedImage. You should promote loose coupling where you can. The majority of image drawing methods in Graphics2D (mostly inherited from Graphics) take Image, as they do not require any specific methods located in BufferedImage. For this reason, returning Image is the less restrictive option. Saying that people should return BufferedImage just because that's the specific implementation of the Image is pretty much your own preference. Would you also use LinkedList or ArrayList as a return type rather than List? It's fine if you do, but at least when writing API for others to use, you should use the highest level class or interface you can. Also to add to the 3 (identical) previous answers; some image hosts may have browser checks to prevent automated use. This can be bypassed by adding the User-Agent property. public static Image loadImageByURL(String surl) { Image img = null; try { URL url = new URL(surl); URLConnection urlc = url.openConnection(); urlc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); img = ImageIO.read(urlc.getInputStream()); } catch (IOException ex) { Logger.getLogger(ResourceLoader.class.getName()).log(Level.SEVERE, null, ex); } return img; } Edited January 8, 2016 by FrostBug Quote Link to comment Share on other sites More sharing options...
Explv Posted January 8, 2016 Share Posted January 8, 2016 You should promote loose coupling where you can. The majority of image drawing methods in Graphics2D (mostly inherited from Graphics) take Image, as they do not require any specific methods located in BufferedImage. For this reason, returning Image is the less restrictive option. Saying that people should return BufferedImage just because that's the specific implementation of the Image is pretty much your own preference. Would you also use LinkedList or ArrayList as a return type rather than List? It's fine if you do, but at least when writing API for others to use, you should use the highest level class or interface you can. You are right, I wasn't thinking about the drawImage methods in Graphics. Quote Link to comment Share on other sites More sharing options...
Paradox68 Posted January 8, 2016 Author Share Posted January 8, 2016 (edited) You should promote loose coupling where you can. The majority of image drawing methods in Graphics2D (mostly inherited from Graphics) take Image, as they do not require any specific methods located in BufferedImage. For this reason, returning Image is the less restrictive option. Saying that people should return BufferedImage just because that's the specific implementation of the Image is pretty much your own preference. Would you also use LinkedList or ArrayList as a return type rather than List? It's fine if you do, but at least when writing API for others to use, you should use the highest level class or interface you can. Also to add to the 3 (identical) previous answers; some image hosts may have browser checks to prevent automated use. This can be bypassed by adding the User-Agent property. public static Image loadImageByURL(String surl) { Image img = null; try { URL url = new URL(surl); URLConnection urlc = url.openConnection(); urlc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); img = ImageIO.read(urlc.getInputStream()); } catch (IOException ex) { Logger.getLogger(ResourceLoader.class.getName()).log(Level.SEVERE, null, ex); } return img; } @FrostBug are.....are you a wizard? p.s. I thought @Explv was a wizard before, but he just got WIZARDED wtfffff Edited January 8, 2016 by Paradox68 Quote Link to comment Share on other sites More sharing options...