Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Image not loading

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    20
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Image not loading

    Hello, I'm trying to load an image and display it as JLabels ImageIcon.
    This is from a method which loads it(the url is just fine)
    try {
    			URL url = new URL("http://wiki.sa-mp.com/wiki/Image:Skin_"+skin+".png");
    			skinImg = new BufferedImage(200,250,BufferedImage.TYPE_INT_ARGB);
    			Graphics2D g = skinImg.createGraphics();
    			g.drawImage(ImageIO.read(url), 0,0,200,250,null);
    			g.dispose();
    		}catch(IOException e) {
    			e.printStackTrace();
    		}
    And the display part(probably not very important)
    JLabel skinImg = new JLabel();
    				skinImg.setIcon(new ImageIcon(player.getSkinImage()));

    And nothing appers. I have other JLabels just after it, they are shown... But not the image one.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Image not loading

    Are you getting any errors? If so, post them. If not, create a runnable example with a common graphic type that we can substitute with our own that shows the problem, and we'll see if we can figure out what you're doing wrong. I'll bet that in the course of creating a simple, runnable example, you'll figure it out.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Bebras (January 10th, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    20
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Image not loading

    No errors. And you were wrong, I didn't figure it out.
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
     
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
     
     
     
    public class Test extends JApplet {
    	public void init() {
    		JPanel panel = new JPanel();
    		int skin = 126;
    		BufferedImage skinImg = null;
    		try {
    			URL url = new URL("http://wiki.sa-mp.com/wiki/Image:Skin_"+skin+".png");
    			skinImg = new BufferedImage(200,250,BufferedImage.TYPE_INT_ARGB);
    			Graphics2D g = skinImg.createGraphics();
    			g.drawImage(ImageIO.read(url), 0,0,200,250,null);
    			g.dispose();
    		}catch(IOException e) {
    			e.printStackTrace();
    		}
    		JLabel label = new JLabel();
    		label.setIcon(new ImageIcon(skinImg));
    		panel.add(label);
    		add(panel);
    	}
    }

    Here's a runnable example

  5. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Image not loading

    Quote Originally Posted by Bebras View Post
    (the url is just fine)
    No, the url is ok but .... it gets an HTML page! (and this is why don't work for ImageIO) The real image is at url:
    http://wiki.sa-mp.com/wroot/images2/3/3f/Skin_126.png
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. The Following User Says Thank You to andbin For This Useful Post:

    Bebras (January 10th, 2014)

  7. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    20
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Image not loading

    Now I feel stupid... Ofcourse thankyou.
    But it seems the URL is different for different skins...
    So I'll have to load images from my pc. But this is an applet... Could you give me some info on how to do it?

Similar Threads

  1. Selecting and dragging a image from multiple image in Java Applet
    By CY5 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2013, 02:44 PM
  2. Problems With Image Loading and Extended Objects
    By Gravity Games in forum Object Oriented Programming
    Replies: 13
    Last Post: April 22nd, 2013, 01:31 PM
  3. loading my image
    By riddick_charel in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2013, 06:12 AM
  4. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  5. ImageIcon Erratically Loading URL Image
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 3rd, 2011, 04:05 PM