0,0 → 1,144 |
package ak.resizeapplet; |
|
import javax.swing.*; |
import java.awt.*; |
import java.awt.event.*; |
import java.awt.image.*; |
import java.util.*; |
import java.io.*; |
|
public class ResizeApplet extends JApplet |
{ |
private final static int thumbnailWidth = 200; |
private final static int thumbnailHeight = 150; |
|
private JLabel fileLabel; |
private JTextField fileField; |
private JButton browseButton; |
private JButton loadButton; |
private JLabel imagePanel; |
|
private ImageResizer resizer = new JiuResizer(); |
private ImageIcon thumbnail = new ImageIcon(); |
|
private void createGUI() |
{ |
setLayout(new GridBagLayout()); |
GridBagConstraints c = new GridBagConstraints(); |
|
fileLabel = new JLabel("File"); |
fileLabel.setDisplayedMnemonic('f'); |
c.gridx = 0; c.gridy = 0; c.weightx = 0.0; c.weighty = 0.0; c.fill = GridBagConstraints.NONE; |
add(fileLabel, c); |
|
fileField = new JTextField(); |
c.gridx = 1; c.gridy = 0; c.weightx = 0.7; c.weighty = 0.0; c.fill = GridBagConstraints.HORIZONTAL; |
add(fileField, c); |
fileLabel.setLabelFor(fileField); |
|
browseButton = new JButton("Browse"); |
browseButton.setMnemonic('b'); |
browseButton.addActionListener(new ActionListener() { |
public void actionPerformed(ActionEvent e) { |
browseButtonClicked(); |
} |
} ); |
c.gridx = 2; c.gridy = 0; c.weightx = 0.1; c.weighty = 0.0; c.fill = GridBagConstraints.HORIZONTAL; |
add(browseButton, c); |
|
loadButton = new JButton("Load"); |
loadButton.setMnemonic('l'); |
loadButton.addActionListener(new ActionListener() { |
public void actionPerformed(ActionEvent e) { |
loadButtonClicked(); |
} |
} ); |
c.gridx = 3; c.gridy = 0; c.weightx = 0.1; c.weighty = 0.0; c.fill = GridBagConstraints.HORIZONTAL; |
add(loadButton, c); |
|
imagePanel = new JLabel(); |
imagePanel.setBorder(BorderFactory.createLineBorder(Color.black)); |
imagePanel.setMinimumSize(new Dimension(thumbnailWidth, thumbnailHeight)); |
imagePanel.setPreferredSize(new Dimension(thumbnailWidth, thumbnailHeight)); |
c.gridx = 0; c.gridy = 1; c.weightx = 0.0; c.weighty = 0.9; c.fill = GridBagConstraints.NONE; |
c.gridwidth = 4; |
add(imagePanel, c); |
} |
|
public void init() |
{ |
try { |
createGUI(); |
} |
catch(Exception ex) { |
System.err.println("Cannot create GUI: " + ex.getMessage()); |
} |
} |
|
private void browseButtonClicked() |
{ |
try { |
JFileChooser chooser = new JFileChooser(); |
ExtFileFilter filter = new ExtFileFilter("Images"); |
|
filter.addExtension("jpg"); |
filter.addExtension("jpeg"); |
filter.addExtension("gif"); |
chooser.setFileFilter(filter); |
|
chooser.setSelectedFile(new File(fileField.getText())); |
if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { |
fileField.setText(chooser.getSelectedFile().getAbsolutePath()); |
loadButtonClicked(); |
} |
} |
catch(Exception ex) { |
JOptionPane.showMessageDialog(this, "Cannot browse files: " + ex.getMessage(), |
"ResizeApplet", JOptionPane.ERROR_MESSAGE); |
} |
} |
|
private void loadButtonClicked() |
{ |
try { |
thumbnail.setImage( |
resizer.resize(loadImage(fileField.getText()), thumbnailWidth, thumbnailHeight)); |
imagePanel.setIcon(thumbnail); |
} |
catch(Exception ex) { |
JOptionPane.showMessageDialog(this, "Cannot browse files: " + ex.getMessage(), |
"ResizeApplet", JOptionPane.ERROR_MESSAGE); |
} |
} |
|
private void sendToServer() |
{ |
//javax.imageio.ImageIO.write(thumbnail.getImage(), "jpg", outputStream); |
} |
|
protected Image loadImage(String fileName) |
throws Exception |
{ |
Toolkit toolkit = Toolkit.getDefaultToolkit(); |
Image image = toolkit.createImage(fileName); |
|
toolkit.prepareImage(image, -1, -1, null); |
|
while(true) { |
int status = toolkit.checkImage(image, -1, -1, null); |
|
if((status & ImageObserver.ALLBITS) != 0) break; |
if((status & ImageObserver.ERROR) != 0) throw new Exception("Cannot read the image"); |
|
try { |
Thread.sleep(100); |
} |
catch(Exception ex) { |
throw new Exception("Cannot read the image"); |
} |
} |
|
return image; |
} |
} |
|