📄 numberlabel.java
字号:
/*
* Created on 2005-6-18
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package view;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
/**
* @author rover
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class NumberLabel extends JPanel {
/**
*
*/
public NumberLabel() {
super();
side = 0;
number = new StringBuffer();
// TODO Auto-generated constructor stub
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (font != null && number != null) {
drawMapFrame(0, 0, getWidth(), getHeight(), side, false, g);
if (fontColor != null) {
g.setColor(fontColor);
}
g.setFont(font);
g.drawString(number.toString(), x, y);
}
}
public void setNumber(String number) {
this.number = new StringBuffer(number);
if (number.length() < length) {
for (int i = 0; i != length - number.length(); i++) {
this.number.insert(0, '0');
}
}
}
public void setFontColor(Color fontColor) {
this.fontColor = fontColor;
}
private Color fontColor;
private int length;
public void setFontAndBoard(Font font, int length, int side) {
this.side = side;
this.length = length;
this.font = font;
number = new StringBuffer();
for (int i = 0; i != length; i++) {
number.append('0');
}
}
public void fitSizeToFont(){
Graphics2D g2 = (Graphics2D) getGraphics();
if (g2 == null) return;
Rectangle2D bounds = font.getStringBounds(number.toString(),
(g2).getFontRenderContext());
this.setSize((int) bounds.getWidth() + 8,
(int) bounds.getHeight() + 4);
x = (int) (getWidth() - bounds.getWidth()) / 2;
y = (int) ((getHeight() + bounds.getY()) / 2 - bounds.getY());
}
private Font font;
private int side;
private int x;
private int y;
private StringBuffer number;
private int fontSize;
// draw the map side frame
private void drawMapFrame(int x, int y, int width, int height, int side,
boolean raised, Graphics g) {
//the sets of coordinate of side Polygons
int setX[][] = { { x, x + width, x + width - side, x + side },//up
{ x + width - side, x + width, x + width, x + width - side },//right
{ x + side, x + width - side, x + width, x },//down
{ x, x + side, x + side, x } };//left
int setY[][] = {
{ y, y, y + side, y + side },//up
{ y + side, y, y + height, y + height - side },//right
{ y + height - side, y + height - side, y + height, y + height },//down
{ y, y + side, y + height - side, y + height } };//left
//draw the side polygons
for (int i = 0; i <= 3; i++) {
if (raised) {//raised
switch (i) {//set color
case 0:
g.setColor(new Color(244, 242, 232));
break;
case 1:
g.setColor(new Color(152, 150, 139));
break;
case 2:
g.setColor(new Color(102, 101, 93));
break;
case 3:
g.setColor(new Color(240, 238, 224));
break;
}
} else {//lowered
switch (i) {//set color
case 0:
g.setColor(new Color(100, 100, 100));
break;
case 1:
g.setColor(new Color(240, 240, 240));
break;
case 2:
g.setColor(new Color(250, 250, 250));
break;
case 3:
g.setColor(new Color(120, 120, 120));
break;
}
}
g.fillPolygon(setX[i], setY[i], 4);//draw
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -