Java作为一种广泛使用的编程语言,以其强大的功能和灵活性深受开发者喜爱。本文将带领您走进Java编程的世界,通过一个简单的示例,教您如何使用Java Swing库绘制一个动态的蛇形动画。准备工作在开...
Java作为一种广泛使用的编程语言,以其强大的功能和灵活性深受开发者喜爱。本文将带领您走进Java编程的世界,通过一个简单的示例,教您如何使用Java Swing库绘制一个动态的蛇形动画。
在开始之前,请确保您已安装以下软件:
SnakeAnimation。以下是实现蛇形动画的Java代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Queue;
public class SnakeAnimation extends JPanel implements ActionListener { private final int DOT_SIZE = 10; private final int ALL_DOTS = 300; private final int RAND_POS = 29; private final int RAND_DIR = 4; private final int x[] = new int[ALL_DOTS]; private final int y[] = new int[ALL_DOTS]; private int dots; private int apple_x; private int apple_y; private char direction = 'R'; private boolean running = false; private Timer timer; private int delay = 140; public SnakeAnimation() { initGame(); } public void initGame() { dots = 3; for (int z = 0; z < dots; z++) { x[z] = 50 - z * 10; y[z] = 50; } locateApple(); timer = new Timer(delay, this); timer.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); drawDots(g); drawSnake(g); drawApple(g); } public void drawDots(Graphics g) { for (int z = 0; z < dots; z++) { if (z == 0) { g.setColor(Color.red); } else { g.setColor(Color.green); } g.fillOval(x[z], y[z], DOT_SIZE, DOT_SIZE); } } public void drawSnake(Graphics g) { for (int z = dots; z > 0; z--) { if (z == dots) { g.setColor(Color.red); } else { g.setColor(Color.green); } g.fillOval(x[z], y[z], DOT_SIZE, DOT_SIZE); } } public void drawApple(Graphics g) { g.setColor(Color.red); g.fillOval(apple_x, apple_y, DOT_SIZE, DOT_SIZE); } public void locateApple() { int r = (int) (Math.random() * RAND_POS); apple_x = ((r * DOT_SIZE)); r = (int) (Math.random() * RAND_POS); apple_y = ((r * DOT_SIZE)); } public void move() { for (int z = dots; z > 0; z--) { x[z] = x[(z - 1)]; y[z] = y[(z - 1)]; } switch (direction) { case 'U': y[0] = y[0] - DOT_SIZE; break; case 'D': y[0] = y[0] + DOT_SIZE; break; case 'L': x[0] = x[0] - DOT_SIZE; break; case 'R': x[0] = x[0] + DOT_SIZE; break; } } public void checkApple() { if ((x[0] == apple_x) && (y[0] == apple_y)) { dots++; locateApple(); } } public void checkCollision() { for (int z = dots; z > 0; z--) { if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) { running = false; } } if (y[0] >= 290) { y[0] = 0; } if (y[0] < 0) { y[0] = 290; } if (x[0] >= 290) { x[0] = 0; } if (x[0] < 0) { x[0] = 290; } if (running == false) { timer.stop(); } } @Override public void actionPerformed(ActionEvent e) { if (running) { move(); checkApple(); checkCollision(); } repaint(); } public static void main(String[] args) { JFrame frame = new JFrame("Snake Animation"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 300); frame.add(new SnakeAnimation()); frame.setVisible(true); }
}main方法。程序将弹出一个窗口,展示一个简单的蛇形动画。通过按键控制蛇的移动方向,尝试吃掉苹果,使蛇的长度增加。
本文通过一个简单的蛇形动画示例,展示了Java Swing库的强大功能。通过学习这个示例,您可以更好地理解Java编程中的图形界面设计和事件处理。希望这篇文章能帮助您解锁Java编程的魅力!