djangofan
djangofan

Reputation: 29669

Load image in a JPanel?

I can't figure out why this code wont work. Any ideas?

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.image.ImageObserver;
import java.net.URL;

import javax.swing.JPanel;

public class ImageTool extends JPanel {

  private static final long serialVersionUID = 1L;
  private static Image image;

  public ImageTool(URL url) {       
    image = Toolkit.getDefaultToolkit().getImage(url);
    rightSize();
  }

  private void rightSize() {
    int width = image.getWidth(this);
    int height = image.getHeight(this);
    if (width == -1 || height == -1)
      return;
    addNotify();
    System.out.println("Image width: "+width);
    System.out.println("Image height"+height);

  }

  public boolean imageUpdate(Image img, int infoflags, int x, int y,
      int width, int height) {
    if ((infoflags & ImageObserver.ERROR) != 0) {
      System.out.println("Error loading image!");
      System.exit(-1);
    }
    if ((infoflags & ImageObserver.WIDTH) != 0
        && (infoflags & ImageObserver.HEIGHT) != 0) {
      rightSize();
      System.out.println("1");
    }
    if ((infoflags & ImageObserver.SOMEBITS) != 0)
      repaint();
    if ((infoflags & ImageObserver.ALLBITS) != 0) {
        System.out.println("2");
      rightSize();
      repaint();
      return false;
    }
    return true;
  }

  public void update(Graphics g) {
    paint(g);
  }

  public void paintComponent(Graphics g) {
    Insets insets = getInsets();
    g.drawImage(image, insets.left, insets.top, this);
  }
  public static void main(String[] args) throws Exception {
    String url = "http://www.java2s.com/style/logo.png";
    new ImageTool(new URL(url));

  }
}

Upvotes: 1

Views: 4687

Answers (2)

camickr
camickr

Reputation: 324118

I'm not sure what you are trying to do either, but your code looks like an old AWT example and should not be used for Swing.

  1. there is no need to override update()
  2. paintComponent() should invoke super.paintComponent()

Read the Swing tutorial on How to Use Icons for example code that uses images on labels.

Upvotes: 2

WhiteFang34
WhiteFang34

Reputation: 72039

In your code you're missing a JFrame or JDialog to contain your JPanel in. Here's an example that I believe does what you're asking for. It loads the same image into a visible window and outputs the dimensions to the console.

public class ImageTool extends JPanel {
    public ImageTool(URL url) {
        ImageIcon icon = new ImageIcon(url);
        JLabel label = new JLabel(icon, JLabel.CENTER);
        add(label);

        System.out.println("Image width: " + icon.getIconWidth());
        System.out.println("Image height: " + icon.getIconHeight());
    }

    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("http://www.java2s.com/style/logo.png");
        JPanel panel = new ImageTool(url);
        JFrame frame = new JFrame();
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

Upvotes: 2

Related Questions