📄 webbase.java
字号:
/*
Base class for Ed's web applets
Provides some of the base functionality used for my
web applets.
Parameters:
SLEEPTIME Milliseconds between updates
def: DEFAULT_SLEEP
SCROLLBY Increment to scroll messages on updates
def: DEFAULT_SCROLL
BORDER Draw a border around it all
def: 0 Indicates width of the 3D border
Negative values makes an "inset" border
Uses background color to draw it
FOREGROUND Color of the text
def: black
BACKGROUND Color of the background
def: white
PICTURE Background image - must be .GIF or .JPG
def: none
TARGET Target frame name for URL jumps
def: _top
Copyright 1998 by E. A. Graham, Jr.
Free to use or modify.
*/
import java.awt.*;
import java.applet.*;
import java.net.URL;
import java.net.MalformedURLException;
public class WebBase extends Applet implements Runnable {
private final int DEFAULT_SLEEP = 150;
private final int DEFAULT_SCROLL = 10;
Thread myThread = null; //The main thread
int maxWidth,maxHeight, // Dimensions of the applet area
borderWidth; // draw a 3-D border around the thing
// adjust these two for scrolling parameters
int scrollUnit; // the incremental scrolling distance in pixels
long sleepTime; // controls the speed of the scroll
Image appImage; // The applet 'image' object
Graphics appGC; // GC used for applet 'image'
Color foreColor,backColor; // foreground and background colors
static boolean loaded = false; // helps determine if we're running or not
Image bPicture = null;
String frameTarget = null;
// initialization time!
public void init() {
String tparam;
// get the size of the applet
maxWidth = size().width;
maxHeight = size().height;
// used for children of this class to target URL jumps
// defaults to "_top"
tparam = ReadText("TARGET");
frameTarget = new String(tparam==null ? "_top" : tparam);
// get some numeric parameters
tparam = ReadText("SleepTime");
sleepTime = (tparam != null) ? Long.parseLong(tparam) : DEFAULT_SLEEP;
tparam = ReadText("ScrollBy");
scrollUnit = (tparam != null) ? Integer.parseInt(tparam) : DEFAULT_SCROLL;
tparam = ReadText("BORDER");
borderWidth = (tparam == null) ? 0 : Integer.parseInt(tparam);
// the color parameters
foreColor = ReadColor("FOREGROUND",Color.black);
backColor = ReadColor("BACKGROUND",Color.white);
setBackground(backColor);
// a background picture (go ahead and wait for full load)
tparam = ReadText("PICTURE");
if (tparam != null) {
MediaTracker imgTrack;
URL theURL = null;
try {
if (tparam.indexOf("http://") >= 0 ) {
theURL = new URL(tparam);
}
else {
theURL = new URL(getDocumentBase(),tparam);
}
bPicture = getImage(theURL);
if (bPicture != null) {
imgTrack = new MediaTracker(this);
imgTrack.addImage(bPicture,0);
try {
imgTrack.waitForAll();
}
catch (Exception e) {
System.out.println(e);
bPicture = null;
}
}
}
catch (MalformedURLException e) {}
}
appImage = createImage(maxWidth, maxHeight);
appGC = appImage.getGraphics();
}
// reads an HTML-supplied parameter
String ReadText(String myParam){
String tempString = null;
try {
tempString = getParameter(myParam);
}
catch (Exception e) {
System.out.println(e);
}
return tempString;
}
// reads in a color HTML-supplied parameter (either R,G,B or hex RGB value)
Color ReadColor(String sParam, Color defColor) {
String tparam = ReadText(sParam);
Color tcolor;
int rc,gc,bc;
rc = gc = bc = -1;
if (tparam != null) {
if (tparam.startsWith("#")) {
if (tparam.length()==7) {
rc = Integer.parseInt(tparam.substring(1,3),16);
gc = Integer.parseInt(tparam.substring(3,5),16);
bc = Integer.parseInt(tparam.substring(5),16);
}
}
else {
int x,y;
x = tparam.indexOf(',');
y = tparam.lastIndexOf(',');
if (x>0 && y>0 && x!=y) {
rc = Integer.parseInt(tparam.substring(0,x));
gc = Integer.parseInt(tparam.substring(x+1,y));
bc = Integer.parseInt(tparam.substring(y+1));
}
}
}
tcolor = (rc>=0 && rc<=255 && gc>=0 && gc<=255 && bc>=0 && bc<=255) ? new Color(rc,gc,bc) : defColor;
return(tcolor);
}
// start/stop the applet thread
public void start() {
if(myThread == null) {
myThread = new Thread(this);
myThread.start();
loaded = true;
}
}
public void stop() {
if((myThread != null) && myThread.isAlive()) {
myThread.stop();
}
loaded = false;
myThread = null;
}
// this triggers the update and scrolls the text by updating the X position
public void run() {
Thread me = Thread.currentThread();
me.setPriority(Thread.MIN_PRIORITY);
appImage = createImage(maxWidth, maxHeight);
appGC = appImage.getGraphics();
repaint();
while(myThread == me){
while(loaded){
doAppThing();
repaint();
try {
Thread.sleep(sleepTime);
}
catch(InterruptedException e){}
}
}
}
synchronized void doAppThing() {
}
// we don't want it to do anything here...
public void paint(Graphics g)
{
}
public synchronized void update(Graphics g) {
if (loaded) {
// if there's a background picture, use it
if (bPicture != null) {
appGC.drawImage(bPicture,0, 0, maxWidth, maxHeight,this);
}
// otherwise, draw background rectangle...
else {
appGC.setColor(backColor);
appGC.fillRect(0, 0, maxWidth, maxHeight);
}
}
}
// draws a 3-D border
void drawBorder(int w,int h,int bw) {
if (bw == 0) return;
Color bc_light,bc_dark;
int pWidth = Math.abs(bw);
Polygon bp_below = new Polygon();
Polygon bp_above = new Polygon();
if (backColor.equals(Color.white)) {
bc_light = backColor.darker();
bc_dark = bc_light.darker();
}
else {
bc_light = backColor.brighter();
bc_dark = backColor.darker();
}
// add the vertices to the polygons
bp_above.addPoint(0,0);
bp_above.addPoint(w,0);
bp_above.addPoint(w-pWidth,pWidth);
bp_above.addPoint(pWidth,pWidth);
bp_above.addPoint(pWidth,h-pWidth);
bp_above.addPoint(0,h);
bp_below.addPoint(w,0);
bp_below.addPoint(w,h);
bp_below.addPoint(0,h);
bp_below.addPoint(pWidth,h-pWidth);
bp_below.addPoint(w-pWidth,h-pWidth);
bp_below.addPoint(w-pWidth,pWidth);
// draw the border
if (bw < 0) {
appGC.setColor(bc_dark);
}
else {
appGC.setColor(bc_light);
}
appGC.fillPolygon(bp_above);
if (bw < 0) {
appGC.setColor(bc_light);
}
else {
appGC.setColor(bc_dark);
}
appGC.fillPolygon(bp_below);
}
// the next two methods make a default TimesRoman font
// to fit in a certain height
Font appFont(int ht) {
return appFont(ht,Font.PLAIN);
}
Font appFont(int ht, int fAttr) {
return appFont(ht,"TimesRoman",fAttr);
}
// makes a font for the applet
Font appFont(int ht,String fName,int fAttr) {
Font tf = new Font(fName, fAttr, 14);
setFont(tf);
FontMetrics tfm = getFontMetrics(tf);
int fh = tfm.getHeight();
fh = 14 * (maxHeight-2) / fh; // scale the font height
tf = new Font(fName, fAttr, fh); // "make" the font
return tf;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -