Results 1 to 4 of 4

Thread: Display an image in JPanel

  1. #1
    Join Date
    Apr 2009
    Posts
    40

    Display an image in JPanel

    Hello,

    I had picture which i wanted to display in JPanel but it seems very difficult to be displayed.

    It gives me a NullPointerException every time, when I checked the path,and recovers with or without a JFileChooser. I tried other images and extensions. but Nothing appears...

    Here is my piece of code:


    Code:
      java. awt. Toolkit toolkit = java. awt. Toolkit. getDefaultToolkit (); 
      Image image = toolkit. GetImage ( "c/ program files /java  /home/ Ange.gif"); 
      if (image! = null) 
      g. drawImage (image, 10, 10, 90, 90, jPanel1);
    If someone sees where is the problem, thank you.

  2. #2
    Join Date
    Oct 2008
    Posts
    167

    Re: Display an image in JPanel

    probably u need to call repaint() or revalidate() methods.

    Code:
    import javax.swing.JPanel;
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    
    public class ImagePan extends JPanel {
      protected  Image ci=null;
      
      public ImagePan() {
      }
      public void setImage(Image si) {
        this.ci=si;
        validate();
        repaint();
    
      }
      public Image getDisplayedImage() {
        return this.ci;
      }
    
      public void update(Graphics g) {
        if (ci!=null) {
          g.drawImage(ci, 0,0,this.getSize().width,this.getSize().height, this);
        } else {
          super.update();
        }
      }
      public void paint (Graphics g) {
        update(g);
      }
    }

  3. #3
    Join Date
    Apr 2009
    Posts
    40

    Re: Display an image in JPanel

    Thank you for your answer.

    I tried also with imageIo.

    In fact, I do not know how to declare and use a method here paintComponent (Graphics g), then call when you click a button to display the image.

    If someone could show me that I would find it useful.

    Thank you in advance

  4. #4
    Join Date
    Dec 2008
    Posts
    202

    Re: Display an image in JPanel

    I tried also with imageIo.
    In fact, I do not know how to declare and use a method here paintComponent (Graphics g)
    It is a simple method overloading. I strongly advise you to leave on a good foundation and I formed this using Java language. Go see the tutorial and the site.

    then call it when you click a button to display the image.
    The paintComponent method is never called directly. There should be a call to "repaint" or similar methods.

Similar Threads

  1. Display JtextField in Jpanel
    By New ID in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 12:01 PM
  2. JPanel class of java
    By Ram Bharose in forum Software Development
    Replies: 4
    Last Post: 16-02-2010, 11:01 AM
  3. Display An Image In A JLabel
    By Amaresh in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 05:56 PM
  4. Component event in JPanel
    By John Wilson in forum Software Development
    Replies: 1
    Last Post: 04-11-2009, 01:44 PM
  5. How to display image from the web in ACCESS
    By Janet J in forum Software Development
    Replies: 2
    Last Post: 04-02-2009, 06:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,772,492.90864 seconds with 17 queries