Jump to content

Why can't I add a image?


Ruzi

Recommended Posts

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 by Explv
  • Like 2
Link to comment
Share on other sites

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 by Ruzi
Link to comment
Share on other sites

 

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):

 

51af38f61d.png

 

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 by Explv
  • Like 2
Link to comment
Share on other sites

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):

 

51af38f61d.png

 

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! 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...