简单的动画在图像上遇到的问题

简单的动画在图像上遇到的问题

问题描述:

我正在尝试使图像(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

+0

啊,你不是正确使用工具包大写。你需要更加小心。 –

+0

不要把你的逻辑放在paintComponent方法中,任何时候绘画都会发生,这可能会破坏你的更新 – MadProgrammer

的错误外星人PNG不言自明:

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 

的外来的变量,因为它是在一个构造函数或方法,而不是在类中声明是不是在程序可见。

很好,我认为这可能是问题的一些东西。 第一个ToolKit.getDefaultToolkit()你reffering到不同的工具包改变,要java.awt.Toolkit.getDefaultToolkit() 第二个是Timer timer = new Timer(50, this);你不能添加当前对象这是一个的JFrame作为定时器构造函数的参数 可以在AnimationDemo实施actionlistner类或者你可以这样来做

Timer timer = new Timer(50,new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       //your action 
      } 
     }); 
    timer.start(); 

第三个是Image alien;它的一个局部变量您CON内部声明它对你的jpanel不可见。声明Image alien;您的JFrame类中(一个实例变量

Image alien; 
public AnimationDemo() { 
alien = ToolKit.getDefaultToolkit().getImage("alien.png"); 
    } 


正如其他人说的错误是自我解释,我已经解决了这个错误并修改你的代码,这将是工作的罚款,现在的图像是移动的,下面是修改
确定公司运作的形象完全限定路径工具包
2.创建MovingMessagePanel对象,并设置外来物体
3.Pass MovingMessagePanel对象定时器
4.In你的构造AnimationDemothis.add(messagePannel);使面板可见

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); 
    } 
} 
+0

我有完整的路径和一切,但仍然没有在屏幕上 –

+0

你可以尝试使用'ImageIO加载图像(如下面的''Toolkit'里面'try {alien = ImageIO.read(new File(“/ /alien.png”));} catch(IOException e){System.out.println(“File Not Found“);}'然后检查你是否能够加载图像 – user6249479