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

📄 scroller.java

📁 java对文件进行操作的例子
💻 JAVA
字号:
/* 
*  从一个文件中随机地读取一些信息,并在屏幕上以滚动方式显示这些信息,
*  同时用户还可以设置显示框的背景色以及和文字的大小、颜色、字体等属性。
*/
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.lang.*;
import java.io.*;
import java.util.*;
//定义Scroller类
public class Scroller extends Applet implements Runnable {
    Thread timerThread;//定义线程
    int curX; //当前位置
    int scrollX, scrollY, scrollW, scrollH;//滚动位置参数
    int shift;
    int delay;
    Font font;//字体
    FontMetrics fontM;   
    String message;//信息
    int messageW;//信息框宽度
    int messageH;//信息框高度
    String fontName;//字体名称
    int fontBold;//字体风格
    int rec_r, rec_g, rec_b;
    int txt_r, txt_g, txt_b;
    Color recColor;//信息框颜色
    Color txtColor;//文字颜色
	//获取Applet基本信息
	public String getAppletInfo() {
		return "Scroller";
    	}
    	//应用二维数组定义基本参数
	public String[][] getParameterInfo() {
		String[][] info = {
			{"font", "string", "a font name"},
			{"fontsize", "int", "font size"},
			{"fontbold", "0=on|1=off", "set bold"},
			{"shift", "int", "left shifting of message"},
			{"delay", "int", "scroll delay"},
			{"rectangle", "x y width heigh (int)", "coodrinates of the rectangle"},
			{"message", "string", "message to write"},
			{"rect_color", "red green blue (int)", "rectangle background color"},
			{"text_color", "red green blue (int)", "text color"},
			{"filename", "string", "filename"},
			{"filelines", "int", "lines in filename"},
		};
		return info;
	}
	// 函数: getParameter
	// 输入: 参数,默认值
	// 输出: 存在并且有效的参数值或默认值
	String getParameter(String p, String def) {
		p = getParameter(p);
		if (p == null) return def;
		return p;
	}
	// 函数: getParameterInt
	// 输入: 参数,默认值
	// 输出:存在并且有效的参数值或者默认值
	// 注意: 与getParameter函数相同,但是返回一个整数值
	int getParameterInt(String p, int def) {
		p = getParameter(p, null);
		if (p != null) {
			return Integer.parseInt(p);
		}
		return def;
	}
	// 函数: getParameterList
	// 输入: 参数,作为分隔符的字符
	// 输出: 参数列表
	String[] getParameterList(String param, char cs) {
		String p = param;
		String[] result = null;
		if (param != null) {
			int pos = 0;
			int count = 0;
			while ((pos = p.indexOf(cs)) >= 0) {
				p = p.substring(pos + 1);
				count++;
			}
			if (p.length() > 0) {
			count++;
			}
			result = new String[count];
			p = param;
			for (int i=0; i<result.length; i++) {
				pos = p.indexOf(cs);
				if (pos < 0) {
					result[i] = p.substring(0, p.length());
					p = null;
				} else {
					result[i] = p.substring(0, pos);
					p = p.substring(pos+1);
				}
			}
		}
		return result;
	}
	// 函数: DirectoryURL
	// 输入: 无
	// 输出: 文件目录的Url值
	public String DirectoryURL() {
		URL thisURL;
		String thisURLString,newURLString;
		int SlashPos = -1,tempInt = -1;
		thisURL = getDocumentBase();
		thisURLString = thisURL.toString();
		SlashPos = thisURLString.lastIndexOf("/");
		newURLString = thisURLString.substring(0, (SlashPos + 1));
		return newURLString;
	}
	// 函数: GetFortuneString
	// 输入: 文件名,要显示的行数
	// 输出: 显示的信息
	public String GetFortuneString(String FileName, int TheNum) {
		String TheLine,result = "NO FORTUNE";
		String TheUrl;
		int cnt = 1;
		TheUrl = DirectoryURL() + FileName;
		try {
			URL url = new URL(TheUrl);
			try {
				InputStream TheFile = url.openStream();
				try {
					DataInputStream MyData = new DataInputStream(TheFile);
					try {
						while ((TheLine = MyData.readLine()) != null) {
							if (TheNum == cnt) {
								result = TheLine;
							}
							cnt++;
						}
					} catch (Exception e) {
						System.out.println("Error ReadLine " + e.toString());
					}
				} catch (Exception e) {
					System.out.println("Error DataInputStream " + e.toString());
				}
			} catch (Exception f) {
				System.out.println("Error OpenStream " + f.toString());
			}
		} catch (Exception g) {
			System.out.println("Error URL" + g.toString());
		}
		return result;
	}
	//初始化程序
	public void init() {
		Dimension d = size();
		String[] paramlist;
		String p, FortuneFile;
		int SlashPos;
		int TempInt = 0;
		Random TheRandom;
		float MyRand;
		int MyInt, FortuneLines;
		FortuneLines = getParameterInt("filelines", -1);
		if (FortuneLines == -1) {
			destroy();
		}
		// 信息源文件(默认值scroll.txt)
		FortuneFile = getParameter("filename", "scroll.txt");
		// 读取随机行
		TheRandom = new Random();
		MyRand = TheRandom.nextFloat();
		MyRand = (MyRand * FortuneLines) + 1;
		MyInt = (int)MyRand;
		// 读取信息
		message=GetFortuneString(FortuneFile, MyInt);
		// pixel shift (默认值 4)
		shift = getParameterInt("shift", 4);
		// 延迟时间(默认值为 40)
		delay = getParameterInt("delay", 40);
		// 字体名称(默认值:"Courier") 
		fontName = getParameter("font", "Courier");
		// 字体是否为粗体(默认值:1=正常/其它值,0=粗体)
		fontBold = getParameterInt("fontbold", 1);
		if (fontBold == 0) {
			font = new Font(fontName, Font.BOLD, getParameterInt("fontsize", 12));
		} else {
			font = new Font(fontName, Font.PLAIN, getParameterInt("fontsize", 12));
		}
		//矩形区域大小(默认值为Applet大小)
		p = getParameter("rectangle", "0 0 "+d.width+" "+d.height);
		paramlist =getParameterList(p, ' ');
		scrollX = Integer.parseInt(paramlist[0]);
		scrollY = Integer.parseInt(paramlist[1]);
		scrollW = Integer.parseInt(paramlist[2]);
		scrollH = Integer.parseInt(paramlist[3]);
		// 矩形显示框的背景颜色(默认值为黑色)
		p = getParameter("rect_color", "0 0 0");
		paramlist = getParameterList(p, ' ');
		rec_r = Integer.parseInt(paramlist[0]);
		rec_g = Integer.parseInt(paramlist[1]);
		rec_b = Integer.parseInt(paramlist[2]);
		recColor = new Color(rec_r, rec_g, rec_b);
		// 信息文字颜色(默认值为白色)
		p = getParameter("text_color", "255 255 255");
		paramlist = getParameterList(p, ' ');
		txt_r = Integer.parseInt(paramlist[0]);
		txt_g = Integer.parseInt(paramlist[1]);
		txt_b = Integer.parseInt(paramlist[2]);
		txtColor = new Color(txt_r, txt_g, txt_b);
	}
	//启动
	public void start() {
		curX = scrollW+scrollX;
		startThread();
	}
	//停止
	public void stop() {
		timerThread = null;
	}
	//开始线程
	void startThread() {
		timerThread = new Thread(this);
		timerThread.start();
	}
    
