Код: Выделить всё
`package animation2D;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyPanel extends JPanel implements ActionListener{
final int PANEL_WIDTH = 500;
final int PANEL_HEIGHT = 500;
Image enemy;
Image backgroundImage;
Timer timer;
int xVelocity = 2;
int yVelocity = 2;
int x = 0;
int y = 0;
MyPanel(){
this.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
this.setBackground(Color.black);
enemy = new ImageIcon("enemy.png").getImage();
backgroundImage = new ImageIcon("earth.jpg").getImage();
timer = new Timer(10, this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g); //Paint the background
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(backgroundImage, 0, 0, null);
g2D.drawImage(enemy, x, y, null);
}
@Override
public void actionPerformed(ActionEvent e) {
if (x>=PANEL_WIDTH-enemy.getWidth(null) || x=PANEL_HEIGHT-enemy.getHeight(null) || y
Подробнее здесь: [url]https://stackoverflow.com/questions/79122620/java-image-not-displaying-in-2d-animation[/url]