java - 在 Java 中显示一张笑脸

标签 java swing embedded-resource imageicon

我在 Java GUI 中显示图像文件 src/happyFace.gif 时遇到问题。目标是显示一张笑脸图像,该图像似乎以一定角度滑过程序窗口,并从窗口边缘弹起。

我认为我的问题在于 src/ReboundPanel.java 中的图像变量(类型 ImageIcon),因为 ImageIcon 类可能与 future 的 swing 版本不兼容(根据 Oracle 的文档: https://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html )。如果这是真的,我认为 Swing 库可能无法支持 ImageIcon 类。我不知道如何检查我的 Swing 库。

src/happyFace.gif

enter image description here

我的输出窗口

enter image description here

src/Rebound.java:

//********************************************************************
// Rebound.java Java Foundations
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Rebound{
//-----------------------------------------------------------------
// Displays the main frame of the program.
//-----------------------------------------------------------------
    public static void main (String[] args){
        JFrame frame = new JFrame ("Rebound");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ReboundPanel());
        frame.pack();
        frame.setVisible(true);
    }
}

src/ReboundPanel.java:

//********************************************************************
// ReboundPanel.java Java Foundations
//
// Represents the primary panel for the Rebound program.
//********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ReboundPanel extends JPanel{
    private final int WIDTH = 300, HEIGHT = 100;
    private final int DELAY = 20, IMAGE_SIZE = 35;
    private ImageIcon image;
    private Timer timer;
    private int x, y, moveX, moveY;
    //-----------------------------------------------------------------
    // Sets up the panel, including the timer for the animation.
    //-----------------------------------------------------------------
    public ReboundPanel(){
        timer = new Timer(DELAY, new ReboundListener());
        image = new ImageIcon ("happyFace.gif");
        x = 0;
        y = 40;
        moveX = moveY = 3;
        setPreferredSize (new Dimension(WIDTH, HEIGHT));
        setBackground (Color.black);
        timer.start();
    }
    //-----------------------------------------------------------------
    // Draws the image in the current location.
    //-----------------------------------------------------------------
    public void paintComponent (Graphics page){
        super.paintComponent (page);
        image.paintIcon (this, page, x, y);
    }
    //*****************************************************************
    // Represents the action listener for the timer.
    //*****************************************************************
    private class ReboundListener implements ActionListener{
        //-----------------------------------------------------------------
        // Updates the position of the image and possibly the direction
        // of movement whenever the timer fires an action event.
        //-----------------------------------------------------------------
        public void actionPerformed (ActionEvent event){
            x += moveX;
            y += moveY;
            if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
                moveX = moveX * -1;
            if (y <= 0 || y >= HEIGHT-IMAGE_SIZE)
                moveY = moveY * -1;
            repaint();
        }
    }
}

最佳答案

ReboundPanel类中,

更改image = new ImageIcon("happyFace.gif");

image = new ImageIcon("src/happyFace.gif");

enter image description here

请注意,这种解决方案只能用于测试目的。如 Andrew Thompson 中所述的评论,存储和加载图像的正确方法是使用 embedded resource .

关于java - 在 Java 中显示一张笑脸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021588/

相关文章:

multithreading - AWT-EventQueue 线程和 AWT-Shutdown 线程未关闭

c++ - cmake 运行 xxd 作为自定义命令

java - 从 .jar 文件获取图像

java - 如何按每个字符串的第二个字符对字符串数组进行排序?

java - 单线程上下文中易失可变字段的风险?

java - 当嵌套枚举在其构造函数中引用父静态成员时,为什么会出现 NPE?

java - 我可以在一个 SwingWorker 中运行另一个 SwingWorker 吗?

java - 为什么我在添加 setLayout(null) 时失去视觉效果?

java - setMaxForRoute 在 ThreadSafeClientConnManager 中不起作用

c# - 如何为dll文件设置自定义图标?