View Single Post
  #4 (permalink)  
Old 09-22-2007, 12:51 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
// <applet code="AppletImageTest" width="400" height="400"></applet> import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.*; import javax.imageio.ImageIO; public class AppletImageTest extends Applet implements ActionListener { AppletImageCanvas canvas = new AppletImageCanvas(); FileDialog fileDialog; public void init() { ScrollPane scrollPane = new ScrollPane(); scrollPane.add(canvas); setLayout(new BorderLayout()); add(scrollPane); add(getUIPanel(), "Last"); } public void actionPerformed(ActionEvent e) { showDialog(); String folder = fileDialog.getDirectory(); String fileName = fileDialog.getFile(); if(folder != null && fileName != null) { File file = new File(folder, fileName); System.out.println("file = " + file.getPath()); BufferedImage image = loadImage(file); canvas.setImage(image); canvas.getParent().validate(); } } private void showDialog() { fileDialog = new FileDialog(new Frame()); // Filter does not work in windows os - see api. fileDialog.setFilenameFilter(new ImageFileFilter()); fileDialog.pack(); fileDialog.setVisible(true); } private BufferedImage loadImage(File file) { BufferedImage image = null; try { URL url = file.toURL(); image = ImageIO.read(url); } catch(MalformedURLException e) { System.out.println("URL error: " + e.getMessage()); } catch(IOException e) { System.out.println("IO error: " + e.getMessage()); } return image; } private Panel getUIPanel() { Button button = new Button("open"); button.addActionListener(this); Panel panel = new Panel(); panel.add(button); return panel; } public static void main(String[] args) { Applet applet = new AppletImageTest(); Frame f = new Frame(); f.addWindowListener(closer); f.add(applet); f.setSize(400,400); f.setLocation(200,200); applet.init(); f.setVisible(true); } private static WindowListener closer = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; } class AppletImageCanvas extends Canvas { BufferedImage image; public void paint(Graphics g) { if(image != null) { int x = (getWidth() - image.getWidth())/2; int y = (getHeight() - image.getHeight())/2; g.drawImage(image, x, y, this); } } public Dimension getPreferredSize() { if(image == null) return new Dimension(100,100); return new Dimension(image.getWidth(), image.getHeight()); } public void setImage(BufferedImage image) { this.image = image; invalidate(); repaint(); } } class ImageFileFilter implements FilenameFilter { final static String BMP = "bmp"; final static String GIF = "gif"; final static String JPG = "jpg"; final static String JPEG = "jpeg"; final static String PNG = "png"; public boolean accept(File dir, String name) { String ext = getExtension(name); return ext.equals(BMP) || ext.equals(GIF) || ext.equals(JPG) || ext.equals(JPEG) || ext.equals(PNG); } private String getExtension(String fileName) { int dot = fileName.lastIndexOf("."); if(dot > -1 && dot < fileName.length()-1) return fileName.substring(dot+1).toLowerCase(); return null; } }
Edit: add these two lines
Code:
canvas.getParent().validate(); invalidate();
to validate scrollPane after setting image.

Last edited by hardwired : 09-22-2007 at 03:17 AM. Reason: add validation code
Reply With Quote