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.

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){
    public client() {
        super(new BorderLayout());

        log = new JTextArea(5,20);
        log.setMargin(new Insets(5,5,5,5));
        JScrollPane logScrollPane = new JScrollPane(log);

        fc = new JFileChooser();

        openButton = new JButton("Open a File...");

        JPanel buttonPanel = new JPanel(); 
        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);
    private static void createAndShowGUI() {
        JFrame frame = new JFrame("FileChooserDemo");

        frame.add(new client());

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                UIManager.put("swing.boldMetal", Boolean.FALSE); 
This article has been dead for over six months. Start a new discussion instead.