我正在尝试让图像(alien.png)随机移动到屏幕上,一旦它撞到墙壁就会回来。我实际上有这么多麻烦我只是找不到上传图像并使其反弹的方法。这是我到目前为止,但我遇到了很多错误
package animationdemo;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimationDemo extends JFrame {
public AnimationDemo() {
Image alien;
alien = ToolKit.getDefaultToolkit().getImage("alien.png");
Timer timer = new Timer(50, this);
timer.start();
}
public static void main(String[] args) {
AnimationDemo frame = new AnimationDemo();
frame.setTitle("AnimationDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class MovingMessagePanel extends JPanel implements ActionListener {
public int xCoordinate = 20;
public int yCoordinate = 20;
public int xDir=5;
public int yDir=5;
public void actionPerformed(ActionEvent e) {
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (xCoordinate > getWidth()) xDir*=-1;
if (yCoordinate > getHeight()) yDir*=-1;
if (xCoordinate <0) xDir*=-1;
if (yCoordinate <0) yDir*=-1;
xCoordinate += xDir;
yCoordinate += yDir;
g.drawImage(alien,xCoordinate,yCoordinate,this);
}
}
下面是我得到的一些错误
AnimationDemo.java:18: error: cannot find symbol
alien = ToolKit.getDefaultToolkit().getImage("alien.png");
^
symbol: variable ToolKit
location: class AnimationDemo
AnimationDemo.java:19: error: incompatible types: AnimationDemo cannot be converted to ActionListener
Timer timer = new Timer(50, this);
^
AnimationDemo.java:52: error: cannot find symbol
g.drawImage(alien,xCoordinate,yCoordinate,this);
^
symbol: variable alien
location: class MovingMessagePanel
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
3 errors
我只是不确定为什么即使我导入它也无法找到工具包我也不确定为什么它不能识别g.drawImage上的外星人png
答案 0 :(得分:1)
错误是不言自明的:
AnimationDemo.java:18: error: cannot find symbol alien = ToolKit.getDefaultToolkit().getImage("alien.png"); ^ symbol: variable ToolKit location: class AnimationDemo
您错误地将Toolkit资本化。你必须精确而谨慎地避免这些错误。
AnimationDemo.java:19: error: incompatible types: AnimationDemo cannot be converted to ActionListener Timer timer = new Timer(50, this); ^
AnimationDemo类没有实现ActionListener,所以你不能这样使用它。
AnimationDemo.java:52: error: cannot find symbol g.drawImage(alien,xCoordinate,yCoordinate,this); ^ symbol: variable alien location: class MovingMessagePanel
外来变量在程序中不可见,因为它是在构造函数或方法中声明的,而不是在类中声明。
答案 1 :(得分:1)
我认为可能存在的问题很少。
第一个ToolKit.getDefaultToolkit()
您正在使用不同的工具包将其更改为java.awt.Toolkit.getDefaultToolkit()
第二个是Timer timer = new Timer(50, this);
您无法在Timer构造函数中添加当前对象,这是 jframe作为参数
您可以在actionlistner
课程中实施AnimationDemo
,也可以这样做
Timer timer = new Timer(50,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//your action
}
});
timer.start();
第三个是Image alien;
在构造函数中声明的局部变量,它对您的jpanel不可见。在jframe类中声明Image alien;
(实例变量)
Image alien;
public AnimationDemo() {
alien = ToolKit.getDefaultToolkit().getImage("alien.png");
}
答案 2 :(得分:1)
嗨,正如其他人所说,错误是自我解释的,我已经解决了错误,并修改了你的代码,这将是正常工作,现在图像正在移动,下面是修改
1.为Toolkit提供完全合格的图像路径
2.创建MovingMessagePanel对象并设置外来对象
3.Pass MovingMessagePanel对象到计时器
4.在构造函数AnimationDemo
this.add(messagePannel);
中,以便Panel可见
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class AnimationDemo extends JFrame {
Image alien;
public AnimationDemo() {
alien = Toolkit.getDefaultToolkit().getImage("/<Fully Qualified Path>/alien.png");
MovingMessagePanel messagePannel = new MovingMessagePanel();//Pass this object to Timer
messagePannel.alien = this.alien;
Timer timer = new Timer(50, messagePannel);
timer.start();
//Add MovingMessagePanel object to JFrame then only it will be visible
this.add(messagePannel);
}
public static void main(String[] args) {
AnimationDemo frame = new AnimationDemo();
frame.setTitle("AnimationDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
class MovingMessagePanel extends JPanel implements ActionListener {
public int xCoordinate = 20;
public int yCoordinate = 20;
public int xDir=5;
public int yDir=5;
public Image alien;//initialize this with the image
public void actionPerformed(ActionEvent e) {
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (xCoordinate > getWidth()) xDir*=-1;
if (yCoordinate > getHeight()) yDir*=-1;
if (xCoordinate <0) xDir*=-1;
if (yCoordinate <0) yDir*=-1;
xCoordinate += xDir;
yCoordinate += yDir;
g.drawImage(alien,xCoordinate,yCoordinate,this);
}
}