java模拟舞动字符
源代码:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class brandishString extends Applet implements Runnable, MouseListener {
String str; // 要显示的字符串
char strChars[]; // 字符串的字符数组表示
Thread runner = null; // 线程
boolean threadSuspended; // 线程的状态
int strLength; // 字符串长度
static final int REGULAR_WD = 100; // 字符舞动的宽度
static final int REGULAR_HT = 50; // 字符舞动的高度
Font regularFont = new Font("Serif", Font.BOLD, 120); // 设置字体
public void init() {// 初始化方法
this.setBackground(Color.RED);
this.setForeground(Color.BLACK);
str = "08计本曹锋"; // 设置默认参数
strLength = str.length();
strChars = new char[strLength];
str.getChars(0, strLength, strChars, 0); // 获取字符数组
threadSuspended = false; // 用来判断线程的
this.addMouseListener(this); // 当前对象自己监视自己
this.setSize(1000, 500); // 设置大小
}
public void start() { // init()方法执行完自动执行start()方法
runner = new Thread(this); // 创建一个线程
runner.start(); // 启动线程
}
public void run() {
Thread me = Thread.currentThread(); // 获取当前线程
while (runner == me) { // 判断当前线程是否和runner是同一个线程
try {
Thread.sleep(100);
synchronized (this) {// 对当前对象加锁
if (threadSuspended)
wait(); // 进入等待状态
}
} catch (InterruptedException e) {
} // 不需要编写异常代码
repaint(); // 刷新屏幕
}
}
public void paint(Graphics g) {
int length = strChars.length;// 求字符长度
for (int i = 0, x = 0; i < length; i++) {
g.setFont(regularFont); // 设置字体
int px = (int) (100 * Math.random() + x);// 设置x坐标
int py = (int) (300 * Math.random() + REGULAR_HT);// 设置y坐标
g.drawChars(strChars, i, 1, px, py); // 输出一个字符
x += REGULAR_WD;
}
}
public synchronized void mousePressed(Mouse
相关文档:
Java NIO API详解
在JDK
1.4以前,Java的IO操作集中在java.io这个包中,是基于流的阻塞(blocking)API。对于大多数应用来说,这样的API使用很方
便,然而,一些对性能要求较高的应用,尤其是服务端应用,往往需要一个更为有效的方式来处理IO。从JDK 1.4起,NIO
API作为一个基于缓冲区,并能提供非阻塞(non-blo ......
Sun公司一名员工自己创作的歌,关于Java EE 5,虽然不怎么好听,但歌词很有意思,程序员业余生活也可以这么丰富,羡慕!
Ladies and gentlemen, this is Java EE 5!
One, two, three, four, five
There’s a technology I use day and night
For my application with a web frontend
They told me to use .Net
......
选择了java,也就是选择了一条光明而坎坷的道路。说他光明是因为应用的地方日益广泛,全球有很大的开发群体在为之忙碌,开源项目应有尽有;说他坎坷,是因为随着java的发展,现在有了太多的分支技术j2ee 13种技术,太多的框架(从底层到页面)如果要掌握还真不是件容易的事情,而且开源的东西永无止尽的在升级,在推陈 ......
众所周知,String是由字符组成的串,在程序中使用频率很高。Java中的String是一个类,而并非基本数据类型。 不过她却不是普通的类哦!!!
【镜头1】 String对象的创建
1、关于类对象的创建,很普通的一种方式就是利用构造器,String类也不例外:
& ......
程序源代码:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane; ......