我有一项任务,我正在做的工作,我应该实现并设计一个应用程序来玩一个名为catch the creature的游戏。让生物出现在随机位置然后消失并重新出现在其他地方。目标是通过用鼠标按钮单击该生物来“捕获”该生物。记录生物被捕获的次数。
我需要帮助才能显示作为皮卡丘的JPEG的生物,我尝试过一些东西,但它们都不起作用。感谢您的任何帮助!
主要代码:
import javax.swing.*;
public class Catch_The_Creature
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Catch the Creature");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Creature panel = new Creature();
JOptionPane.showMessageDialog(frame, "Catch Pikachu!");
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
生物代码:
import java.awt.*;
import java.util.Random;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Creature extends JPanel
{
private final int WIDTH = 400, HEIGHT = 300;
private final int DELAY=20, IMAGE_SIZE = 60;
private ImageIcon image;
private int pikachuX, pikachuY;
private int x, y;
private int catchCount=0;
private static Random generator = new Random();
private Timer time;
private ActionListener updater;
private JLabel countLabel;
public Creature()
{
image = new ImageIcon("image/pikachu.jpg");
time = new Timer(DELAY, updater);
addMouseListener ((MouseListener) new MouseClickedListener());
setBackground (Color.green);
setPreferredSize(new Dimension(1900,1000));
time.start();
}
public boolean point(int x, int y)
{
if (x == pikachuX && y == pikachuY)
{
catchCount++;
return true;
}
return false;
}
public int getCatchCount()
{
return catchCount;
}
private class MouseClickedListener extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{
point(event.getX(), event.getY());
}
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.drawImage(image.getImage(),WIDTH, HEIGHT, null);
page.drawString("Pikachus Captured: " + catchCount, 10, 35);
setFont(new Font("Arial", Font.BOLD,35));
}
public void actionPerformed(ActionEvent event)
{
time.setDelay(1000);
x += pikachuX;
y += pikachuY;
if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
pikachuX = pikachuX * -1;
if (y <= 0 || y >= HEIGHT-IMAGE_SIZE)
pikachuY = pikachuY * -1;
repaint();
}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0){}
}
答案 0 :(得分:1)
您似乎没有将ImageIcon
添加到面板或告诉它使用paintComponent()
方法进行绘制。
第一个解决方案 [首选]:将ImageIcon
添加到面板。在构造函数
super.add(image);
确保使用正确的布局(可能是空布局或绝对布局)并更新ImageIcon
本身的坐标,而不仅仅是某些成员变量。
第二个解决方案:在ImageIcon
方法中绘制paintComponent()
。这可能是气馁的,因为它违背了一般的Swing原则。
答案 1 :(得分:1)
确保您的Image文件位于正确的目录中。如果您从netbeans或eclipse运行,您的文件结构应该如下所示
ProjectRoot
src
bin
image
pikachu.jpeg
由于您正在使用"image/pikachu.png"
,因此图像处理器应该是项目根文件夹的子项,因为IDE将首先搜索文件路径
编辑:绘制图像。而不是使用ImageIcon,使用BufferedImage
try {
BufferedImage image = ImageIO.read("image/pikachu.jpeg");
} catch (Exception ex){
ex.printStackTrace();
}
public void paintComponent(Graphics page)
{
super.paintComponent(page);
page.drawImage(image, x, y, heightYouWant, widthYouWant, this);
page.drawString("Pikachus Captured: " + catchCount, 10, 35);
setFont(new Font("Arial", Font.BOLD,35));
}
答案 2 :(得分:0)
我需要做的就是将值放在我希望图片在构造函数中开始的位置。
public Creature()
{
image = new ImageIcon ("pikachu.png");
time = new Timer(DELAY, updater);
x = 0;
y = 50;
addMouseListener ((MouseListener) new MouseClickedListener());
setBackground (Color.green);
setPreferredSize(new Dimension(1900,1000));
time.start();
}
在使用图像图标的同时仍然在绘画组件中绘制图像。
public void paintComponent(Graphics page)
{
super.paintComponent(page);
image.paintIcon (this, page, x, y);
page.drawString("Pikachus Captured: " + catchCount, 10, 35);
setFont(new Font("Arial", Font.BOLD,35));
}