📄 ticker.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
public class Ticker extends Applet implements Runnable{
//设定全局参数
private Thread m_thread;
private Image m_imgBuffer = null;
private int y_pos = 0; // 纵向位置
private int cur_news = 0; // 当前news项目
private int cur_pos = 0; // 当前鼠标光标所在位置
private boolean pause = false; // 是否暂停
private boolean m_over = false; // 是否鼠标掠过
private Vector news = new Vector(); //包含显示的文字
private Vector urls = new Vector(); //包含相关的URL
//设定默认参数
private int size = 10;
private Font ft = new Font("TimesRoman",Font.PLAIN,size);
private String filename = "news.txt";
private String target = "_new";
private int height;
private int xmargin = 10;
private int ymargin = 0;
private Color irect = new Color(8388736);
private Color orect = new Color(0);
private Color ntext = new Color(0);
private Color htext = new Color(16711680);
private int speed = 100;
//初始化
public void init(){
//获取小应用程序给定的参数
String param;
param = getParameter("size");
if (param != null) size = Integer.parseInt(param);
param = getParameter("font");
if (param != null) ft = new Font(param,Font.PLAIN,size);
param = getParameter("target");
if (param != null) target = param;
param = getParameter("xmargin");
if (param != null) xmargin = Integer.parseInt(param);
param = getParameter("ymargin");
if (param != null) ymargin = Integer.parseInt(param);
param = getParameter("irect");
if (param != null) irect = new Color(Integer.parseInt(param,16));
param = getParameter("orect");
if (param != null) orect = new Color(Integer.parseInt(param,16));
param = getParameter("ntext");
if (param != null) ntext = new Color(Integer.parseInt(param,16));
param = getParameter("htext");
if (param != null) htext = new Color(Integer.parseInt(param,16));
param = getParameter("filename");
if (param != null) filename = param;
param = getParameter("speed");
if (param != null) speed = Integer.parseInt(param);
setFont(ft);
FontMetrics fm = getFontMetrics(ft);
height = fm.getAscent() + fm.getDescent();
if (height > ymargin) ymargin = height; //设定的纵向边框空白处不能太小
//读取news文件中的信息
readFile();
}
//鼠标移入
public boolean mouseEnter(Event e, int x, int y){
stop();
m_over = true;
cur_pos = e.y;
repaint();
return true;
}
//鼠标移出
public boolean mouseExit(Event e, int x, int y){
start();
m_over = false;
repaint();
return true;
}
//鼠标移动
public boolean mouseMove(Event e, int x, int y){
cur_pos = e.y;
m_over = true;
repaint();
return true;
}
//单击鼠标
public boolean mouseDown(Event e, int x, int y){
if (urls.elementAt(cur_news) instanceof URL){
URL myURL = (URL) urls.elementAt(cur_news);
getAppletContext().showDocument(myURL,getParameter("target"));
}
return true;
}
//本函数实现读取文件
private void readFile()
{
String line;
try{
URL url = new URL(getDocumentBase(), filename);
try{ //读取文件
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
do{
line = in.readLine();
if (line != null){
try{
StringTokenizer st = new StringTokenizer(line, "|");
news.addElement(st.nextToken()); //获取要显示的文字
if (st.hasMoreTokens()){ //得到url
urls.addElement(new URL(getDocumentBase(), st.nextToken()));
}
else{ // 如果没有url
urls.addElement("nourl");
}
}catch (NoSuchElementException nsee){} //抛出异常
}
}while (line != null);
}
catch (IOException e){
news.addElement("Could not open news feed file.");
urls.addElement("nourl");
System.err.println("Could not open news feed file.");
}
}catch (MalformedURLException e){}
}
public void paint(Graphics g){
if (m_imgBuffer == null){ //双缓冲
m_imgBuffer = createImage(size().width, size().height);
render(g); //显示
}
g.drawImage(m_imgBuffer, 0, 0, null);
}
public void update(Graphics g){
render(g);
paint(g);
}
//本函数实现显示
public void render(Graphics g)
{
//画背景区域
g = m_imgBuffer.getGraphics();
g.setColor(orect);
g.fillRect(0, 0, size().width, size().height);
g.setColor(irect);
g.fillRect(xmargin, ymargin, size().width - 2 * xmargin, size().height - 2 * ymargin);
g.setColor(ntext);
FontMetrics fm = getFontMetrics(ft); //计算字符宽度
boolean reset = false; //重新设置y_pos的值
int y = 0;
String s = "";
while (y + y_pos < size().height){
//计算news中每个项目的位置
for (int i = 0; i < news.size(); i++){
if (m_over){ //如果鼠标的光标移动到文字上
int b1 = y_pos + 14; //news边框的顶部
int b2 = y_pos + 14; //news边框的底部
//寻找当前的news项目,并将其突出显示
while (b2 < size().height - height){
for (int j = 0; j < news.size(); j++){
b1 = b2; //设置顶部边框
StringTokenizer st = new StringTokenizer((String) news.elementAt(j)," ",false);
String t = "";
while (st.hasMoreTokens()){
s = "";
while (fm.stringWidth(s + t) < size().width - 2 * xmargin - 5 && st.hasMoreTokens()){
if (!t.equals("")) s += t + " ";
t = st.nextToken();
}
if (!st.hasMoreTokens() && fm.stringWidth(s + t) > size().width - 2 * xmargin - 5){
b2 += 2 * height;
}
else{
b2 += height;
}
}
if (cur_pos > b1 && cur_pos < b2){//搜寻当前的news
cur_news = j;
}
b2 += 20; //计算底部边框
}
}
//突出显示项目
if (i == cur_news && (urls.elementAt(i) instanceof URL)) g.setColor(htext);
else g.setColor(ntext);
}
//当新项目出现时,使滚动暂停
if (y_pos + y == 3){
pause = true;
}
//显示文字
String t = "";
StringTokenizer st = new StringTokenizer((String) news.elementAt(i)," ",false);
while (st.hasMoreTokens()){
s = "";
while (fm.stringWidth(s + t) < size().width - 2 * xmargin - 5 && st.hasMoreTokens()){
if (!t.equals("")) s += t + " ";
t = st.nextToken();
}
y += height;
if (st.hasMoreTokens()){
if ( y + y_pos > ymargin - height && y + y_pos < size().height - ymargin - 2 * height){
g.drawString(s, xmargin + 2, y + y_pos + ymargin);
}
}
else{ //对最后一行做特殊处理
if (fm.stringWidth(s + t) > size().width - 2 * xmargin - 5){
//如果没有足够的空间,在另一行显示
if ( y + y_pos > ymargin - height && y + y_pos < size().height - ymargin - 2 * height){
g.drawString(s, xmargin + 2, y + y_pos + ymargin);
}
y += height;
if ( y + y_pos > ymargin - height && y + y_pos < size().height - ymargin - 2 * height){
g.drawString(t, xmargin + 2, y + y_pos + ymargin);
}
}
else{ //否则在同一行显示
if ( y + y_pos > ymargin - height && y + y_pos < size().height - ymargin - 2 * height){
g.drawString(s + t, xmargin + 2, y + y_pos + ymargin);
}
}
}
}
y += 20;
}
if (y + y_pos < 0) reset = true;
}
if (reset) y_pos = 0; //重新设置滚动器的纵向位置
}
public void start(){
m_thread = new Thread(this);
m_thread.start();
}
public void stop(){
m_thread.stop();
}
public void run(){
try{
while(true){
if (pause){ //当新的项目出现时,暂停
m_thread.sleep(4000);
pause = false;
}
m_thread.sleep(speed);
if (!m_over) y_pos--; //实现滚动
repaint();
}
}
catch (InterruptedException e){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -