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 3 of 3

Thread: Image on a JPanel

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Image on a JPanel

    Hey guys, it's the first post for me here.
    I'm simply trying to make a gui for managing images for my purpose, but i fall at the first step: I'm actually not able to display an image, whether it would be a .gif or .jpg and i don't know why!
    I tried several code found on internet, after the first one, but none works. Do you find any problem on it?

    Thank you


    //--------MY PANEL FOR VIEWING IMAGE---------
    package graph2D;
     
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.Toolkit;
     
    import javax.swing.JPanel;
     
    class PannelloConImmagine extends JPanel {
    	private Image miaImmagine;
     
    	public PannelloConImmagine() {
    		miaImmagine = Toolkit.getDefaultToolkit().getImage("pippo.gif");
    		MediaTracker tracker = new MediaTracker(this);
    		tracker.addImage(miaImmagine, 0);
    		try {
    			tracker.waitForID(0);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
     
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.drawImage(miaImmagine, 0,0, this);
    	}
    }
     
     
    //------------GUI-------------
     
    package graph2D;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class ProvaPannelloConImmagine extends JFrame {
     
    	PannelloConImmagine myPanel;
     
    	public ProvaPannelloConImmagine() {
    		this.setTitle("PROVA");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		myPanel = new PannelloConImmagine();
    		this.add(myPanel);
     
    		this.setSize(600,600);
    		this.setVisible(true);
    		pack();
     
    	}
     
    	public static void main(String[] args) {
    		ProvaPannelloConImmagine pjf = new ProvaPannelloConImmagine();
    	}
    }

    it is supposed that i placed the image "pippo.gif" in my project "src" folder, isn't it? anyway I tried to add a new folder to my build Path, and place pippo.gif on it, but it doesn't work.
    Last edited by fricke; April 13th, 2014 at 07:01 AM. Reason: Java format code


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,362
    My Mood
    Mellow
    Thanks
    254
    Thanked 786 Times in 771 Posts

    Default Re: Image on a JPanel

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly.

    The code below, which is your code only modified slightly for my environment, works fine. I suspect your image file isn't being found, something simple. Check that the image is being founded and added.
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.Toolkit;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class TestClass extends JFrame
    {
        PannelloConImmagine myPanel;
     
        public TestClass()
        {
            this.setTitle("PROVA");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
     
            myPanel = new PannelloConImmagine();
            this.add(myPanel);
     
            this.setSize(600,600);
            this.setVisible(true);
            pack();
     
        }
     
        public static void main(String[] args)
        {
            new TestClass();
        }
    }
     
    class PannelloConImmagine extends JPanel
    {
        private Image miaImmagine;
     
        public PannelloConImmagine()
        {
            miaImmagine = Toolkit.getDefaultToolkit().getImage("./files/cup.gif");
            MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(miaImmagine, 0);
     
            setPreferredSize( new Dimension( 200, 200 ) );
     
            try {
                tracker.waitForID(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
     
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(miaImmagine, 0,0, this);
        }
    }
    BTW - my ./files folder is at the same level as my src folder.

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

    fricke (April 13th, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Image on a JPanel

    The code works fine since i reach the image at the following address "src\\immagine.jpg" and I added this line

    	setPreferredSize( new Dimension( 600, 600) );

    Thank you Greg!

Similar Threads

  1. [SOLVED] Image on JPanel ---> Canvas
    By justyStepi in forum AWT / Java Swing
    Replies: 19
    Last Post: May 8th, 2012, 08:20 PM
  2. Add Image to JPanel
    By bgroenks96 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 16th, 2011, 03:44 PM
  3. Save JPanel as image
    By fahien in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2011, 08:20 AM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM

Tags for this Thread