January 8, 201610 yr I had it but I lost the snippet for getting an image from URL and putting it into onPaint
January 8, 201610 yr 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; }
January 8, 201610 yr Method private Image getImage(String url) { try { return ImageIO.read(new URL(url)); } catch (IOException e) { log(e); } return null; }
January 8, 201610 yr Author You should return BufferedImage not Image why is bufferedImage better for scripting than just using an Image?
January 8, 201610 yr why is bufferedImage better for scripting than just using an Image? deleted answer Ignore the stupid shit I said Edited January 8, 201610 yr by Explv
January 8, 201610 yr 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, 201610 yr by FrostBug
January 8, 201610 yr 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.
January 8, 201610 yr Author 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, 201610 yr by Paradox68
Create an account or sign in to comment