📄 panels.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 Panels extends Applet
{
//declare panels and label of these panels
private Panel Para,Paper;
private Canvas Screen;
private Label VceLabel,VceValue,IbLabel,IbUnit,YwayLabel,YUnit,XwayLabel,XUnit,IbLevelLabel;
private Label BLabel,ParamShowLabel,HandInLabel,VaryItLabel,ResultLabel,Result,TrueBLabel;
private Scrollbar Vce,IbLevel;
private Choice Ib,Yway,Xway;
private TextField B;
private Button HandIn,VaryIt;
private int w=800;
private int h=480;
private final int w1=w/40;
private final int h1=h/24;
private int TrueB=80;
/**
* The entry point for the applet.
*/
public void init()
{
this.setSize(w,h);
//initForm();
usePageParams();
// TODO: Add any constructor code after initForm call.
//不使用布局管理器
setLayout(null);
//设置前景色
setForeground(Color.blue);
//建立Sreen画布
Screen=new Canvas()
{
//x0,y0为原点坐标, x1,y1为拐点坐标, x,y为x,y坐标
private int x0=3*w1;
private int y0=13*h1;
private int x1;
private int y1;
private int x,y;
//IcMax-Ic电流每格;Vce-电压每格;VceV-Vce;IbmA-Ib;j-阶梯个数;VceTurn-拐点Vce
private int IcMax,VceMax,VceV,IbmA,IbmATemp,Ic,j,VceTurn=100;
private int VceTurnK;
//画输出曲线
public void paint(Graphics g)
{
g.setColor(Color.green);
//表盘
g.drawOval(0,0,w1*8*2,h1*8*2);
//标尺
g.drawRect(w1*3,h1*3,w1*10,h1*10);
for (int hor=1;hor<10;hor++)
{
g.drawLine(w1*3,h1*3+h1*hor,w1*13,h1*3+h1*hor);
g.drawLine(w1*3+w1*hor,3*h1,w1*3+w1*hor,h1*13);
}
g.setColor(Color.black);
//得到Ic/格;
IcMax=Integer.parseInt(Yway.getSelectedItem());
//得到Vce/格
VceMax=Integer.parseInt(Xway.getSelectedItem());
//得到Vce
VceV=Vce.getValue();
//得到Ib
IbmA=Integer.parseInt(Ib.getSelectedItem());
//保存Ib,便于累加
IbmATemp=IbmA;
//得到阶梯个数
j=IbLevel.getValue();
//将Vce换成坐标
x1=XVce(VceTurn,VceMax);
//画图
for (int i=0;i<j;i++)
{
//计算Ic
Ic=IbmA*TrueB;
//将Ic换成坐标
y=YIc(Ic,IcMax);
//求拐点到原点直线的斜率
VceTurnK=(int)(y*100/x1);
//将Vce换成坐标
x=XVce(VceV,VceMax);
if ((VceV<VceTurn)||(VceV==VceTurn))
{
//当Vce<VceTurn时,画图
//求Y坐标
y=(int)((x*VceTurnK/100));
//画
g.drawLine(x0,y0,x0+x,y0-y);
//Ib是阶梯波,换下一级,
IbmA=IbmA+IbmATemp;
}else
{
//如果Vcd>VceTurn,画
g.drawLine(x0,y0,x0+x1,y0-y);
g.drawLine(x0+x1,y0-y,x0+x,y0-y);
//Ib是阶梯波,换下一级,
IbmA=IbmA+IbmATemp;
}
}
}
//Ic变y坐标的函数
public int YIc(int Ic,int IcMax)
{
return (int)(Ic*(w1)/IcMax);
}
//Vce变x坐标的函数
public int XVce(int VceV,int VceMax)
{
return (int)(VceV*(h1)/VceMax);
}
};
Para=new Panel();
Paper=new Panel();
//...three panels 建立完毕
//建立全部标签
VceLabel=new Label(" 峰值电压(Vce):");
VceValue=new Label("3.0"+"(伏)");
IbLabel=new Label("阶梯选择(Ib):");
IbUnit=new Label("(微安/级)");
YwayLabel=new Label(" Y轴作用: ");
YUnit=new Label("(微安/格)");
XwayLabel=new Label(" X轴作用: ");
XUnit=new Label("(毫伏/度)");
BLabel=new Label(" 请输入B: ");
HandInLabel=new Label("与真实B比较");
VaryItLabel=new Label("换个晶体管再测");
IbLevelLabel=new Label(" Ib级数 ");
ResultLabel=new Label("建议: ");
Result=new Label("测测 B 吧!");
TrueBLabel=new Label(" ");
//建立在para面板里的东西
Vce=new Scrollbar(Scrollbar.HORIZONTAL,300,2,0,2200);
IbLevel=new Scrollbar(Scrollbar.HORIZONTAL,4,2,4,14);
Ib=new Choice();
Ib.addItem("10");
Ib.addItem("20");
Ib.addItem("50");
Ib.addItem("100");
Ib.addItem("200");
Ib.addItem("500");
Yway=new Choice();
Yway.addItem("500");
Yway.addItem("1000");
Yway.addItem("2000");
Yway.addItem("5000");
Yway.addItem("10000");
Xway=new Choice();
Xway.addItem("50");
Xway.addItem("100");
Xway.addItem("200");
//建立在Paper面板的东西
B=new TextField(7);
HandIn=new Button(" 实 际 B");
HandIn.setCursor(new Cursor(12));
VaryIt=new Button(" 再 测 ");
VaryIt.setCursor(new Cursor(12));
//设置面板的背景色
Screen.setBackground(Color.lightGray);
Para.setBackground(Color.lightGray);
Paper.setBackground(Color.lightGray);
//加Screen面板
add(Screen);//add Screen panel to applet
//在Para里加部件
Para.add(VceLabel);
Para.add(Vce);
Para.add(VceValue);
Para.add(IbLabel);
Para.add(Ib);
Para.add(IbUnit);
Para.add(YwayLabel);
Para.add(Yway);
Para.add(YUnit);
Para.add(XwayLabel);
Para.add(Xway);
Para.add(XUnit);
//加Para
add(Para);
//在Paper里加部件
Paper.add(IbLevelLabel);
Paper.add(IbLevel);
Paper.add(BLabel);
Paper.add(B);
Paper.add(HandInLabel);
Paper.add(HandIn);
Paper.add(VaryItLabel);
Paper.add(VaryIt);
Paper.add(ResultLabel);
Paper.add(Result);
Paper.add(TrueBLabel);
//加Paper
add(Paper);
//布局三个板
Screen.reshape(w1*2*2,h1*2*2,w1*8*2,h1*8*2);
Para.reshape(w1*11*2,h1*2*2,w1*3*2,h1*8*2);
Paper.reshape(w1*15*2,h1*2*2,w1*3*2,h1*8*2);
}
//事件处理
public boolean handleEvent(Event e)
{
//按钮"实际 B"
if ((e.target instanceof Button)&&(e.arg==" 实 际 B"))
TrueBLabel.setText("实际 B="+String.valueOf(TrueB));
//按键就触发按钮"HandIn"事件
if (e.id==Event.KEY_RELEASE)
e.target=HandIn;
//处理各部件事件
if ((e.target instanceof Button) || e.target instanceof Scrollbar|| e.target instanceof Choice)
{
if (e.target==HandIn)
{
try
{
int temp=Math.abs(TrueB-Integer.parseInt(B.getText()));
if (temp<20)
{
if ((temp>10)||(temp==10))
Result.setText("不错!接近真实B.");
else if (temp>0)
Result.setText("棒!极接近真B");
else if (temp==0)
Result.setText("正确!你真神了!");
}
else {
if ((temp<100)||(temp==100))
Result.setText("不对!再试试吧!");
if ((temp>100)&&(temp<1000))
Result.setText("太夸张了吧!");
if (temp==Math.abs(334-TrueB))
Result.setText("Nafeel&Nakiki-@*@");
if (temp>1000)
Result.setText("相信您是笔误!");
}
}
catch(Exception ex)
{
Result.setText("B为正整数");
}
} else if (e.target==Vce)
{
if (Vce.getValue()>1400)
{
Result.setForeground(Color.red);
Result.setText("危险!快烧了!");
}
else
{
Result.setText(" Vce<14 ");
}
VceValue.setText(String.valueOf(getVce()/100)+"."+String.valueOf(getVce()%100)+"(伏)");
Screen.repaint();
}
else if (e.target==IbLevel)
{
Screen.repaint();
return true;
}
else if ((e.target==Yway)||(e.target==Xway))
{
Screen.repaint();
return true;
}
if (e.target==VaryIt)
{
TrueB=(int)(Math.random()*40+60);
VaryIt.nextFocus();
Result.setForeground (Color.blue);
Result.setText("请测 B ");
TrueBLabel.setText(" ");
B.setText("");
Screen.repaint();
return true;
}
if (e.target==Ib)
{
if (Ib.getSelectedItem()=="500")
{
Result.setForeground(Color.red);
Result.setText("基极电流过大!");
}else
{
Result.setForeground (Color.blue);
Result.setText("请测 B ");
}
Screen.repaint();
return true;
}
}
return false;
}
//get the value of Vce
public int getVce()
{
return Vce.getValue();
}
//get the value of Ib
public String getIb()
{
return Ib.getSelectedItem();
}
//--------------------------------------------------------------------------------------------
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -