Ruzi Posted March 21, 2016 Share Posted March 21, 2016 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. Quote Link to comment Share on other sites More sharing options...
Explv Posted March 21, 2016 Share Posted March 21, 2016 (edited) 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, 2016 by Explv 2 Quote Link to comment Share on other sites More sharing options...
Bigmain533 Posted March 21, 2016 Share Posted March 21, 2016 your about profile pic?same problem Quote Link to comment Share on other sites More sharing options...
Acerd Posted March 21, 2016 Share Posted March 21, 2016 your about profile pic?same problem 2 Quote Link to comment Share on other sites More sharing options...
Ruzi Posted March 21, 2016 Author Share Posted March 21, 2016 (edited) 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, 2016 by Ruzi Quote Link to comment Share on other sites More sharing options...
Explv Posted March 21, 2016 Share Posted March 21, 2016 (edited) 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, 2016 by Explv 2 Quote Link to comment Share on other sites More sharing options...
Ruzi Posted March 21, 2016 Author Share Posted March 21, 2016 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! Quote Link to comment Share on other sites More sharing options...