查看Applet1.java的内容:
import java.awt.*;
import java.applet.*;
/**
* This class reads PARAM tags from its HTML host page and sets
* the color and label properties of the applet. Program execution
* begins with the init() method.
*/
public class Applet1 extends Applet implements Runnable
{
/**
* The entry point for the applet.
*/
Font ft; //文字字体对象实例
String theString, font, style; //移动的字符串、字体类型及样式
int size, speed; //文字的大小,移动速度
int xSet, Max, Min;//文字每次移动的位移量,右边界和左边界
public void init()
{
initForm();
usePageParams();
// TODO: Add any constructor code after initForm call.
}
private final String labelParam = "label";
private final String backgroundParam = "background";
private final String foregroundParam = "foreground";
private final String param_string="string";
private final String param_font="font";
private final String param_style="style";
private final String param_size="size";
private final String param_speed="speed";
/**
* Reads parameters from the applet's HTML host and sets applet
* properties.
*/
private void usePageParams()
{
final String defaultLabel = "Default label";
final String defaultBackground = "C0C0C0";
final String defaultForeground = "000000";
String labelValue;
String backgroundValue;
String foregroundValue;
/**
* Read the ,
* ,
* and tags from
* the applet's HTML host.
*/
labelValue = getParameter(labelParam);
backgroundValue = getParameter(backgroundParam);
foregroundValue = getParameter(foregroundParam);
if ((labelValue == null) || (backgroundValue == null) ||
(foregroundValue == null))
{
/**
* There was something wrong with the HTML host tags.
* Generate default values.
*/
labelValue = defaultLabel;
backgroundValue = defaultBackground;
foregroundValue = defaultForeground;
}
/**
* Set the applet's string label, background color, and
* foreground colors.
*/
label1.setText(labelValue);
label1.setBackground(stringToColor(backgroundValue));
label1.setForeground(stringToColor(foregroundValue));
this.setBackground(stringToColor(backgroundValue));
this.setForeground(stringToColor(foregroundValue));
String param;
param = getParameter ( param_string);
if(param !=null) theString= param;//取得字符串参数
param = getParameter ( param_font);
if(param !=null) font= param;//取得字型参数
param= getParameter ( param_size);
if (param !=null) size=Integer. parseInt ( param);//取得字提大小参数
param= getParameter ( param_style);
if(param!=null) style= param;//取得字型样式参数
param= getParameter ( param_speed);
if(param !=null) speed=Integer.parseInt (param);//取得文字移动速度参数
int vice_style = Font.PLAIN ;
if(style. equalsIgnoreCase ( "BOLD"))
vice_style=Font.BOLD ;
if (style.equalsIgnoreCase ("ITALIC"))
vice_style=Font.ITALIC ;//处理得到的字型参数,忽略大小写
ft=new Font(font,vice_style,size);
FontMetrics fm= getFontMetrics(ft);//生成FontMetrics类对象实例
Min= -fm.stringWidth (theString);
Max= getSize().width ;
xSet= Max;
}
/**
* Converts a string formatted as "rrggbb" to an awt.Color object
*/
private Color stringToColor(String paramValue)
{
int red;
int green;
int blue;
red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue();
green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue();
blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();
return new Color(red,green,blue);
}
/**
* External interface used by design tools to show properties of an applet.
*/
public String[][] getParameterInfo()
{
String[][] info =
{
{ labelParam, "String", "Label string to be displayed" },
{ backgroundParam, "String", "Background color, format \"rrggbb\"" },
{ foregroundParam, "String", "Foreground color, format \"rrggbb\"" },
};
return info;
}
Label label1 = new Label();
/**
* Intializes values for the applet and its components
*/
void initForm()
{
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
label1.setText("label1");
this.setLayout(new BorderLayout());
this.add("North",label1);
}
public void paint(Graphics g)
{
int ySet=getSize().height ;
ySet=(ySet-size)/2;
g.setFont (ft);
g.setColor (Color.red);
g.drawString (theString,xSet,ySet+40);
//+40是用来调整输出时的高度用的。
xSet--;
if(xSet |
正在阅读:VJ6.0的使用方法(5)Applet编写调试过程VJ6.0的使用方法(5)Applet编写调试过程
2004-02-14 09:34
出处:
责任编辑:pjl
键盘也能翻页,试试“← →”键