ok the other day i posted about how to actually display an image and then what the best way would be to read in a location of an image and then display it.
Now my new question is how do you resize an image. At the moment the image i display is huge, you can only see about half of it when the screen is maximized. How would i set the size to be smaller. Also another problem, which probably has to do with the way i did it is that the image doesnt appear untili maximize the screen. Could you guys look at this code and tell me what you think.
Thanks.
package ImageReader;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import java.awt.image.*;
import javax.imageio.*;
public class client extends JPanel
implements ActionListener {
static private final String newline = "\n";
BufferedImage img;
JButton openButton;
JTextArea log;
JFileChooser fc;
public void paint(Graphics g){
g.drawImage(img,0,0,this);
}
public client() {
super(new BorderLayout());
log = new JTextArea(5,20);
log.setMargin(new Insets(5,5,5,5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
fc = new JFileChooser();
openButton = new JButton("Open a File...");
openButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(openButton);
add(buttonPanel, BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(client.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String path = file.getPath();
try {
img = ImageIO.read(new File(path));
} catch (IOException e1) {
}
} else {
log.append("Open command cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("FileChooserDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new client());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
}