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

📄 letterlabel.java

📁 通过JAVA编写的一个小游戏,键盘打字练习
💻 JAVA
字号:
package letterGame;

import java.awt.Color;
import java.awt.Font;
import java.util.Random;
import javax.swing.*;

/**
 * 继承自JLabel,产生字母并设置字母各项属性
 * 
 * @author 吴诗阳
 * @see javax.swing.JLabel
 */
public class LetterLabel extends JLabel{	
	
	public int width=30;
	public int height=30;
	public int locationX;
	public int locationY=0;
	private int letter;
	public static String[] letters={" ","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
	
	public int direction=GameParams.direction;       //控制方向,向上或向下
	
	public LetterLabel()
	{
		createLocationX();
		setLocationY();
		setAttribute();		
	}
	
	/**
	 * 返回对应字母的Code
	 * 
	 * @return 对应字母的Code
	 */
	public int getCode()
	{		
		return letter+64;
	}
	/**
	 * 返回对应字母的X位置
	 * 
	 * @return 对应字母的X位置
	 */
	public int getLocationX()
	{
		return locationX;
	}
	/**
	 * 返回字母的int值(a--1,b--2,c--3,...z--26)
	 * 
	 * @return 返回字母的int值
	 */
	public int getLetter()
	{
		return letter;
	}
	/**
	 * 返回对应字母的Y位置
	 * 
	 * @return 对应字母的Y位置
	 */
	public int getLocationY()
	{
		return locationY;
	}
	/**
	 * 字母到达屏幕下方,重新设置位置
	 *
	 */
	public void startAgain()
	{
		createLocationX();
		setAttribute();
		
		setLocationY();
	}
	/**
	 * 随机产生字母的X位置,并使初始各字母X位置不重复
	 *
	 */
	private void createLocationX()
	{
		Random random=new Random();
		
		locationX=Math.abs(random.nextInt()%350);
		letter=Math.abs(random.nextInt()%26)+1;	
		if(GameParams.direction==0)
		{
			direction=0;
		}
		else if(GameParams.direction==1)
		{
			direction=1;
		}
		else
			direction=Math.abs(random.nextInt()%2);
	}

	/**
	 * 设置字母的属性,包括字体,对齐方式,背景颜色
	 *
	 */
	private void setAttribute()
	{
        //设置属性
		setFont(new Font("",Font.BOLD,30));
		setHorizontalAlignment(SwingConstants.CENTER);
		setForeground(Color.CYAN);
		setSize(width,height);
		setText(letters[letter]);
		
		GameParams.allCount++;
		GameParams.shouldScore++;
		
	}
	
	public void changeLocationY()
	{
		if(direction==0)
			locationY+=GameParams.fallLines;
		else
			if(direction==1)
				locationY-=GameParams.fallLines;
	}
	
	private void setLocationY()
	{
		if(direction==0)
			locationY=0;
		else
			if(direction==1)
				locationY=550;
	}

}

⌨️ 快捷键说明

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