importing jpegs/bmps onto jPanels


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: importing jpegs/bmps onto jPanels

  1. #1
    Join Date
    Nov 2004
    Posts
    9

    importing jpegs/bmps onto jPanels

    Hi, I'm designing a GUI which consists of various frames, some of which have Jpanels on them. My question is, how do I go about importing a jpeg onto that panel. I know I need to use ImageIcon, but I have no idea where to start with regards to code. Any help would be great!!!

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You don't _import_ images into panels, you read the image files (FileInputStream as bytes), convert the contents to Image, override the paint method of the coponent that will render the image and do a g.drawImage there. If you plan on doing animation (fast image sequences) you'll have to override the update method of the component, using a second image (in memory) that you draw on, and draw this image in the update method. If you don't the animation will look like a bad tv reception.
    eschew obfuscation

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Here's how you read an image file and convert it to an image:

    import java.awt.Image;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    public class GetImageFromFile {

    public Image getImageData (String filePath, JFrame pFrame) {
    try {
    FileInputStream fi=new FileInputStream(filePath);
    byte [] imageBytes=new byte[fi.available()];
    fi.read(imageBytes);
    fi.close();
    Image img=Toolkit.getDefaultToolkit().createImage(imageBytes);
    MediaTracker mt=new MediaTracker(pFrame);
    mt.addImage(img,0);
    try {
    mt.waitForID(0); // cant use it until it's ready....
    } catch (InterruptedException ex) {
    ex.printStackTrace();

    return null;
    }
    return img;
    } catch (IOException ex) {
    ex.printStackTrace();
    return null;
    }
    }
    }
    eschew obfuscation

  4. #4
    Join Date
    Nov 2004
    Posts
    9
    Thanks for your help! Now that I can convert the image file into an actual image, how can I paint that image onto a JPanel in one of my frames-i.e what code do I need to use?

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You override the components paint method.

    Check my answer on another paint problem.

    If you just want to paint an image you don't have to go as far as in that example. Just do a plain drawImage in the paint method;

    g.drawImage(img, 0,0, thePanel);

    Just to state the obvious, you have to extend the component type that you want to paint in.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center