statuspanel.java

来自「windows的蜘蛛纸牌游戏源代码」· Java 代码 · 共 57 行

JAVA
57
字号
package spider.araneid;

import javax.swing.*;
import java.awt.*;

public class StatusPanel extends JPanel implements Runnable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1530852606087504880L;
	Thread runner;
	private Araneid main;

	public StatusPanel(Araneid main) {
		this.main = main;
		if (this.runner==null) {
			this.runner = new Thread(this);
			this.runner.start();
		}
	}

	public void paintComponent(Graphics g) {
		Graphics2D comp = (Graphics2D)g;
		Font font = new Font("宋体",0,12);
		comp.setFont(font);
		comp.drawRect(0,0,this.getSize().width-1,this.getSize().height-1);
		comp.setColor(Color.yellow);
		comp.fillRect(1,1,this.getSize().width-2,this.getSize().height-2);
		comp.setColor(Color.black);
		Player player = (Player)main.player;

		String[] msg = {
			"玩    家:"+player.getNickname(),
			"流逝时间:"+player.getLapseTime()+" 秒",
			"得    分:"+player.getScore()
		};

		int max=0;
		for(int i=0;i<msg.length;i++) {
			if (max<msg[i].length()) max = msg[i].length();
		}
		int x = (this.getSize().width-max*7)/2;
		for(int i=0;i<msg.length;i++) {
			comp.drawString(msg[i],x,30+i*20);
		}
	}

	public void run() {
		try {
			while(true) {
				main.player.addLapseTime();
				this.repaint();
				Thread.sleep(1000);
			}
		} catch (InterruptedException ignore) {}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?