applet1.java
来自「矩阵乘法的计算程序」· Java 代码 · 共 150 行
JAVA
150 行
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
{
/**
* The entry point for the applet.
*/
public void init()
{
initForm();
usePageParams();
// TODO: Add any constructor code after initForm call.
}
public void paint(Graphics g){
Dimension d=getSize();
int cx=d.width/2,cy=d.height/2;
Font f=new Font("Helvetica",Font.PLAIN,20);
g.setFont(f);
String s1="AaBbCcDdEeFfGgHhIiJjKkLlMmNn";
String s2="OoPpQqRrTtUuVvWwXxYyZz";
FontMetrics fm=g.getFontMetrics();
int ascent=fm.getAscent();
int descent=fm.getDescent();
int leading=fm.getLeading();
int w=fm.stringWidth(s1);//计算S1字符串的宽度
int x1=cx-w/2,y1=cy-leading-descent;
g.drawLine(x1-10,y1,cx+w/2+10,y1);
//g.drawLine(x1-10,y1+descent,cx+w/2+10,y1+descent);
//g.drawLine(x1-10,y1-ascent,cx+w/2+10,y1-ascent);
//写字符是从基线开始
g.drawString(s1,x1,y1);
g.drawLine(0,cy,d.width,cy);
w=fm.stringWidth(s2);//计算S1字符串的宽度
int x2=cx-w/2,y2=cy+leading/2+ascent;
g.drawLine(x2-10,y2,cx+w/2+10,y2);
g.drawLine(x2-10,y2+descent,cx+w/2+10,y2+descent);
g.drawLine(x2-10,y2-ascent,cx+w/2+10,y2-ascent);
//写字符是从基线开始
g.drawString(s2,x2,y2);
}
private final String labelParam = "label";
private final String backgroundParam = "background";
private final String foregroundParam = "foreground";
/**
* 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 <PARAM NAME="label" VALUE="some string">,
* <PARAM NAME="background" VALUE="rrggbb">,
* and <PARAM NAME="foreground" VALUE="rrggbb"> 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));
}
/**
* 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);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?