📄 graphicsutil.java
字号:
/*
* GraphicsUtil.java
*
* Created on 2007年8月21日, 上午11:41
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package minegame;
/**
*
* @author linda
*/
import java.awt.*;
import javax.swing.*;
public class GraphicsUtil {//该类用来为雷区单元提供图像画出方法,每个单元尺寸为32*32
//定义一个颜色数组,设置相应数字的颜色
public static Color[] colorreg = new Color[] {
null, // 数字0的颜色
Color.blue, // 数字1的颜色
Color.green.darker(), // 数字2的颜色
Color.red, // 数字3的颜色
Color.blue.darker(), // 数字4的颜色
Color.MAGENTA, // 数字5的颜色
Color.CYAN.darker(), // 数字6的颜色
Color.BLACK, // 数字7的颜色
Color.orange.darker() // 数字8的颜色
};
//地雷颜色
public static Color mbcolor = new Color(90, 90, 90);
//未扫单元颜色
public static Color ukcolor = new Color(99, 130, 191);
//数字字体
public static Font numfont = new Font("Verdana", Font.BOLD, 18);
//问号字体
public static Font qnmfont = new Font("Verdana", Font.PLAIN, 10);
//画出未扫单元
public static void drawUnknown(Graphics g, int x, int y) {
g.setColor(ukcolor);
g.fillRect(x, y, 32, 32);
}
//画出地雷单元
public static void drawMine(Graphics g, int x, int y) {
g.clearRect(x, y, 32, 32);//清空单元
g.setColor(mbcolor);//设置地雷颜色
g.fillOval(x+5, y+9, 21, 19);//画出圆形地雷
g.setColor(Color.black);//设置地雷柄部颜色
g.fillRect(x+11, y+5, 10, 6);//画出方形地雷柄部
}
//画出已扫单元
public static void drawFlag(Graphics g, int x, int y) {
g.clearRect(x, y, 32, 32);
g.setColor(Color.red);
g.fillRect(x+8, y+8, 16, 10);//画出红色旗子
g.setColor(Color.black);
g.drawLine(x+8, y+8, x+8, y+24);//画出黑色旗杆
g.drawLine(x+9, y+8, x+9, y+24);//画出黑色旗杆
}
//画出疑问单元
public static void drawDoubt(Graphics g, int x, int y) {
g.clearRect(x, y, 32, 32);
g.setColor(colorreg[4]);
g.fillRect(x+8, y+8, 16, 10);//画出深蓝色旗子
g.setColor(Color.black);
g.drawLine(x+8, y+8, x+8, y+24);//画出黑色旗杆
g.drawLine(x+9, y+8, x+9, y+24);//画出黑色旗杆
g.setColor(Color.yellow);//设置问号颜色
g.setFont(qnmfont);//设置问号字体
FontMetrics fm = g.getFontMetrics();
String s = "?";
int sx = (14 - fm.stringWidth(s)) / 2;
int sy = (10 - fm.getHeight()) / 2 + fm.getAscent();
g.drawString(s, x+sx+10, y+sy+8);//画出问号
}
//如果游戏失败则在标错的位置画出一个叉号(X)
public static void drawCross(Graphics g, int x, int y) {
g.setColor(Color.black);//设置叉号颜色
//画出叉号
g.drawLine(x+2, y+2, x+28, y+28);
g.drawLine(x+2, y+3, x+28, y+29);
g.drawLine(x+3, y+2, x+29, y+28);
g.drawLine(x+2, y+28, x+28, y+2);
g.drawLine(x+2, y+27, x+28, y+1);
g.drawLine(x+3, y+28, x+29, y+2);
}
//画出数字
public static void drawNumber(Graphics g, int x, int y, int i) {
g.clearRect(x, y, 32, 32);
if (i == 0)
return;
g.setColor(colorreg[i]);//设置相应数字的颜色
g.setFont(numfont);//设置数字字体
FontMetrics fm = g.getFontMetrics();//获取字体规格对象
String s = String.valueOf(i);//数字转换成字符串对象
//数字在单元中居中
int sx = (32 - fm.stringWidth(s)) / 2;
int sy = (32 - fm.getHeight()) / 2 + fm.getAscent();
g.drawString(s, x+sx, y+sy);//画出数字
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -