⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch15_11_jumptext.java

📁 java applet Ch15_15_HwLoop 有PAINT STOP 等功能
💻 JAVA
字号:
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class Ch15_11_JumpText 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);
		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;
		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);
			x += w;
		}
		if(Math.random() > 0.99)
		{
			directX1 = -directX1;
		}
		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 arg0) {
	}

	public void mouseEntered(MouseEvent arg0) {
	}

	public void mouseExited(MouseEvent arg0) {
	}

	public void mousePressed(MouseEvent e) {
		if(thread == null)
		{
			start();
		}
		else
		{
			thread = null;
		}
	}

	public void mouseReleased(MouseEvent arg0) {
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -