📄 treesap.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
public class TreesAp extends Applet implements ActionListener {
/* The Trees applet by J M Bishop January 1997
* ================ Java 1.1 Jan 1998
Asks for information about the leaves of a tree.
Codes the information and links to a URL where
further information about the tree is stored.
Gets the tree names and codes from an HTML page.
Illustrates interaction with a browser,
button arrays, image tracking,
nested panel layouts and string tokenizers.
*/
// A TextField, boolean and integer for each leaf part.
private TextField widthField, lengthField, messageField;
private Button againButton;
private boolean gotChoice = false;
private boolean gotWidth = false;
private boolean gotLength = false;
private int c, w, l;
private Hashtable table = new Hashtable ();
private String patterns [] = {
"Alternate", "Opposite",
"Whorled", "Palmate",
"Complex", "Bipinnate"};
private Button patternButton [] = new Button[6];
public void init () {
//===================
// First read in the parameters from the HTML page
setUpTable();
// Create the applet screen with two text fields above
// and one below. In the centre is the grid of nine
// leaf patterns, each a border panel in its own right
// with a button in the south and a canvas occupying
// the rest. The canvas is painted by the leafPattern
// class. The font is set to 10pt.
Font f = new Font ("SanSerif",Font.PLAIN,10);
this.setFont (f);
setLayout(new BorderLayout(0,0));
Panel p = new Panel ();
p.add (new Label ("Leaf width"));
widthField = new TextField (8);
p.add (widthField);
widthField.addActionListener(this);
p.add (new Label ("Leaf length"));
lengthField = new TextField (8);
p.add (lengthField);
lengthField.addActionListener(this);
add ("North",p);
// Create a panel for the nine leaf patterns
Panel q = new Panel ();
q.setLayout(new GridLayout(2,3));
for (int b = 0; b<patterns.length; b++) {
Panel s = new Panel ();
s.setLayout(new BorderLayout(0,0));
s.add ("Center",new leafPattern (this,b));
patternButton[b] = new Button(patterns[b]);
patternButton[b].addActionListener(this);
s.add ("South",patternButton[b]);
q.add (s);
}
add ("Center",q);
// Follow up with a message and again button
// at the bottom of the applet.
Panel r = new Panel();
messageField = new TextField (30);
r.add (messageField);
againButton = new Button ("Again");
r.add(againButton);
againButton.addActionListener(this);
add("South",r);
}
private void setUpTable() {
//=========================
String s, item;
StringTokenizer t;
int code;
s = getParameter("entries");
int n = Integer.parseInt(s);
for (int i = 0; i<n; i++) {
// extract a tree name from the html
String tree = getParameter("tree-"+(i+1));
// read all its codes
s = getParameter("codes-"+(i+1));
t = new StringTokenizer (s);
// store each code-tree pair in the table.
while (true) {
try {
item = t.nextToken();
code = Integer.parseInt(item.trim());
table.put(new Integer(code),tree);
}
catch (NoSuchElementException e) {
break;
}
}
}
}
public void actionPerformed (ActionEvent e) {
//===========================================
Object source = e.getSource();
String name = e.getActionCommand();
if (source == widthField) {
w = readValue(widthField);
gotWidth = true;
} else if (source == lengthField) {
l = readValue(lengthField);
gotLength = true;
} else if (source == againButton) {
resetFields();
} else if (name instanceof String) {
c = 0;
while (!name.equals(patterns[c])) c++;
gotChoice = true;
patternButton[c].setBackground(Color.green);
}
if (gotChoice & gotLength & gotWidth)
findTree ();
}
private void findTree () {
//========================
// Calculate the three-part code
int part2=convertLength();
int part3=convertWidth();
int code = 100*(c+1)+10*part2+part3;
// Fetch the tree name from the table if it exists
String tree = (String) table.get(new Integer (code));
if (tree == null)
setMessage("No information on code "+code+" yet.");
else
fetchTree (tree);
}
private void fetchTree (String treeName) {
//====================================
// Try to get a web page of information on
// the tree.
try {
AppletContext context = getAppletContext ();
String s = treeName+".html";
setMessage("Looking for "+s);
URL u = new URL (getCodeBase(),s);
context.showDocument(u,"_self");
}
catch (MalformedURLException e) {
setMessage("No information for that tree yet");
}
}
// Code conversion methods
//========================
private int convertLength () {
// returns a code based on the leaf length supplied
if (l<=25) return 4; else
if (l<=50) return 5; else
if (l<=100) return 6; else
if (l<=200) return 7; else
return 8;
}
private int convertWidth () {
// returns a code based on the leaf width supplied
if (w<=10) return 4; else
if (w<=20) return 5; else
if (w<=40) return 6; else
if (w<=80) return 7; else
return 8;
}
// Utility methods
// ===============
private int readValue (TextField t) {
int x = Integer.parseInt(t.getText());
t.setEditable(false);
t.setBackground(Color.green);
return x;
}
private void clearField(TextField t) {
t.setEditable(true);
t.setBackground(Color.white);
t.setText("");
}
private void resetFields () {
gotChoice = false;
patternButton[c].setBackground(Color.lightGray);
gotWidth = false;
clearField(widthField);
gotLength = false;
clearField(lengthField);
clearField(messageField);
messageField.setEditable(false);
}
private void setMessage(String s) {
messageField.setText(s);
messageField.setBackground(Color.green);
}
class leafPattern extends Canvas {
//================================
Image im;
leafPattern(Applet a, int b) {
weAre = a;
// Construct the URL name
pattern = "images/leaf"+Integer.toString(b)+".jpg";
// Get the image
im = weAre.getImage(weAre.getCodeBase(),pattern);
// Ensure that the image has been received before
// the constructor returns.
MediaTracker tracker = new MediaTracker(weAre);
tracker.addImage(im, 0);
try { tracker.waitForID(0);}
catch (InterruptedException e) {}
}
public void paint (Graphics g) {
g.drawImage(im,0,0,this);
}
private Applet weAre;
private String pattern;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -