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

📄 charindicator.java

📁 Typing fingers Typing programme
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;

/**	This class is label that has the ability to highlight a character to be typed. */
	
class CharIndicator extends JPanel{
	private Font font;
	private FontMetrics metrics;
	private Color backcolor;
	private int total,charWidth;
	private char characters[];
	private Color color[],highLight=Utilities.getHighLightColor();
	private int candisplay;
	private int saveIndex=-1;
	CharIndicator(){
		UserData user=MainScreen.getInstance().getCurrentUser();
		font=new Font("MonoSpaced",Font.PLAIN,user.getTypingFontSize());
		metrics=getFontMetrics(font);
		charWidth=metrics.charWidth('a');
		setPreferredSize(new Dimension(Utilities.getLineWidth(),25));
		candisplay=(Utilities.getLineWidth()-2*Utilities.getIndent())/charWidth;
		characters=new char[candisplay+1];
		color=new Color[candisplay+1];
		characters[0]=' ';
		backcolor=user.getBackground();
	}
	public void paint(Graphics g){
		g.setFont(font);
		g.setColor(backcolor);
		g.fillRect(0,0,getWidth(),getHeight());
		int y=(getHeight()-metrics.getHeight())/2+metrics.getAscent();
		for(int i=0;i<=total;i++){
			g.setColor(color[i]);
			g.drawString(String.valueOf(characters[i]),Utilities.getIndent()+i*charWidth,y);
		}
		highlightAt(saveIndex,g);
	}
	public void highlightChar(int index,int x){
		if(index<1&&x==-1) return;
		Graphics g=getGraphics();
		if(g==null) return;
		int y=(getHeight()-metrics.getHeight())/2+metrics.getAscent();
		g.setColor(backcolor);
		g.fillRect(Utilities.getIndent()+(index+x)*charWidth,0,charWidth,metrics.getHeight());
		g.setColor(color[index+x]);
		g.setFont(font);
		g.drawString(String.valueOf(characters[index+x]),Utilities.getIndent()+(index+x)*charWidth,y);
		g.setColor(highLight);
		g.fillRect(Utilities.getIndent()+index*charWidth,0,charWidth,metrics.getHeight());
		g.setColor(Color.white);
		g.setFont(new Font("MonoSpaced",Font.BOLD,font.getSize()));
		g.drawString(String.valueOf(characters[index]),Utilities.getIndent()+index*charWidth,y);
		saveIndex=index;
	}
	public void highlightAt(int index,Graphics g){
		if(index==-1||index>=candisplay) return;
		if(g==null){
			saveIndex=0;
			repaint();
			return;
		}
		g.setFont(new Font("MonoSpaced",Font.BOLD,font.getSize()+2));
		int y=(getHeight()-metrics.getHeight())/2+metrics.getAscent();
		g.setColor(highLight);
		g.fillRect(Utilities.getIndent()+index*charWidth,0,charWidth,metrics.getHeight());
		g.setColor(Color.white);
		g.drawString(String.valueOf(characters[index]),Utilities.getIndent()+index*charWidth,y);
	}
	public void clearHighlight(int index){
		if(index<0||index>total)	return;
		Graphics g=getGraphics();
		if(g==null)	return;
		g.setFont(font);
		g.setColor(backcolor);
		g.fillRect(Utilities.getIndent()+index*charWidth,0,charWidth,metrics.getHeight());
		g.setColor(Color.blue);
		int y=(getHeight()-metrics.getHeight())/2+metrics.getAscent();
		g.drawString(String.valueOf(characters[index]),Utilities.getIndent()+index*charWidth,y);
	}
	public void setBackground(Color c){
		backcolor=c;
		repaint();
	}
	public char charAt(int index){
		if(index<0||index>total)
			return '\0';
		return characters[index];
	}
	public void addChar(char ch,Color c){
		if(total>=candisplay)
			return;
		characters[total]=ch;
		color[total]=c;
		total++;
		characters[total]=' ';
		repaint();
	}
	public boolean nextAllowed(){
		return (total<candisplay) ? true : false;
	}
	public void clearText(){
		total=0;
		characters[0]=' ';
		repaint();
	}
}

⌨️ 快捷键说明

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