📄 mainpanel.java
字号:
package system;
import java.awt.*;
import javax.swing.*;
import snake.*;
import food.*;
//============================================================================
// 此面板现在的宽度和高度都是501像素
//============================================================================
public class MainPanel
extends JPanel {
private MainFrame mainFrame;
private Image backGround; //背景图片
private Image offScreen; //次画面,用来防止图象的闪烁
private Graphics g2; //次画面的Graphics对象
//背景图片的宽度和高度
public static final int IMAGE_WIDTH = 20;
public static final int IMAGE_HEIGHT = 20;
public MainPanel(MainFrame frame) {
this.mainFrame = frame;
this.setBackground(Color.yellow);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//获得面板的高度和宽度
int width = this.getWidth();
int height = this.getHeight();
//取得地板的图片
this.backGround = new ImageIcon(Config.CURRENT_BG).getImage();
//建立次画面,处理图象的闪烁
this.offScreen = this.createImage(this.getWidth(), this.getHeight());
this.g2 = this.offScreen.getGraphics();
//======================以平铺的方式绘制地板===============================
int iX = (int) width / MainPanel.IMAGE_WIDTH;
int iY = (int) height / MainPanel.IMAGE_HEIGHT;
for (int i = 0; i <= iX; i++) {
for (int j = 0; j <= iY; j++) {
this.g2.drawImage(this.backGround, MainPanel.IMAGE_WIDTH * i,
MainPanel.IMAGE_HEIGHT * j,
MainPanel.IMAGE_WIDTH, MainPanel.IMAGE_HEIGHT, this);
}
}
//====================贪吃蛇死亡,绘制Game Over字符=======================
if (this.mainFrame.isGameOver()) {
Font oldFont = this.g2.getFont(); //保存原有字体
Color oldColor = this.g2.getColor(); //保存原有颜色
this.g2.setFont(new Font("Dialog", Font.BOLD, 64));
//取得GAME OVER字符的相关信息
FontMetrics fm = this.g2.getFontMetrics();
int ascent = fm.getAscent(); //上坡度
int descent = fm.getDescent(); //下坡度
int length = fm.stringWidth(Config.GAME_OVER);
//计算字符串绘制的位置(在面板的中央)
int x = (width - length) / 2;
int y = (height - (ascent + descent)) / 2 + ascent;
//设置新的颜色
this.g2.setColor(new Color(0, 0, 0, 128));
//绘制字符
this.g2.drawString(Config.GAME_OVER, x, y);
this.g2.setColor(new Color(255, 50, 50, 200));
this.g2.drawString(Config.GAME_OVER, x - 4, y - 4);
//恢复原有的字体和颜色
this.g2.setFont(oldFont);
this.g2.setColor(oldColor);
}
else if(this.mainFrame.isWelcome()){
//========================显示开头画面=============================
Font oldFont = this.g2.getFont(); //保存原有字体
Color oldColor = this.g2.getColor(); //保存原有颜色
//设置新的字体和颜色
this.g2.setFont(new Font("Dialog", Font.BOLD, 96));
this.g2.setColor(new Color(0, 0, 0, 200));
//取得GO字符的相关信息
FontMetrics fm = this.g2.getFontMetrics();
int ascent = fm.getAscent(); //上坡度
int descent = fm.getDescent(); //下坡度
int length = fm.stringWidth(Config.GAME_NAME);
//计算字符串绘制的位置(在面板的中央)
int x = (width - length) / 2;
int y = (height - (ascent + descent)) / 2;
//绘制“贪吃蛇”字符
this.g2.drawString(Config.GAME_NAME, x, y);
this.g2.setColor(new Color(12, 244, 12, 255));
this.g2.drawString(Config.GAME_NAME, x + 8, y + 8);
//绘制版本号
this.g2.setFont(new Font("Dialog", Font.PLAIN, 12));
fm = this.g2.getFontMetrics();
length = fm.stringWidth(Config.VERSION);
x = (width - length) / 2;
y += descent + 30;//在“贪吃蛇”下面10象素处绘制
this.g2.setColor(Color.black);
this.g2.drawString(Config.VERSION,x,y);
//绘制“华游工作室”
this.g2.setFont(new Font("Dialog", Font.BOLD, 32));
fm = this.g2.getFontMetrics();
length = fm.stringWidth("华游工作室");
x = (width - length) / 2;
y += 80;//在版本号下面30象素处绘制
this.g2.setColor(new Color(0,0,0,200));
this.g2.drawString("华游工作室",x,y);
this.g2.setColor(Color.red);
this.g2.drawString("华游工作室",x + 2,y + 2);
//恢复原有的字体和颜色
this.g2.setFont(oldFont);
this.g2.setColor(oldColor);
}
else {
//======================在开始新游戏时,绘制GO字符串=================
if (this.mainFrame.isNewGame()) {
Font oldFont = this.g2.getFont(); //保存原有字体
Color oldColor = this.g2.getColor(); //保存原有颜色
//设置新的字体和颜色
this.g2.setFont(new Font("Dialog", Font.BOLD + Font.ITALIC, 64));
this.g2.setColor(new Color(0, 0, 0, 200));
//取得GO字符的相关信息
FontMetrics fm = this.g2.getFontMetrics();
int ascent = fm.getAscent(); //上坡度
int descent = fm.getDescent(); //下坡度
int length = fm.stringWidth("G O!!!");
//计算字符串绘制的位置(在面板的中央)
int x = (width - length) / 2;
int y = (height - (ascent + descent)) / 2 + ascent;
//绘制字符
this.g2.drawString("G O!!!", x, y);
this.g2.setColor(new Color(255,0,0,200));
this.g2.drawString("G O!!!", x - 2, y - 2);
//恢复原有的字体和颜色
this.g2.setFont(oldFont);
this.g2.setColor(oldColor);
//设置isNewGame为false,表示字符绘制完毕
// this.mainFrame.setNewGame(false);
}
if(this.mainFrame.isLevelUp()){
Font oldFont = this.g2.getFont(); //保存原有字体
Color oldColor = this.g2.getColor(); //保存原有颜色
//设置新的字体和颜色
this.g2.setFont(new Font("Dialog", Font.BOLD + Font.ITALIC, 64));
this.g2.setColor(new Color(0, 0, 0, 200));
//取得GO字符的相关信息
FontMetrics fm = this.g2.getFontMetrics();
int ascent = fm.getAscent(); //上坡度
int descent = fm.getDescent(); //下坡度
int length = fm.stringWidth("Level Up!");
//计算字符串绘制的位置(在面板的中央)
int x = (width - length) / 2;
int y = (height - (ascent + descent)) / 2 + ascent;
//绘制字符
this.g2.drawString("Level Up!", x, y);
this.g2.setColor(new Color(20,200,20,200));
this.g2.drawString("Level Up!", x - 2, y - 2);
//恢复原有的字体和颜色
this.g2.setFont(oldFont);
this.g2.setColor(oldColor);
//设置isLevelUp为false,表示字符绘制完毕
// this.mainFrame.setLevelUp(false);
}
//=============================显示贪吃蛇===========================
Snake s = this.mainFrame.getSnake();
if (s == null || s.size() < 1) {
return;
}
for (int i = 0; i < s.size(); i++) {
Block b = (Block) s.get(i);
this.g2.drawImage(b.getImage(), b.getX(), b.getY(), Block.WIDTH, Block.HEIGHT, this);
}
//===========================显示食物==============================
Food f = this.mainFrame.getFood();
this.g2.drawImage(f.getImage(), f.getX(), f.getY(), Food.WIDTH, Food.HEIGHT, this);
}
//============================将次画面画到屏幕上======================
g.drawImage(this.offScreen, 0, 0, this);
}
} //:~zj
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -