Results 1 to 4 of 4
  1. #1
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default ...................... Importing images.

    I know there are a hundred billion tutorials on this but, naturally, NONE of them are working for me. Where do I put an image to make it work with my code??? Does it need to be a certain format? I've been using .png and .gif images, but apparently they are a format not created or known on earth.
    I've tried putting the images in the src folder of the workspace. I've tried creating a 'resources' folder and using ...("resources/theimagethatwillneverbeloaded.whateverextensionher e")
    I've tried putting my image drawing code in every fathomable line of space in my code.

    I never see the image drawn... I've tried toolkits, imageios, drawing, importing... nothing works. here's the code I have so far. I can't really get any further without ya know, actually having something drawn on screen.
    Java Code:
    package As04_animations;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Toolkit;
    
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.imageio.ImageWriter;
    import javax.swing.JApplet;
    
    
    public class animation extends JApplet implements KeyListener, MouseListener
    {
    	//I've tried putting the image initialisation code here.
           //I've tried it here!
    	public void init()
    	{
    		//Doesn't work if it's in here...
    	}
    	public void paint(Graphics Graph)
    	{
    		Image img = Toolkit.getDefaultToolkit().createImage("resources/battleship2.gif"); 
                 //and this certainly isn't working either.
    		Graph.setColor(Color.gray.darker());
    		//Graph.fillRect(0,0, getWidth(), getHeight());
    		Graph.drawImage(img, 0,0, null);
    		//Down here? Nope.
    	
    	}
    
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    If they are to be packaged within you project, use somthing like
    Java Code:
    ImageIO.read(getClass().getResource("/resources/battleship2.gif"));//first slash indicates the root of your package folder
    You should load it once rather than in the paint method
    Last edited by doWhile; 02-15-2011 at 08:17 PM.

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    292
    Rep Power
    4

    Default

    As I can see at the moment your program will search for image in subfolder resources of folder where your compiled classes are, something like:

    Java Code:
    + src
    - classes
     |
        - resources
         |
            image.gif
            image.png

  4. #4
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    I got with one of the TAs in class and we figured it out... Probably not the simplest of options, but we came up with
    Java Code:
    public void init()
    	{
    		File file;
    		setBackground(Color.BLACK);
    		try 
    		{
    			file = new File("/Users/seanrepanshek/Documents/workspace/CS1410 Spring 11/doc/resources/battleship2.gif");
    			image = ImageIO.read(file);
    
    		} catch (Exception e) {
    			System.out.println("There was some error in loading the image...");
    			e.printStackTrace();
    		}
    	}
    Now.. I've read that movement is pretty difficult to do, and from what I've been finding, this is not exactly untrue... Any ideas why this doesnt work? I'm not familiar yet with where repaint() should go and or when it should be called, but I don't see why this shouldn't work, but it doesnt...
    Java Code:
    import java.awt.event.MouseMotionListener;
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Ellipse2D;
    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.imageio.ImageWriter;
    import javax.swing.ImageIcon;
    import javax.swing.JApplet;
    
    public class animation extends JApplet implements KeyListener, MouseListener, MouseMotionListener
    {
    	int yV= 0;
    	int xV=0;
    	Image image = null;
    	public void init()
    	{
    		File file;
    		setBackground(Color.BLACK);
    		try 
    		{
    			file = new File("/Users/seanrepanshek/Documents/workspace/CS1410 Spring 11/doc/resources/battleship2.gif");
    			image = ImageIO.read(file);
    
    		} catch (Exception e) {
    			System.out.println("There was some error in loading the image...");
    			//e.printStackTrace();
    		}
    		addMouseListener (this);
    	}
    	public void paint(Graphics Graph)
    	{
    	     Graphics2D g2 = (Graphics2D) Graph;
    		setSize(400,400);
    		
            Graph.drawImage(image, xV, yV, 60, 120, null);
            addMouseListener (this); 
    	}
    	public void mouseClicked(MouseEvent e) 
    	{	
    	}
    
    	public void mousePressed(MouseEvent e) 
    	{
    	}
    
    	public void mouseReleased(MouseEvent e) 
    	{
    	}
    
    	@SuppressWarnings("unused")
    	public void keyTyped(KeyEvent e, Graphics Graph) { //pretty much just trying random things here....
    		int key = e.getKeyCode();
    		if (key == KeyEvent.VK_W)
    		{
    			yV = yV -10;
    			Graph.drawImage(image, xV, yV, 60, 120, null);
    			repaint();
    			Thread t = new Thread();
    			t.start();
    		}
    		
    	}
    	 public void run () 
    	    {  
    	        while(true) 
    	        { 
    	            try 
    	            { 
    	                Thread.sleep(20);  
    	            } 
    	            catch(Exception e) {} 
    	            repaint();
    	        } 
    	    }
    	public void keyPressed(KeyEvent e, Graphics Graph) {
    		int key = e.getKeyCode();
    		if (key == KeyEvent.VK_S) 
    		{
                yV = +10;
                Graph.drawImage(image, xV, yV+10, 60, 120, null);
                repaint();
            }
    		repaint();
    	}
    	public void keyReleased(KeyEvent e) 
    	{	
    		int key = e.getKeyCode();
    		if(key == KeyEvent.VK_A)
    		{
    			xV = 0;
    		}
    		if(key == KeyEvent.VK_D)
    		{
    			xV = 0;
    		}
    		if(key == KeyEvent.VK_W)
    		{
    			yV = 0;
    		}
    		if(key == KeyEvent.VK_S)
    		{
    			yV = 0;
    		}	
    	}
    	public void mouseEntered(MouseEvent e)
    	{	
    		yV=this.getY();
    		xV=this.getX();
    		repaint();
    		System.out.println(xV + " " + yV);  //this only returns 0 0. Why can't I get it to print where the mouse is?
    	}
    	public void mouseExited(MouseEvent e) 
    	{	
    	}
    	public void keyTyped(KeyEvent e) 
    	{	
    	}
    	public void keyPressed(KeyEvent e)  //animations actions to move ship, but don't do anything....
    	{	
    		int key = e.getKeyCode();
    		if(key == KeyEvent.VK_A)
    		{
    			xV = -1;
    		}
    		if(key == KeyEvent.VK_D)
    		{
    			xV = 1;
    		}
    		if(key == KeyEvent.VK_W)
    		{
    			yV = -1;
    		}
    		if(key == KeyEvent.VK_S)
    		{
    			yV = 1;
    		}
    	}
    }

Similar Threads

  1. Importing Packages
    By shadowfax57 in forum New To Java
    Replies: 3
    Last Post: 12-28-2010, 07:48 PM
  2. Replies: 0
    Last Post: 12-16-2010, 05:14 PM
  3. Replies: 4
    Last Post: 12-16-2010, 04:32 PM
  4. Importing JXL API
    By ryanlbowen in forum New To Java
    Replies: 0
    Last Post: 07-17-2009, 07:17 PM
  5. Importing package
    By bugger in forum New To Java
    Replies: 5
    Last Post: 11-26-2007, 12:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •