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 Icon problem

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Image Icon problem

    Hi guys I need a little help So, i create a little game, and i want this game sent my friend. Problem is model. When i sent game, my friend not see a model. I Sent this thing in the folder with models, but its not work. This is code:

    public void setImage5() {
    		ImageIcon i = new ImageIcon("C:/Documents and Settings/Vakaris/Desktop/JAVA/Game/src/Tikras3.gif");
    		face = i.getImage();
    	}

    (This works only me)

    I try this code change like
     ImageIcon i = new ImageIcon("C:/Desktop/Game/Tikras3.gif");

    But this not work. What you do recomend?
    I think about instal but this is stupid for so small game


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Image Icon problem

    Try packaging your files up as resources inside the jar and use the getResource() functions to load them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Image Icon problem

    Thanks, but i watch a lot tut and this code :
    public void setImage() {
     
    		ImageIcon u = new ImageIcon(getClass().getResource("/Image/Tikras2.gif"));
    		face = u.getImage();
    	}

    dont work. I create new package Image like tut and this not work..

    Full code:

    package Pakas;
     
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.net.MalformedURLException;
    import java.net.URL;
     
    import javax.swing.*;
     
     
     
    public class Game extends JFrame implements Runnable {
     
    	private Image dbImage;
    	private Graphics dbg;
    	Image face;
    	int x,y,xDirection, yDirection;
     
    	Font font = new Font("Arial",Font.BOLD | Font.ITALIC, 30);
     
    	public void run() {
    		try {
     
    			while(true) {
     
    				move();
     
    				Thread.sleep(5);
    			}
    		}catch(Exception e) {
    			System.out.println("Error");
    		}
    	}
    	public void move() {
    		x+=xDirection;
    		y+= yDirection;
     
    		if (x <= 0)
    			x = 0;
    		if (x >= 200)
    			x = 200;
    		if (y <= 50)
    			y = 50;
    		if (y >= 190)
    			y = 190;
    	}
     
    	public void setXDirection(int xdir) {
    		xDirection = xdir;
    	}
     
    	public void setYDirection(int ydir) {
    		yDirection = ydir;
    	}
    	//Creating images!!!!
    	public void setImage() {
    		//This image for test load.
    		ImageIcon u = new ImageIcon(getClass().getResource("/Image/Tikras2.gif"));
    		face = u.getImage();
    	}
     
    	public void setImage2() {
    		ImageIcon i = new ImageIcon("C:/Documents and Settings/Vakaris/Desktop/JAVA/Game/src/Tikras1.gif");
    		face = i.getImage();
    	}
     
    	public void setImage3() {
    		ImageIcon i = new ImageIcon("C:/Documents and Settings/Vakaris/Desktop/JAVA/Game/src/Tikras.gif");
    		face = i.getImage();
    	}
     
    	public void setImage4() {
    		ImageIcon i = new ImageIcon("C:/Documents and Settings/Vakaris/Desktop/JAVA/Game/src/Tikras.gif");
    		face = i.getImage();
    	}
     
    	public void setImage5() {
    		ImageIcon i = new ImageIcon("C:/Documents and Settings/Vakaris/Desktop/JAVA/Game/src/Tikras3.gif");
    		face = i.getImage();
    	}
     
    	public class AL extends KeyAdapter {
    		public void keyPressed(KeyEvent e) {
    			int KeyCode = e.getKeyCode();
     
     
    			if (KeyCode == e.VK_LEFT) {
    			setXDirection(-1);
    			setImage();
    			}
    			if (KeyCode == e.VK_RIGHT) {
    				setXDirection(+1);
    				setImage2();
    			}
    			if (KeyCode == e.VK_UP) {
    				setYDirection(-1);
    				setImage3();
     
    			}
    			if (KeyCode == e.VK_DOWN) {
    				setYDirection(+1);
    				setImage5();
     
    				}				
    		}
     
    		public void keyReleased(KeyEvent e) {
    			int KeyCode = e.getKeyCode();
    			if (KeyCode == e.VK_LEFT) {
    			setXDirection(0);
    			}
    			if (KeyCode == e.VK_RIGHT) {
    				setXDirection(0);
    			}
    			if (KeyCode == e.VK_UP) {
    				setYDirection(0);
    			}
    			if (KeyCode == e.VK_DOWN) {
    				setYDirection(0);
    }
    		}
    		}
     
     
     
     
    	public Game() {
    		//Images
    		ImageIcon i = new ImageIcon("C:/Documents and Settings/Vakaris/Desktop/JAVA/Game/src/Tikras.gif");
    		face = i.getImage();
    		//Nustatymai
    		addKeyListener(new AL());
    		setTitle("Java Game");
    		setSize(250,250);
    		setResizable(false);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBackground(Color.black);
    		x = 150;
    		y = 150;
     
    	}
     
     
    	public void paint(Graphics g) {
    		dbImage = createImage(getWidth(), getHeight());
    		dbg = dbImage.getGraphics();
    		paintComponent(dbg);
    		g.drawImage(dbImage, 0, 0, this);
     
    	}
     
     
     
     
    	public void paintComponent(Graphics g) {
    		g.setFont(font);
    		g.setColor(Color.BLUE);
    		g.drawString("Hello World", 50, 50);
     
    		g.drawImage(face,x,y,this);
     
     
     
     
    		repaint();
    	}
     
     
    	public static void main(String[] args) {
    		Game game = new Game();
    		Thread t1 = new Thread(game);
    		t1.start();
    	}
     
     
     
    }

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

    Default Re: Image Icon problem

    Quote Originally Posted by xkendzu View Post
    		ImageIcon i = new ImageIcon("C:/Documents and Settings/Vakaris/Desktop/JAVA/Game/src/Tikras3.gif");
    Putting a "cabled" absolute path into the source is really a bad bad thing, acceptable just only for a quick try/test.
    If the image is "part" of your software, the best way is to use getResource/getResourceAsStream of a java.lang.Class. These methods work with the same concepts that are used for class loading, that is, they find resources "on the classpath".

    URL imgUrl = YourClass.class.getResource("someimg.gif");
    ImageIcon i = new ImageIcon(imgUrl);

    The someimg.gif is "relative" to the package of YourClass and is found independently of the "current directory". And this works if classes/resources are packaged or unpackaged in a jar.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

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

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Location
    Lithuania
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Image Icon problem

    System.out.println("Thank you so much )))");

Similar Threads

  1. Two questions: setting an image icon and closing up a game
    By MW130 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 24th, 2014, 03:46 AM
  2. [SOLVED] Display icon/image in a JTable when reading in from MySQL
    By Psyclone625 in forum AWT / Java Swing
    Replies: 4
    Last Post: December 6th, 2013, 12:12 PM
  3. Replies: 0
    Last Post: February 14th, 2011, 06:10 AM
  4. Stupid thing can't find image icon right in front of it!!!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 9th, 2010, 01:02 AM
  5. Icon change and lib folder problem
    By LeonLanford in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2009, 03:00 AM