📄 keyto8_3.java
字号:
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class KeyTo8_3 extends JApplet implements Runnable{
int Rectangle1_x;
int Rectangle1_y;
int dx;
int dy;
int Rectangle2_x;;
int Rectangle2_y;;
int xPosition;
int yPosition;
int wordSize=20;
int Direct_x;
int Direct_y;
int xLeft;
int xRight;
int yTop;
int yBottom;
int xFlag;
int yFlag;
double Random;
boolean st;
String str="Java Programming";
Font wordFont;
Thread TextThread;
Image OffScreen;
Graphics drawOffScreen;
public void init(){
wordFont=new Font("TimesRoman",Font.BOLD,wordSize);
Rectangle1_x=getSize().width;
Rectangle1_y=getSize().height;
dx=20;
dy=20;
Rectangle2_x=Rectangle1_x-2*dx;
Rectangle2_y=Rectangle1_y-2*dy;
xPosition=Rectangle1_x/2;
yPosition=Rectangle1_y/2;
xLeft=dx;
xRight=Rectangle1_x-dx-str.length();
yTop=dy+8;
yBottom=Rectangle1_y-dx;
xFlag=1;
yFlag=1;
Random=Math.random()*Math.PI*2;
Direct_x=xLeft+(int)(Math.cos(Random)*(xRight-xLeft));
Direct_y=yTop+(int)(Math.sin(Random)*(yBottom-yTop));
st=true;
OffScreen=createImage(Rectangle1_x,Rectangle1_y);
drawOffScreen=OffScreen.getGraphics();
}
public void start(){
if(Direct_x>Direct_y){
Direct_x=Direct_x/Direct_y;
Direct_y=1;
}
else{
Direct_y=Direct_y/Direct_x;
Direct_x=1;
}
if(TextThread==null){
TextThread=new Thread(this);
TextThread.start();
}
}
public void stop(){
if(TextThread!=null){
TextThread.stop();
TextThread=null;
}
}
public void run(){
while(true){
xPosition+=Direct_x*xFlag;
yPosition+=Direct_y*yFlag;
if(xPosition<=xLeft){
xFlag=1;
if(Direct_x<0)
Direct_x*=-1;
}
if(xPosition>=xRight-str.length()*wordSize/2-2)
xFlag=-1;
if(yPosition<=yTop+wordSize/2){
yFlag=1;
if(Direct_y<0)
Direct_y*=-1;
}
if(yPosition>=yBottom-wordSize/2+6)
yFlag=-1;
if(xPosition<xLeft)
xPosition=xLeft;
if(xPosition>xRight-str.length()*wordSize/2-2)
xPosition=xRight-str.length()*wordSize/2-2;
if(yPosition<yTop+wordSize/2)
yPosition=yTop+wordSize/2;
if(yPosition>yBottom-wordSize/2+6)
yPosition=yBottom-wordSize/2+6;
repaint();
try{
Thread.sleep(20);
}catch(InterruptedException e){}
}
}
public void paint(Graphics g){
if(st==true){
drawOffScreen.setColor(Color.blue);
drawOffScreen.fillRect(0,0,Rectangle1_x,Rectangle1_y);
}
drawOffScreen.setColor(Color.black);
drawOffScreen.fillRect(dx,dy,Rectangle1_x-2*dx,Rectangle1_y-2*dy);
drawOffScreen.setColor(Color.red);
drawOffScreen.setFont(wordFont);
drawOffScreen.drawString(str,xPosition,yPosition);
g.drawImage(OffScreen,0,0,this);
st=false;
}
public void update(Graphics g){
paint(g);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -