View Single Post
  #2 (permalink)  
Old 03-23-2008, 09:50 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Some resource links:
How to Use Icons
Trail: 2D Graphics
Code:
import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class LoadAndShow extends JPanel { BufferedImage image; Dimension size = new Dimension(); public LoadAndShow(BufferedImage image) { this.image = image; size.setSize(image.getWidth(), image.getHeight()); } /** * Drawing an image can allow for more * flexibility in processing/editing. */ protected void paintComponent(Graphics g) { // Center image in this component. int x = (getWidth() - size.width)/2; int y = (getHeight() - size.height)/2; g.drawImage(image, x, y, this); } public Dimension getPreferredSize() { return size; } public static void main(String[] args) throws IOException { String path = "images/hawk.jpg"; BufferedImage image = ImageIO.read(new File(path)); LoadAndShow test = new LoadAndShow(image); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new JScrollPane(test)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); //showIcon(image); } /** * Easy way to show an image: load it into a JLabel * and add the label to a container in your gui. */ private static void showIcon(BufferedImage image) { ImageIcon icon = new ImageIcon(image); JLabel label = new JLabel(icon, JLabel.CENTER); JOptionPane.showMessageDialog(null, label, "icon", -1); } }
Reply With Quote