TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
//绕屏幕四边运动的文字。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
public class ChenWin11 extends JApplet implements Runnable
{
int width=0;
int height=0;
int length=0;
int delay=125;
int point_size=25;
String message="This is ChenWin11!";
String font_to_use="TimesRpman";
char char_array[];
int offset_x[];
int offset_y[];
Thread mythread;
public void init()
{ setBackground(Color.green);
length=message.length();
char_array=new char[length];
message.getChars(0,length,char_array,0);
offset_x=new int[length];
offset_y=new int[length];
}
public void start()
{ mythread=new Thread(this);
mythread.start();
}
void get_defaults()
{
width=getSize().width;
height=getSize().height;
}
public void re_draw()
{
repaint();
try
{
Thread.sleep(delay);
}
catch(InterruptedException e)
{
}
}
public void run()
{
while(true)
{
for(int y1=height-point_size;y1>point_size;y1-=point_size)
{
int j=-1;
for(int i=0;i<length;i++)
{
if(y1+i*point_size>height-point_size)
{if(j<0)
j=i;
offset_y=height-point_size;
offset_x=(i-j+2)*point_size;
}
else
{
offset_y=y1+i*point_size;
offset_x=point_size;
}
}
re_draw();
}
for(int x1=point_size;x1<width;x1+=point_size)
{
int j=-1;
for(int i=0;i<length;i++)
{
if(x1-i*point_size<point_size*2)
{if(j<0)
j=i;
offset_y=(i-j+1)*point_size;
offset_x=point_size;
}
else
{
offset_y=point_size;
offset_x=x1-i*point_size;
}
}
re_draw();
}
for(int y1=point_size*2;y1<height-point_size;y1+=point_size)
{
int j=-1;
for(int i=0;i<length;i++)
{
if(y1-i*point_size<point_size)
{if(j<0)
j=i;
offset_y=point_size;
offset_x=width-(i-j+2)*point_size;
}
else
{
offset_y=y1-i*point_size;
offset_x=width-point_size;
}
}
re_draw();
}
for(int x1=width-point_size;x1>point_size;x1-=point_size)
{
int j=-1;
for(int i=0;i<length;i++)
{
if(x1+i*point_size>width-point_size*2)
{if(j<0)
j=i;
offset_y=height-(i-j+1)*point_size;
offset_x=width-point_size;
}
else
{
offset_y=height-point_size;
offset_x=x1+i*point_size;
}
}
re_draw();
}
}
}
public void paint(Graphics g)
{
get_defaults();
Graphics2D g2d=(Graphics2D)g;
BufferedImage bi=new BufferedImage(
getWidth(),getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d1=bi.createGraphics();
Font font=new Font(font_to_use,Font.PLAIN,point_size);
g2d1.setFont(font);
g2d1.clearRect(0,0,width,80);
g2d1.clearRect(width-80,0,80,height);
g2d1.clearRect(0,height-80,width,80);
g2d1.clearRect(0,0,80,height);
g2d1.setColor(Color.blue);
for(int i=0;i<message.length();i++)
g2d1.drawChars(char_array,i,1,offset_x,offset_y);
g2d1.setColor(Color.red);
g2d1.drawString("This is ChenWin11",width/4,height/2);
g2d.drawImage(bi,0,0,width,height,this);
}
public void update(Graphics g)
{
paint(g);
}
}
源码下载:http://file.javaxxz.com/2014/10/30/235530921.zip |
|