Reputation: 25
Im currently trying to make an image (alien.png) to move around the screen randomly and once it hits the walls it comes back. Im actually have so much trouble with this I just can't find a way to upload the image and make it bounce around. this is what I have so far but I'm getting a lot of errors
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);
}
}
Heres some of the errors I get
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
I'm just not sure why it can't find the toolkit even thought I imported it also I'm not sure why its not recognizing the alien png on the g.drawImage
Upvotes: 0
Views: 68
Reputation: 11
Hi
As others said that The errors are self explanatory, i have resolved the errors and modified your code which will be working fine, now the image is moving , the below are modifications
1.Give fully qualified path of image to Toolkit
2.Create MovingMessagePanel object and set alien object
3.Pass MovingMessagePanel object to Timer
4.In your constructor AnimationDemo
this.add(messagePannel);
so that Panel is visible
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);
}
}
Upvotes: 1
Reputation: 2989
well theres few things which i think might be the problem.
first one ToolKit.getDefaultToolkit()
you are reffering to different toolkit change that to java.awt.Toolkit.getDefaultToolkit()
second one is Timer timer = new Timer(50, this);
you cant add your current object which is a jframe as a parameter in the Timer constructor
you can implement actionlistner
in the AnimationDemo
class or you can do it this way
Timer timer = new Timer(50,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//your action
}
});
timer.start();
third one is Image alien;
its a local variable declared inside your constructor it is not visible to your jpanel. declare Image alien;
inside your jframe class(a instance variable)
Image alien;
public AnimationDemo() {
alien = ToolKit.getDefaultToolkit().getImage("alien.png");
}
Upvotes: 1
Reputation: 285405
The errors are self explanatory:
AnimationDemo.java:18: error: cannot find symbol alien = ToolKit.getDefaultToolkit().getImage("alien.png"); ^ symbol: variable ToolKit location: class AnimationDemo
You're capitalizing Toolkit wrong. You have to be precise and careful to avoid these errors.
AnimationDemo.java:19: error: incompatible types: AnimationDemo cannot be converted to ActionListener Timer timer = new Timer(50, this); ^
AnimationDemo class does not implement ActionListener, and so you can't use it as such.
AnimationDemo.java:52: error: cannot find symbol g.drawImage(alien,xCoordinate,yCoordinate,this); ^ symbol: variable alien location: class MovingMessagePanel
The alien variable is not visible in the program because it is declared in a constructor or method and not in the class.
Upvotes: 1