让自己快乐的面孔出现在Java中

问题描述 投票:0回答:1

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

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

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();
        }
    }
}
java swing embedded-resource imageicon
1个回答
2
投票

ReboundPanel班,

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

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

enter image description here

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

© www.soinside.com 2019 - 2024. All rights reserved.