📄 applet1.java
字号:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
public class Applet1 extends Applet implements Runnable,MouseListener
{
private Thread thread;
private String str1,str2;
private Font font1,font2;
private int speed,lastX1,lastY1,directX1,directY1,lastX2,lastY2,directX2,directY2;
public void init()
{
String param;
addMouseListener(this);
param=getParameter("speed");
if(param==null)
speed=50;
else speed=Integer.valueOf(param).intValue();
//font1=new Font("TimesRoman",Font.BOLD,30);
font1=new Font("黑体",Font.BOLD,60);
param=getParameter("str1");
if (param==null)
str1="Null";
else str1=param;
lastX1=(int)(Math.random()*(getSize().width-1));
lastY1=(int)((getSize().height-font1.getSize()-1)*Math.random());
directX1=3;
directY1=3;
}
public void start(){
this.setBackground(Color.white);
if (thread==null){
thread=new Thread(this);
thread.start();
}
}
public void paint(Graphics g){
int x,y,space;
StringTokenizer t;
FontMetrics fm;
g.setColor(Color.black);
g.setFont(font1);
fm=g.getFontMetrics();
space=fm.stringWidth (" ");
x=lastX1;
y=lastY1;
//以空格为分隔符依次从STR1中取字显示
for(t=new StringTokenizer(str1);t.hasMoreTokens();){
String word=t.nextToken();
int w=fm.stringWidth(word)+space;
if (x>getSize().width){
x=x-getSize().width ;//到边界则回头
}
//产生一种随机色
g.setColor(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));
g.drawString(word,x,y);
System.out.println(word);
x+=w;
y+=3;
}
if (Math.random()>0.99){
directX1=-directX1;
}
//lastX1,lastY1为下一次显示字符串的起点位置
//directX1,directY1为两次显示的间隔
lastX1+=directX1;
if (lastX1>=getSize().width){
lastX1=0;
}else if (lastX1<0){
lastX1=getSize().width-1;
}
lastY1+=directY1;
if (lastY1>=getSize().height){
directY1=-3;
}else if(lastY1<font1.getSize()){
directY1=3;
}
}
public void stop(){
thread =null;
}
public void run(){
while (thread !=null){
repaint();
try{
thread.sleep(speed);
}catch(InterruptedException e){}
}
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
if (thread==null){start();}
else{ thread=null;}
}
public void mouseReleased(MouseEvent e){
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -