public final void stop();为什么会导致编译出错
时间: 2023-12-06 11:40:19 浏览: 21
`public final void stop()` 方法是 `java.lang.Thread` 类的方法,该方法已经被标记为不建议使用(deprecated)并且在 Java 11 中已被弃用。这意味着在使用较新版本的 Java 编译器时,编译器会发出警告,提示您不要使用该方法。
如果您在 Java 11 或更高版本中使用 `public final void stop()` 方法,编译器会报错,因为该方法已被删除,无法使用。
建议使用 `Thread.interrupt()` 方法来停止线程,这是一种更好的方式。
相关问题
编写一个贪吃蛇的JAVA小游戏
好的,我来为您编写一个基本的贪吃蛇小游戏。游戏规则如下:
1. 贪吃蛇初始长度为3,每次吃到食物会增加1格长度。
2. 如果贪吃蛇撞到自己或者墙壁,游戏结束。
3. 食物每次随机生成在游戏区域内的一个位置。
以下是JAVA代码实现:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SnakeGame extends JPanel implements ActionListener {
private final int WIDTH = 300;
private final int HEIGHT = 300;
private final int UNIT_SIZE = 10;
private final int GAME_UNITS = (WIDTH * HEIGHT) / UNIT_SIZE;
private final int DELAY = 100;
private final int[] x = new int[GAME_UNITS];
private final int[] y = new int[GAME_UNITS];
private int bodyParts = 3;
private int foodEaten;
private int foodX;
private int foodY;
private char direction = 'R';
private boolean running = false;
private Timer timer;
public SnakeGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.black);
setFocusable(true);
addKeyListener(new MyKeyAdapter());
startGame();
}
public void startGame() {
newFood();
running = true;
timer = new Timer(DELAY, this);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if (running) {
// 绘制食物
g.setColor(Color.red);
g.fillRect(foodX, foodY, UNIT_SIZE, UNIT_SIZE);
// 绘制蛇身
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.green);
} else {
g.setColor(new Color(45, 180, 0));
}
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
// 绘制分数
g.setColor(Color.white);
g.drawString("得分:" + foodEaten, 10, 20);
} else {
gameOver(g);
}
}
public void newFood() {
foodX = (int) (Math.random() * (WIDTH / UNIT_SIZE)) * UNIT_SIZE;
foodY = (int) (Math.random() * (HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
}
public void move() {
// 移动身体
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
// 移动头部
switch (direction) {
case 'U':
y[0] = y[0] - UNIT_SIZE;
break;
case 'D':
y[0] = y[0] + UNIT_SIZE;
break;
case 'L':
x[0] = x[0] - UNIT_SIZE;
break;
case 'R':
x[0] = x[0] + UNIT_SIZE;
break;
}
}
public void checkFood() {
if (x[0] == foodX && y[0] == foodY) {
bodyParts++;
foodEaten++;
newFood();
}
}
public void checkCollisions() {
// 撞墙检测
for (int i = bodyParts; i > 0; i--) {
if (x[0] == x[i] && y[0] == y[i]) {
running = false;
}
}
if (x[0] < 0 || x[0] >= WIDTH || y[0] < 0 || y[0] >= HEIGHT) {
running = false;
}
if (!running) {
timer.stop();
}
}
public void gameOver(Graphics g) {
g.setColor(Color.white);
g.setFont(new Font("Ink Free", Font.BOLD, 40));
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Game Over", (WIDTH - metrics.stringWidth("Game Over")) / 2, HEIGHT / 2);
g.drawString("得分:" + foodEaten, (WIDTH - metrics.stringWidth("得分:" + foodEaten)) / 2, (HEIGHT / 2) + 50);
}
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkFood();
checkCollisions();
}
repaint();
}
public class MyKeyAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (direction != 'R') {
direction = 'L';
}
break;
case KeyEvent.VK_RIGHT:
if (direction != 'L') {
direction = 'R';
}
break;
case KeyEvent.VK_UP:
if (direction != 'D') {
direction = 'U';
}
break;
case KeyEvent.VK_DOWN:
if (direction != 'U') {
direction = 'D';
}
break;
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("贪吃蛇");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SnakeGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
您可以将以上代码保存到一个名为`SnakeGame.java`的文件中,并使用JDK编译运行。
贪吃蛇Java运行源代码
以下是一个使用 Java Swing 编写的贪吃蛇小游戏的源代码示例:
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GamePanel extends JPanel implements ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 600;
public static final int HEIGHT = 600;
public static final int UNIT_SIZE = 20;
public static final int GAME_UNITS = (WIDTH * HEIGHT) / (UNIT_SIZE * UNIT_SIZE);
public static final int DELAY = 75;
public static final int[] X_OFFSET = { 0, 0, -1, 1 };
public static final int[] Y_OFFSET = { -1, 1, 0, 0 };
private ArrayList<Point> snake;
private Point food;
private int score;
private boolean running;
private int direction;
private Timer timer;
private Random random;
public GamePanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.black);
setFocusable(true);
addKeyListener(this);
initGame();
}
private void initGame() {
snake = new ArrayList<Point>();
addSnakeUnit(new Point(WIDTH / 2, HEIGHT / 2));
addSnakeUnit(new Point(WIDTH / 2, HEIGHT / 2 + UNIT_SIZE));
addSnakeUnit(new Point(WIDTH / 2, HEIGHT / 2 + 2 * UNIT_SIZE));
food = generateRandomFood();
score = 0;
running = true;
direction = 0;
timer = new Timer(DELAY, this);
timer.start();
random = new Random();
}
private Point generateRandomFood() {
int x = random.nextInt(WIDTH / UNIT_SIZE) * UNIT_SIZE;
int y = random.nextInt(HEIGHT / UNIT_SIZE) * UNIT_SIZE;
return new Point(x, y);
}
private void addSnakeUnit(Point p) {
snake.add(p);
}
private void removeSnakeUnit(Point p) {
snake.remove(p);
}
private void moveSnake() {
Point head = snake.get(0);
int newX = head.x + X_OFFSET[direction] * UNIT_SIZE;
int newY = head.y + Y_OFFSET[direction] * UNIT_SIZE;
Point newHead = new Point(newX, newY);
if (checkCollision(newHead)) {
endGame();
} else {
addSnakeUnit(newHead);
if (newHead.equals(food)) {
score++;
food = generateRandomFood();
} else {
removeSnakeUnit(snake.get(snake.size() - 1));
}
}
}
private boolean checkCollision(Point p) {
return p.x < 0 || p.x >= WIDTH || p.y < 0 || p.y >= HEIGHT || snake.contains(p);
}
private void endGame() {
running = false;
timer.stop();
}
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
moveSnake();
}
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawSnake(g);
drawFood(g);
drawScore(g);
}
private void drawSnake(Graphics g) {
for (int i = 0; i < snake.size(); i++) {
Point p = snake.get(i);
g.setColor(Color.white);
g.fillRect(p.x, p.y, UNIT_SIZE, UNIT_SIZE);
}
}
private void drawFood(Graphics g) {
g.setColor(Color.red);
g.fillRect(food.x, food.y, UNIT_SIZE, UNIT_SIZE);
}
private void drawScore(Graphics g) {
g.setColor(Color.white);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("Score: " + score, 20, 30);
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && direction != 1) {
direction = 0;
} else if (key == KeyEvent.VK_RIGHT && direction != 0) {
direction = 1;
} else if (key == KeyEvent.VK_UP && direction != 3) {
direction = 2;
} else if (key == KeyEvent.VK_DOWN && direction != 2) {
direction = 3;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
```
这个程序定义了一个 GamePanel 类,它继承自 JPanel,并实现了 ActionListener 和 KeyListener 接口。在 GamePanel 中,我们定义了一些常量,例如窗口的宽度和高度、方格的大小、刷新时间间隔等等。我们还定义了一些变量,例如蛇的坐标、食物的坐标、玩家的得分等等。
在 initGame 方法中,我们初始化了游戏的状态,包括蛇的初始位置、食物的随机位置、得分、运行状态等等。在 moveSnake 方法中,我们实现了蛇的移动逻辑,包括头部的移动、身体的增加与删除、碰撞检测等等。在 endGame 方法中,我们结束了游戏。
在 actionPerformed 方法中,我们在游戏运行时不断刷新界面,在 paintComponent 方法中,我们绘制了蛇、食物、得分等界面元素。在 keyPressed 方法中,我们处理了键盘事件,根据按下的方向键来改变蛇的运动方向。
如果想要运行这个程序,需要将 GamePanel.java 和 SnakeGame.java 两个文件保存到同一个文件夹中,并使用 JDK 编译器将它们编译成 class 文件。然后,在命令行中进入这个文件夹,并执行以下命令:
```
java SnakeGame
```
这样,就可以启动贪吃蛇小游戏了。