Results 1 to 3 of 3
  1. #1
    duplicity is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Question Cant put a in a image.

    :confused:I am looking to put in a image into this app that I am working on in class but every were i have looked online has not helped me. I can’t get a pick to work inside this code could someone help me I would like to put a image file name in, but I don’t even know where to start.....
    Java Code:
    package Java;
    
    import java.awt.Color;		//color class 
    import java.awt.Graphics;	//primitive graphics class 
    import java.awt.Graphics2D; 	//Extends Graphics class
    import java.awt.Rectangle;	//Rectangles class
    import java.awt.geom.Ellipse2D;  //Ellipse and circle class
    import java.awt.geom.Line2D; 	//Line class
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    
    public class FaceComponent extends JComponent
    {
    	//Place drawing instructions inside this method
    	public void paintComponent(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D) g;  //Make graphics 2D
    
    		drawGrid(g2);  //Draw grid to make it easier to create component
    		
    		Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 100, 150); //Construct oval head (x, y, width, height)
    		g2.draw(head);             //Draw head
    		Line2D.Double eye1 = new Line2D.Double(25, 70, 45, 90); 	//Construct left eye (x1, y1, x2, y2)
    		g2.draw(eye1);
    		Line2D.Double eye2 = new Line2D.Double(85, 70, 65, 90);	 //Construct right eye (x1, y1, x2, y2)
    		g2.draw(eye2);
    		Rectangle mouth = new Rectangle(30, 130, 50, 5);	 	//Construct mouth (x, y, width, height)
    		g2.setColor(Color.RED);			//Color of mouth
    		g2.fill(mouth);				//Fill mouth with red
    		g2.setColor(Color.BLUE);			
    		g2.drawString("Hello, Class!!!", 5, 175); //Draw greeting (“message”, x, y)
    		
    		
    		
    
    	}
    
    	public void drawGrid(Graphics2D g2)
    	{
    		// The following two for loops draw a grid on the screen to help you 
    		// position your object.  Once you have finished your object, you can 
    		// comment out the calling of this method
    		
    		//Draw horizontal lines
    		g2.setColor(Color.LIGHT_GRAY);									
    		for (int i=50; i<=600; i=i+50)
    		{
    			g2.drawString(""+i, 5, i);
    			Line2D.Double horizonalLine = new Line2D.Double(5, i, 700, i);	
    			g2.draw(horizonalLine);
    		}
    		//Draw vertical lines
    		for (int i=50; i<=600; i=i+50)
    		{
    			g2.drawString(""+i, i, 10);
    			Line2D.Double horizonalLine = new Line2D.Double(i, 10, i, 700);	
    			g2.draw(horizonalLine);
    		}
    		g2.setColor(Color.BLACK);									
    		//**End of grid
    	}
    }
    I am so lost please help
    Thanks in advance.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    The method to use is drawImage. Set the forum/net, there are plenty of examples out there.

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    Here's how I do it:

    a) the image is stored in a subdirectory of where the code is;
    for code in project com.physpics.photos, the image is in
    .../com/physpics/photos/images

    b) I have this utility method in Utils

    Java Code:
    /** Fetches an image from the class directory's /image/ directory.
     * NB: The class must be "built" so the images are
     * copied from the src/ directory to the  build/ directory
     * @param path  Path of resource, realtive to the .class file's directory
     * Typically,  images are in a subdirectory of the class directory
     * and the path is of the form "images/icon.ext"
     * where ext is for an icon (ico) or an image (gif, png, or jpg).
     * @param description  The description of the icon for accessible contexts
     * @return  An Image, or null if there is an error.
     */
    static public Image fetchImageResource(String path,
                                              String description) {
    	java.net.URL imgURL = Tagger.class.getResource(path);
    	Image o = null;
    	if (imgURL != null) try {
    		o = ImageIO.read(imgURL);
    	} catch (IOException ex) { }  // defer to error message below
    	if (o != null) return o;
    	log.severe("Couldn't load image resource: " + path);
    	return null;
    }
    (Note that the log.severe() call could be changed to System.out.println()

    c) To load the image:
    Java Code:
    static final Image cat = Utils.fetchImageResource("images/cat.jpg", "my first cat");
    d) For paint() to display the image at x,y: g.drawImage(cat, x, y, this);

Similar Threads

  1. Replies: 1
    Last Post: 04-17-2009, 12:44 AM
  2. Replies: 14
    Last Post: 04-10-2009, 08:46 AM
  3. Canvas Image popups another image (SWT)
    By SpaceY in forum New To Java
    Replies: 2
    Last Post: 11-11-2008, 01:25 PM
  4. drawing an image to an offscreen image
    By hunterbdb in forum Java 2D
    Replies: 9
    Last Post: 10-30-2008, 06:17 PM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Tags for this Thread

Posting Permissions

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