Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 09-05-2009
Member
 
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.
Reply With Quote
  #2  
Old 09-05-2009
Member
 
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);
  }
}
Reply With Quote
  #3  
Old 09-05-2009
Member
 
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
Reply With Quote
  #4  
Old 09-05-2009
Member
 
Join Date: Dec 2008
Posts: 202
Re: Display an image in JPanel

Quote:
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.

Quote:
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 04:46 PM.