Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By fex

Thread: Load image to jPanel

  1. #1
    DJIT is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default Load image to jPanel

    hi all,

    how can i load image to an jPanel
    i see example in Computer Graphics Using Java™ 2D and 3D book, but not using netbeans, it usse the jpanel as a class.

    here is my netbeans project.
    ImgView.zip

    how can i reuse the jpanel paint method to load the image.
    thanks.

  2. #2
    fex
    fex is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Hello,

    Next time, put your code in the post, not as zip. I'm not touching your file for security reasons ;)

    You say you are using the JPanel class, I assume you created your own class which extends JPanel. Then simply override paintComponent(Graphics g) method.

    Java Code:
        @Override
        public void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;            // adds more functionality
            super.paintComponent(g2);                   // don't forget this !
            if (image != null) {                               // field image
                g.drawImage(image, 0, 0, this);         // draw at point (0,0)
            }
        }
    Simply write a file to your image and call for a repaint method, the repaint methode invokes paintComponent method :D

    Java Code:
    File file = new File("path/to/your/image.jpg");
    BufferedImage image = ImageIO.read(file);
    this.image = image;                                      // field image is set to local
    repaint();
    Shikatsu likes this.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,015
    Rep Power
    10

    Default

    If all you want to do is display an image, the alternative is to simply load your image as an icon in a JLabel and add that JLabel to the JPanel. If you do it that way, there is no need to subclass JPanel.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    And learn Swing.
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    That's easier than struggling to get things the way you want them in the NetBeans visual designer.

    db

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    Quote Originally Posted by fex View Post
    Java Code:
    this.image = image;                                      // field image is set to local
    fex, what exactly does that comment mean?

    db

  6. #6
    fex
    fex is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    You are storing the local variable named 'image' in the field named 'image'.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    So why have a local variable that's never used? Why not assign the field directly from ImageIO#read(...)?

    db

  8. #8
    fex
    fex is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Just to clearly show DJIT it's a BufferedImage. Also, a lot of inline code isn't easy for beginning programmers :)

Similar Threads

  1. Load website on a JPanel.
    By Masochist in forum New To Java
    Replies: 2
    Last Post: 05-11-2010, 08:44 PM
  2. load image in the jsp page
    By maneuk in forum Eclipse
    Replies: 0
    Last Post: 04-09-2010, 10:23 PM
  3. ImageIcon won't load my image
    By JaiRaj in forum AWT / Swing
    Replies: 5
    Last Post: 03-04-2010, 07:35 PM
  4. Can not load image in SWT/Eclipse
    By janice in forum SWT / JFace
    Replies: 4
    Last Post: 10-03-2008, 05:53 PM
  5. Help with load image
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:16 AM

Posting Permissions

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