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

Thread: Image on JPanel ---> Canvas

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Image on JPanel ---> Canvas

    Hello,

    I'm trying to add an Image to my JPanel, but I'm not sure how to add it. I know there are lots of thread like this available, but for me I can't get it working.

    I found that for adding picture I can use Canvas. Can you pls help me and show me what I'm doing wrong?

    here is the code ....

    		Maps = new JPanel();
    		mainTab.addTab("Maps", null, Maps, null);
     
    		Gcanava canvas = new Gcanava();
    		Maps.add(canvas);

    Here the mainTab is a JTabbledPanel and I have a Maps panel on it.

    Gcanava is different class where I have the code for adding picture:

    class Gcanava extends JComponent{
    	/**
    	 * 
    	 */
     
    	public void paint(Graphics g){
    //		Graphics2D g2 = (Graphics2D) g;
    //		
    //		Image img = Toolkit.getDefaultToolkit().getImage("pictures/map.jpg");
    //		g2.drawImage(img, 100, 100,this);
    //		g2.finalize();


    Can you please help me.

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image on JPanel ---> Canvas

    You should override the paintComponent() method in Swing classes not paint().
    Load the image one time outside of the paintComponent method, not every time the method is called.

    Give the component a size (setPreferredSize) or it could have a size of 0
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image on JPanel ---> Canvas

    hey,

    I change the paint() to paint Component()
    [/CODE]
    Image img;
    Graphics g;

    public void paintComponent(Graphics g){

    }
    public Gcanava(){
    try{

    img =ImageIO.read(new File("pictures/map.jpg"));

    }
    catch(IOException e){
    g.drawImage(img,0,0,this);
    }


    }

    }
    [/CODE]

    Where should I change the size? of the Graphics g? or the Image?

    And also now, when I do it, the whole JPanel turns into canvas which I dont want to I want to have a some other components there.

    How can i do that

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image on JPanel ---> Canvas

    Set the size of the Gcanava object. The container's layout manager will use that size when it positions the object.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image on JPanel ---> Canvas

    I kinda changed it, but now im having problems with these exceptions,why?


    at Gcanava.<init>(Gcanava.java:33)
    at NewExample.initialize(NewExample.java:343)
    at NewExample.<init>(NewExample.java:78)
    at NewExample$1.run(NewExample.java:65)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image on JPanel ---> Canvas

    You left off the first lines of the error message that say what the exception was.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image on JPanel ---> Canvas

    It was : java.lang.NullPointerException :/

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image on JPanel ---> Canvas

    Look at line 33 and find the variable that has the null value and then backtrack in the code to find out why the variable does not have a valid non-null value.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image on JPanel ---> Canvas

    Well , line 33 is ---> g.drawImage(img,200,200,this); it used to be set to 0 and 0 but I changed it to 200. And I thought that it will help but it doesnt,why?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image on JPanel ---> Canvas

    what variable has the null value? 200 vs 0 would not cause an exception.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image on JPanel ---> Canvas

    I just found that it might be because of the size of the image. The image might be bigger then my frame size. And for this reason I might be having troubles. Is it possible?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image on JPanel ---> Canvas

    I don't think that will cause a null pointer exception.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image on JPanel ---> Canvas

    Hmm...I can provide you the whole code if you want nd you can see whats happening..

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image on JPanel ---> Canvas

    You should be able to find what variable is null. Add a println just before where the error occurs and print out the variables that are on the line with the error. The print out will show you which variable is null.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image on JPanel ---> Canvas

    Console says that it's the line 33 which is the g.drawImage(img,200,200,this); but it's kinda weird. how this can give me an nullPointerException when the dimension is given

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image on JPanel ---> Canvas

    did you print out the variables? what was null?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image on JPanel ---> Canvas

    Wtf!!

    Right now I commented out thise part of the code nd use [/CODE]
    class Gcanava extends JComponent{
    /**
    *
    */
    Image img;
    Graphics g;

    public void paintComponent(Graphics g){
    Graphics2D g2 = (Graphics2D) g;

    Image img = Toolkit.getDefaultToolkit().getImage("images/map.jpg");
    g2.drawImage(img, 100, 100,this);
    g2.finalize();

    }


    public Gcanava(){
    // try{
    //
    // img =ImageIO.read(new File("images/map.jpg"));
    //
    // }
    // catch(IOException e){
    // g.drawImage(img,200,200,this);
    // }
    //

    }

    }
    [/CODE]

    nd I run it, no exception at all, and even a "border" for the picture appeared on my JPanel why??

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image on JPanel ---> Canvas

    What variable was null?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image on JPanel ---> Canvas

    Maybe g? I dont know, since the line 33 g.drawImage(); is commented out

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image on JPanel ---> Canvas

    A commented out line would not cause an exception.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Add Image to JPanel
    By bgroenks96 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 16th, 2011, 02:44 PM
  2. Save JPanel as image
    By fahien in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2011, 08:20 AM
  3. [SOLVED] Can't Import AWT Image into custom JPanel
    By JavaSquared in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2011, 12:59 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