2

好的,我编译代码并运行程序,图像应该出现在屏幕中央。我将尺寸放在 JTextFields 中,然后按调整大小按钮。一切正常。现在,如果我关闭窗口并再次运行,图像就会出现,但是如果我第一次按下调整大小按钮,它就会消失,如果我再次按下它,它就会出现并且一切正常。只有当我在窗口运行时编译代码并运行时,我才没有那个小问题。

有人知道发生了什么吗?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class ImageApp extends JFrame implements ActionListener{
//*********************************Variables*********************************
//***************************************************************************
    private JButton align_l,align_c,align_r,rsz;
    private JLabel w,h,pic;
    private JTextField w_txt,h_txt;
    private static final String IMG_PATH = "./hamster.jpg";
    private static int img_w,img_h;
    private static ImageIcon icon;
    private JMenuItem reset_option,double_option;
//********************************Constructor********************************
//***************************************************************************
    public ImageApp(String title){
        super(title);
        //****************************Align Panel****************************
        align_l = new JButton("Align Left");
        align_c = new JButton("Align Center");
        align_r = new JButton("Align Right");
        align_l.addActionListener(this);
        align_c.addActionListener(this);
        align_r.addActionListener(this);
        JPanel align_panel = new JPanel();
        align_panel.setLayout(new GridLayout(1,3));
        align_panel.add(align_l);
        align_panel.add(align_c);
        align_panel.add(align_r);
        add("North",align_panel);
        //***************************Picture Label***************************
        try {
             BufferedImage  img = ImageIO.read(new File(IMG_PATH));
             img_w = img.getWidth();
             img_h = img.getHeight();
             ImageIcon icon = new ImageIcon(img);
             pic = new JLabel(icon);
             add(pic);
        } catch (IOException e) {
                e.printStackTrace();
        }
        //****************************Resize Panel***************************
        w = new JLabel("Width:");
        h = new JLabel("Height:");
        w_txt = new JTextField(String.valueOf(img_w),4);
        h_txt = new JTextField(String.valueOf(img_h),4);
        rsz = new JButton("Resize");
        rsz.addActionListener(this);
        JPanel resize_panel = new JPanel();
        resize_panel.setLayout(new GridLayout(3,2));
        resize_panel.add(w);
        resize_panel.add(w_txt);
        resize_panel.add(h);
        resize_panel.add(h_txt);
        resize_panel.add(rsz);
        add("South",resize_panel);
        //*************************Menu Options Panel************************
        JMenuBar menu_bar = new JMenuBar();
        setJMenuBar(menu_bar);
        JMenu options_menu = new JMenu("Options");
        menu_bar.add(options_menu);
        JMenuItem reset_option = new JMenuItem("Reset");
        JMenuItem double_option = new JMenuItem("Double");
        options_menu.add(reset_option);
        options_menu.add(double_option);
        reset_option.addActionListener(this);
        double_option.addActionListener(this);
       //********************************************************************
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }
//**********************************Methods**********************************
//***************************************************************************
    public void actionPerformed(ActionEvent e){
        //Object b = e.getSource();
        String b = e.getActionCommand();
        if (b.equals("Align Left")){pic.setHorizontalAlignment(SwingConstants.LEFT);}
        else if (b.equals("Align Center")){pic.setHorizontalAlignment(SwingConstants.CENTER);}
        else if (b.equals("Align Right")){pic.setHorizontalAlignment(SwingConstants.RIGHT);}
        else if (b.equals("Resize")){
            int wid,hei;
            try{
                wid=Integer.parseInt(w_txt.getText());
                hei=Integer.parseInt(h_txt.getText());
            }catch(NumberFormatException nfe){
                wid=img_w;
                hei=img_h;
            }
            if(wid == 0 || hei == 0){
                wid=img_w;
                hei=img_h;
            }
            if(wid > 4096){
                wid=4096;
            }
            if( hei > 2048){
                hei=2048;
            }
            resize_image(wid,hei);
        }
        else if (b.equals("Reset")){
            resize_image(img_w,img_h);
        }
        else if (b.equals("Double")){
            Icon newIcon = pic.getIcon();
            int wid = 2 * newIcon.getIconWidth();
            int hei = 2 * newIcon.getIconHeight();
            if(wid > 4096){
                wid=4096;
            }
            if( hei > 2048){
                hei=2048;
            }
            resize_image(wid,hei);
        }
    }
    public void resize_image(int w,int h){
            Image im = Toolkit.getDefaultToolkit().getImage(IMG_PATH);
            BufferedImage b_i = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            Graphics g = b_i.createGraphics();
            g.drawImage(im, 0, 0, w, h, null);
            ImageIcon newIcon = new ImageIcon(b_i);
            pic.setIcon(newIcon);
            w_txt.setText(String.valueOf(w));
            h_txt.setText(String.valueOf(h));
    }
    public static void main(String [] args){
        ImageApp i = new ImageApp("Image Application.");
        i.setSize(800,600);
        i.setLocationRelativeTo(null);
        i.setVisible(true);
    }
}
4

1 回答 1

3

resize_image

public void resize_image(int w, int h) {
    Image im = Toolkit.getDefaultToolkit().getImage(IMG_PATH);
    /* ... */
    g.drawImage(im, 0, 0, w, h, null);
    /* ... */
}

第一次getImage被调用,im并没有真正加载。当drawImage被调用时,AWT 开始在另一个线程中加载图像。在加载图像之前它不会阻塞。而且,因为drawImage认为图像没有完全加载,所以它什么都没有。

在第二次getImage调用时,由于AWT 会自动缓存图像im,所以已经加载完毕,所以一切正常。

解决方案:MediaTracker

public void resize_image(int w, int h) {
    Image im = Toolkit.getDefaultToolkit().getImage(IMG_PATH);
    waitForImage(im);
    /* ... */
    g.drawImage(im, 0, 0, w, h, null);
    /* ... */
}

private MediaTracker tracker = new MediaTracker(this);

private void waitForImage(Image image){
    tracker.addImage(image, 0);
    try {
        tracker.waitForID(0);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
于 2013-04-21T12:52:03.473 回答