Results 1 to 3 of 3
  1. #1
    Habib is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Image on a panel in java, how??

    I am a beginner in java. in a project i want to set an image in a panel. I'm in a fix what to do.
    Please anyone help me. Very urgent.

  2. #2
    brigadier90 is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    try this:

    1)create an image instance variable:

    Image face;

    2)load the image in the constructor of the panel:

    face= new ImageIcon("image location on disk").getImage();

    3) in the paintComponent method after you call super.painComponent(g), call the draw image method and give it as a first parameter face:

    public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(face,x,y,null) // x,y = position of image, null = imageobserver

    }
    hope this helps...
    Last edited by brigadier90; 02-23-2011 at 11:28 AM.

  3. #3
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by brigadier90 View Post
    3) in the paintComponent method after you call super.painComponent(g), call the draw image method and give it as a first parameter face:

    public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(face,x,y,null) // x,y = position of image, null = imageobserver

    }
    hope this helps...
    Instead of #3, it's a whole lot easier to just to create a JLabel with the face icon and add it to your panel, perhaps as the CENTER component in a BorderLayout.

Similar Threads

  1. Replies: 1
    Last Post: 10-24-2010, 09:25 PM
  2. Replies: 4
    Last Post: 10-14-2010, 09:18 AM
  3. How to display image in a panel with JFileChooser
    By srisar in forum AWT / Swing
    Replies: 14
    Last Post: 10-11-2009, 07:06 PM
  4. How to set Image in Panel ?
    By sudmitra in forum New To Java
    Replies: 0
    Last Post: 09-12-2009, 11:07 PM
  5. add image on panel
    By samiksha.goel in forum AWT / Swing
    Replies: 4
    Last Post: 08-02-2008, 08:38 PM

Posting Permissions

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