java - 仅当我第一次按下调整大小按钮时为空白图像

标签 java swing bufferedimage imageicon

好的,我编译代码并运行程序,图像按应有的方式显示在屏幕中央。我将尺寸放入 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);
    }
}

最佳答案

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

关于java - 仅当我第一次按下调整大小按钮时为空白图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16131060/

相关文章:

java - 如何逐行拆分文件内容并保存到List

java - JLS : Item 54 - Why classes having thread pool should not implement serializable?

java - 如何将应用程序存储在SD卡中

java对移动物体的高效重画

Java KeyListener 断断续续

java - 以阻塞方式轻松在屏幕上打印图像以进行调试

Java - BufferedImage 未出现在 JFrame 上

java - 获取 Java 中默认系统信任库的 SSLContext(JSEE)

java - java 错误中的关键事件

java - 如何在 Android Studio 中导入 java.awt.image.BufferedImage