⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamefont.java

📁 本代码为java编写的泡泡龙游戏
💻 JAVA
字号:

import java.awt.Image;
import java.awt.Graphics;
import java.awt.Point;

public class GameFont
{
	public int SEPARATOR_WIDTH = 1;
	public int SPACE_CHAR_WIDTH = 6;
	
	private Image fontMap;
	private char[] characters;
	private int[] position;
	private GameApplet applet;
	
	public GameFont(Image fontMap, char[] characters, int[] position, GameApplet applet)
	{
		this.fontMap = fontMap;
		this.characters = characters;
		this.position = position;
		this.applet = applet;
	}
	
	public GameFont(Image fontMap, GameApplet applet)
	{
		this(fontMap, new char[0], new int[0], applet);
	}
	
	public void setCharMap(char[] characters, int[] position)
	{
		this.characters = characters;
		this.position = position;		
	}
		
	public final int charSize(char c)
	{
		if (c == ' ')
		{
			return SPACE_CHAR_WIDTH;
		}	
		
		int index = getCharIndex(c);
				
		if (index == -1)
		{
			return 0;
		}

		if (index == position.length-1)
		{
			return fontMap.getWidth(applet)-position[index];
		}
		
		return position[index+1]-position[index];
	}
	
	public final int paintChar(char c, Graphics g, Point p)
	{
		if (c == ' ')
		{
			return SPACE_CHAR_WIDTH + SEPARATOR_WIDTH;
		}		
		
		int index = getCharIndex(c);
		
		if (index == -1)
		{
			return 0;
		}

		int imageWidth = 0;
		
		if (index == position.length-1)
		{
			imageWidth = fontMap.getWidth(applet)-position[index];
		}
		else
		{
			imageWidth = position[index+1]-position[index];
		}
		
		g.clipRect(p.x, p.y, imageWidth, fontMap.getHeight(applet));
		g.drawImage(fontMap, p.x-position[index], p.y, applet);
		
		return imageWidth + SEPARATOR_WIDTH;
	}

	private final int getCharIndex(char c)
	{
		for (int i=0 ; i<characters.length ; i++)
		{
			if (characters[i] == c)
			{
				return i;
			}
		}
		
		return -1;
	}
}

⌨️ 快捷键说明

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