仅在第一次按空调大小按钮时出现空白图像

时间:2013-04-21 12:23:36

标签: java swing bufferedimage imageicon

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

任何人都有任何关于hapening的线索吗?

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);
    }
}

1 个答案:

答案 0 :(得分: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时,im已加载,因为 AWT会自动缓存图片,因此一切正常。

解决方案: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();
    }
}