Results 1 to 5 of 5
  1. #1
    Rinas is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default An image inside JPanel.

    Please, tell me how to add an image inside Jpanel or JFrame or JLabel.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: An image inside JPanel.

    Surely you've found suggestions on Google first and don't mean for us to re-write that which has been written about thousands of times? So given those suggestions, what have you tried and what doesn't work?

  3. #3
    Rinas is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: An image inside JPanel.

    I'm sorry. I tried soma examples and nothing works.
    Java Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class prob1 {
    
      public static void main(String[] args) {
        ImagePanel panel = new ImagePanel(new ImageIcon("1.jpg").getImage());
    
        JFrame frame = new JFrame();
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
      }
    }
    
    class ImagePanel extends JPanel {
    
      private Image img;
    
      public ImagePanel(String img) {
        this(new ImageIcon(img).getImage());
      }
    
      public ImagePanel(Image img) {
        this.img = img;
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
      }
    
      public void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, null);
      }
    
    }
    This program doesnt work.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: An image inside JPanel.

    Are you sure that you're looking in the right place for your file?

    Please put this somewhere in code that is executed:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    Is your 1.jpg file located in the directory that you see printed in the console?

  5. #5
    Rinas is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: An image inside JPanel.

    Thank you, I understood my mistake.

Similar Threads

  1. Retrieving mouse location inside a thread relative to an external JPanel.
    By Zambash in forum Threads and Synchronization
    Replies: 7
    Last Post: 08-25-2011, 05:33 PM
  2. Help making a grid inside of a JPanel
    By satanfx55 in forum AWT / Swing
    Replies: 3
    Last Post: 05-13-2011, 08:18 PM
  3. JPanel inside Jtabbed isn't display
    By Ravanelly in forum Advanced Java
    Replies: 7
    Last Post: 06-08-2010, 11:19 PM
  4. image not dispalyed inside border
    By ibrahimyoussof in forum AWT / Swing
    Replies: 1
    Last Post: 04-17-2010, 05:19 PM
  5. How to add JFrame inside JPanel
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 8
    Last Post: 12-13-2009, 09:41 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
  •