?? charindicator.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 + -