【Java开源代码栏目提醒】:网学会员为需要Java开源代码的朋友们搜集整理了CryptoApplet.java相关资料,希望对各位网友有所帮助!
import java.awt.*;
import java.applet.*;
import java.io.PrintStream;
// Copyright 1997 SmartSoft, Inc. - Alberto Ricart
// http://www.smartsoft.com
//
// Use of this applet for educational purposes is granted
// by the author. Any other use by permission only.
public class CryptoApplet extends Applet implements Runnable
{
public static String id = "$Id: CryptoApplet.java,v 1.5 1999-04-15 12:32:02-05 aricart Exp aricart $";
private String message;
private Color bgColor;
private Color fgColor;
private int speed;
private int fontSize;
private StringBuffer cryptedMessage;
private String cMessage; // string version of cryptedMessage
private Dimension appletPane;
private FontMetrics fontMetrics;
private int range = 254;
private int messageLength;
private Thread t;
private long millis;
private int baseLine;
private boolean isSuspended = true;
private void resetRange()
{
range = 254;
}
public void init()
{
appletPane = getSize();
setBGColor(getParameter("bgcolor"));
setFGColor(getParameter("fgcolor"));
setMessage(getParameter("message"));
setSpeed(getParameter("speed"));
setFontSize(getParameter("fontSize"));
}
public void setFontSize(String aSize)
{
try
{
fontSize = Integer.parseInt(aSize);
}
catch(NumberFormatException e)
{
fontSize = 32;
}
Font aFont = new Font("Courier", Font.BOLD, fontSize);
setFont(aFont);
fontMetrics = getFontMetrics(aFont);
int middle = appletPane.height / 2;
int lineheight = (fontMetrics.getAscent() - fontMetrics.getDescent()) / 2;
System.out.println("Middle: " + middle);
System.out.println("line h: " + lineheight);
baseLine = (middle + lineheight);
}
public void setSpeed(String aSpeed)
{
try
{
speed = Integer.parseInt(aSpeed);
}
catch(NumberFormatException e)
{
speed = 125;
}
}
public void setMessage(String aMessage)
{
boolean wasAlive = false;
if(t != null && t.isAlive())
{
wasAlive = true;
t.suspend();
}
if(aMessage == null || aMessage.equals(""))
{
message = "Alberto made this!";
}
else
{
message = aMessage;
}
// create the buffer and garble everything
messageLength = message.length();
cryptedMessage = new StringBuffer(message);
for(int i=0; i<messageLength; i++)
{
cryptedMessage.setCharAt(i, (char)Random(32,122));
}
if(wasAlive)
{
resetRange();
t.resume();
}
}
public void start()
{
t = new Thread(this);
millis = System.currentTimeMillis();
isSuspended = false;
resetRange();
t.start();
}
public void stop()
{
t.stop();
isSuspended = true;
}
public void run()
{
while (! message.equals(cryptedMessage.toString()))
{
randomize();
repaint();
try
{
Thread.sleep((long)Random(speed, 0));
}
catch (Exception e)
{
System.out.println(e);
}
}
if(t.isAlive())
{
t.stop();
}
}
public void setBGColor(String stringValue)
{
Color c = colorFromString(stringValue);
if(c == null) c = Color.white;
bgColor = c;
}
public void setFGColor(String stringValue)
{
Color c = colorFromString(stringValue);
if(c == null) c = Color.black;
fgColor = c;
}
private Color colorFromString(String colorValue)
{
Color color = null;
if(colorValue == null || colorValue.equals(""))
{
return null;
}
else
{
if(colorValue.indexOf("#") != -1)
{
// get the text after the #
colorValue = colorValue.substring(colorValue.indexOf("#"));
}
try
{
color = new Color(Integer.parseInt(colorValue, 16));
}
catch(Exception e)
{
color = null;
}
}
return color;
}
public void paint(Graphics g)
{
int messageWidth = fontMetrics.stringWidth(cMessage);
int messageStart = Math.max((appletPane.width - messageWidth) / 2, 0);
setBackground(bgColor);
setForeground(fgColor);
g.setXORMode(bgColor);
g.setPaintMode();
g.setColor(fgColor);
g.drawString(cMessage, messageStart, baseLine);
}
public final void randomize()
{
if (System.currentTimeMillis() - millis > 1000)
{
range = (int)((double)range * 0.65);
millis = System.currentTimeMillis();
}
for (int i = 0; i < cryptedMessage.length(); i++)
{
if (cryptedMessage.charAt(i) != message.charAt(i))
{
cryptedMessage.setCharAt(i, (char)Random(message.charAt(i) - range, message.charAt(i) + range));
}
}
cMessage = cryptedMessage.toString();
}
public boolean mouseDown(Event evt, int x, int y)
{
// don't care about the details...
if(t.isAlive())
{
if(isSuspended)
{
t.resume();
isSuspended = false;
}
else
{
t.suspend();
isSuspended = true;
if(evt.controlDown())
{
cMessage="1997 A. Ricart!";
repaint();
}
}
}
else
{
setMessage(message);
start();
}
return true;
}
public static final int Random(int i, int j)
{
return (int)Math.round(Math.random() * (i - j) + j);
}
}
上一篇:
CrsBean.java
下一篇:
那些令你为之触动的好句子,感慨万分