Problem with loading an image to a Panel

Discussion in 'Java' started by icepac, Jun 21, 2006.

  1. icepac

    icepac Guest

    Salutations everybody. I want to load an image into a panel. I have
    sub-classed the JPanel class in order to be able to override the pain
    method, like this :

    public class ImageCanvas
    extends JPanel {
    Image image;
    public ImageCanvas() {
    super();
    this.setBackground(Color.white);
    }
    public void paint(Graphics g) {
    if (image!=null)
    {
    g.drawImage(image, 0, 0, this);
    }
    }
    public void SetImage(Image img)
    {
    image = img;
    int iW = Math.min(image.getWidth(this),this.getWidth());
    int iH = Math.min(image.getHeight(this), this.getHeight());

    this.setSize(iW>>1,iH>>1);
    //this.setSize(image.getWidth(this)>>1,image.getHeight(this)>>1);
    setVisible(true);
    this.repaint();
    }

    Now I use this code in order to load my image into a normal jPanel :
    (this is the snippet of code that executes loading the image :

    //Display images
    Image img =
    Toolkit.getDefaultToolkit().createImage(strFilePath);//strFilePath of
    course contains the path where the "image.png" is located
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(img, 0);
    try
    {
    tracker.waitForAll();
    }
    catch (InterruptedException exI)
    {
    System.out.println("Error while waiting tracker"+
    exI.getMessage());
    }
    jCanvas.setMaximumSize(jPanelImg.getSize());
    jCanvas.SetImage(img);
    jPanelImg.revalidate(); //jPanelImg (type = JPanel) contains
    jCanvas which is of type //ImageCanvas
    jPanelImg.repaint();


    However, when I use this program it loads the image all right but the
    background is full of trash. Also if I try to load multiple images
    then they all get jumbled together...
    How can I clean this up ?

    Thanx alot !
    Pascal
    icepac, Jun 21, 2006
    #1
    1. Advertising

  2. icepac

    Andrew T. Guest

    icepac wrote:
    > Salutations everybody. I want to load an image into a panel. I have
    > sub-classed the JPanel class in order to be able to override the pain


    'paint'

    > method, like this :
    >
    > public class ImageCanvas
    > extends JPanel {

    ....
    > public void paint(Graphics g) {
    > if (image!=null)

    ....

    In Swing components you should override paintComponent(),
    rather than paint().

    BTW there is a more specialised GUI group..
    http://groups.google.com/group/comp.lang.java.gui

    HTH

    Andrew T.
    Andrew T., Jun 21, 2006
    #2
    1. Advertising

  3. Andrew T. wrote:
    > icepac wrote:
    >> Salutations everybody. I want to load an image into a panel. I have
    >> sub-classed the JPanel class in order to be able to override the pain

    >
    > 'paint'
    >
    >> method, like this :
    >>
    >> public class ImageCanvas
    >> extends JPanel {

    > ...
    >> public void paint(Graphics g) {
    >> if (image!=null)

    > ...
    >
    > In Swing components you should override paintComponent(),
    > rather than paint().
    >
    > BTW there is a more specialised GUI group..
    > http://groups.google.com/group/comp.lang.java.gui
    >
    > HTH
    >
    > Andrew T.
    >


    You don't need a MediaTracker if you are going to use the Image
    Observer/Producer. It does help if you don't know the size of the image
    and need to prepare for it dynamically however.

    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;

    public class ImageObserverExample extends Panel {
    Image image;
    String msg = "Loading Image";
    int n;

    public ImageObserverExample() {
    try {
    URL url = new URL(
    "http://www.thealpacastore.com/alpacacam/latest640.jpg");
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    // use createImage instead of getImage - see api docs for why
    image = toolkit.createImage(url);
    } catch (MalformedURLException murle) {
    msg = "MalformedURLException";
    }

    setPreferredSize(new Dimension(400,300));
    }

    public void paint(Graphics g) {
    // show the number of calls to paint()
    System.out.println(++n);
    // if there is an image and drawing has not finished
    // note: if the image is null drawImage will return true
    if (image == null ||
    !g.drawImage(image,0,0,getWidth(),getHeight(),this)) {
    // draw the message
    g.drawString(msg,20,getHeight()/2);
    System.out.println(msg);
    }
    }

    public static void main(String[] args) {
    Frame frame = new Frame("ImageObserverExample");
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we) {
    System.exit(0);
    }
    });
    ImageObserverExample examp = new ImageObserverExample();
    frame.add(examp,BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Jun 21, 2006
    #3
  4. icepac

    icepac Guest

    Thank you alot everybody...
    I found my problem. I am replying in order to give those who have the
    same problem the answer:
    When you override the paint or paintComponent method you need to make
    sure that you still call the super.paint or super.paintComponent. This
    is what you need to fill out your image background in order not to have
    too much static...

    so the method should go like

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image!=null)
    {
    g.drawImage(image, 0, 0, this);
    }
    }


    And it is indeed better to use paintComponent in Swing ...

    C you around ! (Also thanx for redirecting me in the GUI specialised
    thread)

    P.S. : Last question, is there a thread specifically used for JFM and
    the MediaPlayer ?

    Pascal
    icepac, Jun 21, 2006
    #4
  5. icepac

    Andrew T. Guest

    icepac wrote:
    ...
    > P.S. : Last question,


    ...yeah, yeah - that's what they all say. ;-)

    >..is there a thread specifically used for JFM and
    > the MediaPlayer ?


    You mean the JMF? You might try the forum here..
    http://forum.java.sun.com/forum.jspa?forumID=28

    HTH

    Andrew T.
    Andrew T., Jun 21, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harry
    Replies:
    3
    Views:
    529
    Harry Singh
    Nov 10, 2003
  2. HS1
    Replies:
    3
    Views:
    474
  3. HS1
    Replies:
    4
    Views:
    8,282
    Babu Kalakrishnan
    Dec 8, 2004
  4. JC
    Replies:
    1
    Views:
    737
    Michael Rauscher
    Aug 9, 2006
  5. edfialk
    Replies:
    0
    Views:
    179
    edfialk
    May 10, 2007
Loading...

Share This Page