I am trying to learn about 2D animation and I'm having a problem loading my image.
The program is supposed to display a black background and display star.png.
Here is my code
package star;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
Image star;
Timer timer;
int x, y;
public Board() {
setBackground(Color.BLACK);
ImageIcon ii =
new ImageIcon(this.getClass().getResource("star.png"));
star = ii.getImage();
setDoubleBuffered(true);
x = y = 10;
timer = new Timer(25, this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(star, x, y, this);
Toolkit.getDefaultToolkit().sync();
}
public void actionPerformed(ActionEvent e) {
x += 1;
y += 1;
if (y > 240) {
y = -45;
x = -45;
}
repaint();
}
}
When I use
public Board() {
setBackground(Color.BLACK);
ImageIcon ii =
new ImageIcon(this.getClass().getResource("star.png"));
star = ii.getImage();
it gives me an error and it won't run
and when I use
public Board() {
setBackground(Color.BLACK);
ImageIcon ii =
new ImageIcon("star.png");
star = ii.getImage();
all I get is my black background.
Any help will be very much appreciated.
Thanks.


Sign In
Create Account

Back to top









