Canvas +a picture

How would I go about creating a Canvas inside of my JFrame with the picture named "Track"?Your help is greatly appreciated. Also, should the picture be a .JPG or .GIF?Message was edited by: Rovert
[231 byte] By [Roverta] at [2007-11-27 6:24:22]
# 1

In Swing (components with J prefix) it is better not to use a Canvas which is an AWT, heavyweight component. Use a JPanel instead. See [url=http://java.sun.com/products/jfc/tsc/articles/mixing/]Mixing Heavy and Light Components[/url] for details.

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class SimpleImage {

public static void main(String[] args) throws IOException {

// The images folder is in the current directory.

String path = "images/Bird.gif"; // jpg, png and bmp okay

BufferedImage image = ImageIO.read(new File(path));

JLabel label = new JLabel(new ImageIcon(image));

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().setLayout(new GridLayout(1,0));

f.getContentPane().add(label);

f.getContentPane().add(new BirdPanel(image));

f.pack();

f.setVisible(true);

}

}

class BirdPanel extends JPanel {

BufferedImage image;

public BirdPanel(BufferedImage image) {

this.image = image;

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(image, 0, 0, this);

}

}

crwooda at 2007-7-12 17:43:10 > top of Java-index,Security,Cryptography...