📄 newsscroller.java
字号:
/*
Reads "news" from text file with headlines and URLs.
Vertical scroll.
Parameters (see also WebBase):
DATAFILE File (relative to the HTML document) to get news from
def: news.txt
CLICKCOLOR Color of "clickable" text
def: blue
HEADCOLOR Color of headline text
def: red
FONT Font to use (Arial,Courier,Dialog,TimesRoman)
def: Dialog
FONTSIZE Size (in pts.) of message font
def: 12
MOUSEPAUSE Causes scrolling to stop while the mouse cursor
is over the applet - the value can be anything
def: null (no pause)
TOPPAUSE Pauses scrolling at the "top" of each message
(in milliseconds);
def: 0 (no pause)
Copyright 1998 by E. A. Graham, Jr.
Free to use or modify.
*/
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;
public class NewsScroller extends WebBase {
// fonts for the headline and message (headline is bold version of message)
Font fontHeadline,fontMessage;
// the various colors
Color clickColor,headColor;
// the current "top" message - used for pausing
NewsMessage msgTop = null;
// The File and the Messages
String fileName = null;
Vector vMessages = new Vector();
Vector vNewMessages = new Vector();
int gap = 15; // gap between messages - tweak if necessary
boolean mouse_pause = false; // stop messages from scrolling
boolean pause_now = false;
long top_pause = 0;
//************************************************************************
// initialization time!
//************************************************************************
public void init() {
super.init();
//********************************************************************
// get the HTML parameters or set to defaults
//********************************************************************
String sp;
sp = ReadText("DATAFILE");
fileName = (sp == null) ? "news.txt" : sp;
clickColor = ReadColor("CLICKCOLOR",Color.blue);
headColor = ReadColor("HEADCOLOR",Color.red);
sp = ReadText("MOUSEPAUSE");
mouse_pause = (sp != null);
sp = ReadText("TOPPAUSE");
top_pause = (sp != null) ? Long.parseLong(sp) : 0;
//********************************************************************
// get the HTML font parameters and set them up
//********************************************************************
sp = ReadText("FONTSIZE");
int fs = (sp != null) ? Integer.parseInt(sp) : 12;
sp = ReadText("FONT");
if (sp==null) sp = "Dialog";
fontMessage = new Font(sp,Font.PLAIN,fs);
fontHeadline = new Font(sp,Font.BOLD,fs);
updateNews();
}
public void stop() {
super.stop();
}
// draw the stuff
public synchronized void update(Graphics g) {
if(loaded) {
super.update(g);
// loop over the loaded news items...
for (Enumeration e = vMessages.elements() ; e.hasMoreElements() ;) {
((NewsMessage)e.nextElement()).draw(appGC,borderWidth,headColor,foreColor,clickColor);
}
//...next comes the border...
drawBorder(maxWidth,maxHeight,borderWidth);
//...fade the edges
g.drawImage(appImage, 0, 0, this);
}
}
public boolean handleEvent(Event event) {
return super.handleEvent(event);
}
// handle any clicks, okay?
public boolean mouseDown(Event ev,int x, int y) {
for (Enumeration e = vMessages.elements() ; e.hasMoreElements() ;) {
NewsMessage m = (NewsMessage)e.nextElement();
URL clicked = m.click(y);
if (clicked != null) {
getAppletContext().showDocument(clicked,m.getTarget());
break;
}
}
return true;
}
// start and stop the scrolling
public boolean mouseEnter(Event ev,int x, int y) {
if (mouse_pause) pause_now = true;
return true;
}
public boolean mouseExit(Event ev,int x, int y) {
pause_now = false;
return true;
}
// scroll all the messages
synchronized void doAppThing() {
int maxM = vMessages.size(),
maxN = vNewMessages.size(),
lastBottom;
NewsMessage m,lastM;
boolean reset = false;
// check for new messages - I don't know if this will work on updates, but it should be interesting
if (maxM==0) {
if (maxN > 0) {
vMessages = (Vector)vNewMessages.clone();
}
else {
return;
}
}
// has the top message gone off? take it off and stick it on bottom!
m = (NewsMessage)vMessages.firstElement();
lastM = (NewsMessage)vMessages.lastElement();
if (m.bottom() + scrollUnit <= 0) {
vMessages.removeElement(m);
lastBottom = lastM.bottom() + gap;
m.resetTop(lastBottom);
vMessages.addElement(m);
}
// check for the mouse pause conditions
if (pause_now) return;
// scroll the visible messages
for (Enumeration e = vMessages.elements() ; e.hasMoreElements() ;) {
m = (NewsMessage)e.nextElement();
m.move(scrollUnit);
}
m = (NewsMessage)vMessages.firstElement();
if (m.top() <= (Math.abs(borderWidth)+1) && top_pause > 0 && !m.equals(msgTop)) {
try {
Thread.sleep(top_pause);
msgTop = m;
}
catch (InterruptedException e) {}
}
}
// (re)read the news file
synchronized void updateNews() {
InputStream conn;
DataInputStream dis = null;
URL theURL = null;
if (fileName==null) return;
try {
if (fileName.indexOf("http://") >= 0 ) {
theURL = new URL(fileName);
}
else {
theURL = new URL(getDocumentBase(),fileName);
}
try {
int mode = 0,
lastbottom = maxHeight;
NewsMessage m = null;
String line;
conn = theURL.openStream();
dis = new DataInputStream(new BufferedInputStream(conn));
while( (line=dis.readLine())!=null) {
if (line.startsWith("#") || line.length()==0) continue;
if (line.substring(0,4).equalsIgnoreCase("@END")) {
if (m!=null) {
if (m.getTarget()==null) m.setTarget(frameTarget);
lastbottom = m.bottom() + gap;
vNewMessages.addElement(m);
}
mode = 0;
continue;
}
if (line.substring(0,4).equalsIgnoreCase("@URL") && m!=null) {
m.setLink(getDocumentBase(),line.substring(4));
continue;
}
if (line.substring(0,6).equalsIgnoreCase("@FRAME") && m!=null) {
m.setTarget(line.substring(6));
continue;
}
switch (mode) {
case 0:
if (line.substring(0,5).equalsIgnoreCase("@HEAD")) {
mode = 1;
m = new NewsMessage(lastbottom,maxWidth-(2*borderWidth),appGC,fontHeadline,fontMessage);
m.addHeadLine(line.substring(5));
}
break;
case 1:
if (line.substring(0,4).equalsIgnoreCase("@MSG")) {
mode = 2;
m.addMessageLine(line.substring(4));
}
else {
m.addHeadLine(line);
}
break;
default:
m.addMessageLine(line);
break;
}
}
}
catch (IOException e) {}
}
catch (MalformedURLException e) {}
}
}
class NewsMessage {
int top,height,
currentY,
maxWidth = 0;
URL link = null;
String target = null;
// the first element in each of these is the font, the second is the font metrics
Vector message = null;
Vector headline = null;
NewsMessage (int startat,int mw,Graphics g,Font fontHead,Font fontMessage) {
FontMetrics fm;
height = 0;
top = startat;
headline = new Vector();
headline.addElement(fontHead);
fm = g.getFontMetrics(fontHead);
headline.addElement(fm);
message = new Vector();
message.addElement(fontMessage);
fm = g.getFontMetrics(fontMessage);
message.addElement(fm);
maxWidth = mw - 10;
}
public void draw (Graphics g,int bw,Color headColor,Color msgColor,Color urlColor) {
// is this thing even in the picture?
if (top+height <= 0) return;
// set up and do the headline
// then do the message
currentY = top;
drawline(g,headline,bw,headColor);
drawline(g,message,bw,((link==null) ? msgColor : urlColor));
}
public void move(int amount) { top -= amount; }
public void resetTop(int startat) { top = startat; }
public int bottom() { return top+height; }
public int top() { return top; }
// set/return URL for this message
public void setLink(URL docbase,String s) {
try {
if (s.indexOf("http://") >= 0 ) {
link = new URL(s);
}
else {
link = new URL(docbase,s);
}
}
catch (MalformedURLException e) {};
}
public URL click(int y) {
if (y>=top && y<=top+height) return(link);
return(null);
}
// set/return the target
public void setTarget(String t) { target = new String(t); }
public String getTarget() { return target; }
public void addHeadLine(String s) {
addLine(headline,s);
}
public void addMessageLine(String s) {
addLine(message,s);
}
// calcuate where the lines will wrap and update the height of the message
void addLine(Vector v,String line) {
try {
FontMetrics fm = (FontMetrics)v.elementAt(1);
String s,t;
int x,y,
h;
h = fm.getHeight();
if (v.size() > 2) {
s = new String ((String)v.lastElement() + " " + line);
v.removeElementAt(v.size()-1);
height -= h;
}
else {
s = line;
}
for (x=0; s.length() > 0; x=y+1) {
y = s.indexOf(' ',x);
if (y== -1) {
if ((fm.stringWidth(s) > maxWidth) && x>0) {
v.addElement(s.substring(0,x));
s = s.substring(x);
height += h;
}
v.addElement(s);
height += h;
s = "";
}
else {
t = s.substring(0,y);
if (fm.stringWidth(t) > maxWidth) {
v.addElement(s.substring(0,x));
s = s.substring(x);
y = -1;
height += h;
}
}
}
}
catch (NoSuchElementException e) {}
}
// draw the indicated lines
void drawline(Graphics g,Vector v,int bw,Color c) {
try {
FontMetrics fm = (FontMetrics)v.elementAt(1);
int h,i,y;
h = fm.getHeight();
y = fm.getAscent(); // distance to baseline
g.setColor(c);
g.setFont((Font)v.firstElement());
for (i=2; i<v.size(); i++) {
String s = (String)v.elementAt(i);
g.drawString(s,bw+5,currentY+y);
currentY += h;
}
}
catch (NoSuchElementException e) {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -