March 21, 20169 yr Title^^ @Override public void onPaint(Graphics2D g) { Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png"); g.drawImage(bg, 100, 100, null); } private Image getImage(String url) { try { return ImageIO.read(new URL(url)); } catch (IOException e) {} return null; } It doesn't show anything.
March 21, 20169 yr Title^^ @Override public void onPaint(Graphics2D g) { Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png"); g.drawImage(bg, 100, 100, null); } private Image getImage(String url) { try { return ImageIO.read(new URL(url)); } catch (IOException e) {} return null; } It doesn't show anything. What you are doing is a bad idea. onPaint is called many times per second, and in every loop you are re-getting the image from that url. You should only get the image once and store it. Edited March 21, 20169 yr by Explv
March 21, 20169 yr Author What you are doing is a bad idea. onPaint is called many times per second, and in every loop you are re-getting the image from that url. You should only get the image once and store it. It didn't work sir. Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png"); @Override public void onPaint(Graphics2D g) { g.drawImage(bg, 100, 100, null); } private Image getImage(String url) { try { return ImageIO.read(new URL(url)); } catch (IOException e) {} return null; } Can anyone help me out? Appreciate it very much! Edited March 21, 20169 yr by Ruzi
March 21, 20169 yr It didn't work sir. Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png"); @Override public void onPaint(Graphics2D g) { g.drawImage(bg, 100, 100, null); } private Image getImage(String url) { try { return ImageIO.read(new URL(url)); } catch (IOException e) {} return null; } You should really learn how to debug yourself. For starters, you are catching an Exception and never handling it. If you had printed out the exception by doing this: Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png"); @Override public void onPaint(Graphics2D g){ if(bg != null){ g.drawImage(bg, 100, 100, null); System.out.println("Drawing image"); } } private Image getImage(String url) { try { return ImageIO.read(new URL(url)); } catch (IOException e) { e.printStackTrace(); } return null; } You would have seen this displayed (in debug mode): The website returns a 403 response code (Forbidden error), most likely because it looks at the request headers and sees that you are not visiting this url from a web browser. You can either reupload the image to a different site, or you can set the User-Agent in your Java code to be a browser's User-Agent like this: Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png"); @Override public void onPaint(Graphics2D g){ if(bg != null){ g.drawImage(bg, 100, 100, null); System.out.println("Drawing image"); } } private Image getImage(String url) { try { URL imgUrl = new URL(url); URLConnection connection = imgUrl.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); return ImageIO.read(connection.getInputStream()); } catch (IOException e) { e.printStackTrace(); } return null; } Which works. Edited March 21, 20169 yr by Explv
March 21, 20169 yr Author You should really learn how to debug yourself. For starters, you are catching an Exception and never handling it. If you had printed out the exception by doing this: Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png"); @Override public void onPaint(Graphics2D g){ if(bg != null){ g.drawImage(bg, 100, 100, null); System.out.println("Drawing image"); } } private Image getImage(String url) { try { return ImageIO.read(new URL(url)); } catch (IOException e) { e.printStackTrace(); } return null; } You would have seen this displayed (in debug mode): The website returns a 403 response code (Forbidden error), most likely because it looks at the request headers and sees that you are not visiting this url from a web browser. You can either reupload the image to a different site, or you can set the User-Agent in your Java code to be a browser's User-Agent like this: Image bg = getImage("http://s9.postimg.org/5tbnkub0r/Namnl_st_1.png"); @Override public void onPaint(Graphics2D g){ if(bg != null){ g.drawImage(bg, 100, 100, null); System.out.println("Drawing image"); } } private Image getImage(String url) { try { URL imgUrl = new URL(url); URLConnection connection = imgUrl.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); return ImageIO.read(connection.getInputStream()); } catch (IOException e) { e.printStackTrace(); } return null; } Which works. Thank you man!
Create an account or sign in to comment