	//paint()方法
	public void paint(Graphics g) {
		if (fontM == null) {
			fontM = g.getFontMetrics(font);
			messageW = fontM.stringWidth(message);
			messageH = fontM.getHeight();
		}
		update(g, g.getClipRect());
	}
	//更新滚动文字
	public void update(Graphics g) {
		int y = scrollY+(scrollH-messageH)/2;
		g.copyArea(scrollX+shift, y, scrollW-shift, messageH, -shift, 0);
		update(g, new Rectangle(scrollX+scrollW-shift, scrollY, shift, scrollH));
		curX -= shift;
		if (curX < -messageW) {
			curX = scrollW+scrollX;
		}
	}
	// 更新背景框区域
	void update(Graphics g, Rectangle r) {
		g.clipRect(r.x, r.y, r.width, r.height);
		g.clipRect(scrollX, scrollY, scrollW, scrollH);
		g.setColor(recColor);
		g.fillRect(r.x, r.y, r.width, r.height);
		g.setColor(txtColor);
		g.setFont(font);
		g.drawString(message, curX, (scrollH-messageH)/2 + scrollY+fontM.getAscent());
	}
	// 运行
	public void run() {
		Thread curThread = Thread.currentThread();
        
		while (timerThread == curThread) {
			repaint();
			try {
				curThread.sleep(delay);
			} catch (Exception e) {
			}
		}
	}

}

⌨️ 快捷键说明

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