📄 sentence.java
字号:
/*
Sample Java Application.
This Java application is based on the old PLATO mainframe lesson
called "0sentences" and demonstrates how old ideas can have new life.
The basic idea is to allow the user of the applet the ability to
create a simple sentence and then see the sentence acted out, using
rudimentary animation.
An interesting extension would be to substitute the simple GIF images
with more fleshed out pictures, improve the animation with multiple
GIF images, and the addition of sound. Perhaps in Version 2 :)
This applet expects to find the GIF images used in the animation
in a subdirectory named "images". See "init()" for details or if
that needs to be changed.
Jeff Bauer, Fall 1995
jtbauer@scri.fsu.edu
*/
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.util.*;
public class sentence extends java.applet.Applet {
// Constants.
static final int NOBJECTS = 9;
static final int NWORDS = 16;
static final int NO_WORD = -1;
// First nine are objects with images.
static final int BOY = 0;
static final int CAR = 1;
static final int CAT = 2;
static final int DOG = 3;
static final int GIRL = 4;
static final int HOUSE = 5;
static final int RABBIT = 6;
static final int TREE = 7;
static final int JUMP = 8;
// Modifiers and verbs.
static final int FROM = 8;
static final int TO = 9;
static final int OVER = 10;
static final int THE = 11;
static final int JUMPS = 12;
static final int CARRIES = 13;
static final int WALKS = 14;
static final int RUNS = 15;
static final int NO_CLASS = -1;
// Basic parts of a sentence.
static final int ARTICLE = 0;
static final int VERB = 1;
static final int NOUN = 2;
static final int MODIFIER = 3;
static final int NO_FUNC = -1;
static final int ERASE_FUNC = 0;
static final int RUN_FUNC = 1;
// Globals.
Image Objects[]; // Array of GIF images
int nWords = 0; // Number of words in sentence
int Sentence[]; // Array of sentence objects
int Class[]; // Classification of sentence objects
boolean runSentence; // flag to animate sentence or not
// Methods.
public void initSentence() { // Clear out sentence structures
int i;
for (i = 0; i < NWORDS; i++) {
Sentence[i] = NO_WORD;
Class[i] = NO_CLASS;
}
nWords = 0;
}
public void init() { // applet init : allocate objects &
// load GIF images
int i;
resize(512, 512);
Objects = new Image[NOBJECTS]; // Allocate image objects
Objects[BOY] = getImage(getCodeBase(), "images/boy.gif");
Objects[CAR] = getImage(getCodeBase(), "images/car.gif");
Objects[CAT] = getImage(getCodeBase(), "images/cat.gif");
Objects[DOG] = getImage(getCodeBase(), "images/dog.gif");
Objects[GIRL] = getImage(getCodeBase(), "images/girl.gif");
Objects[HOUSE] = getImage(getCodeBase(), "images/house.gif");
Objects[RABBIT] = getImage(getCodeBase(), "images/rabbit.gif");
Objects[TREE] = getImage(getCodeBase(), "images/tree.gif");
Objects[JUMP] = getImage(getCodeBase(), "images/jump.gif");
Sentence = new int[NWORDS]; // Allocate sentence
Class = new int[NWORDS]; // Allocate classifiers
}
public void drawBox(Graphics g, int x, int y, int w, int h) {
g.drawRect(x, y, w, h);
g.drawRect(x+1, y+1, w-2, h-2);
}
public int findWord(int x, int y) { // Match screen coordinates to an object.
if ((x > (0*125)+6) && (x < (0*125)+6+122) &&
(y > (224+6)) && (y < (224+6+56))) return(FROM);
if ((x > (1*125)+6) && (x < (1*125)+6+122) &&
(y > (224+6)) && (y < (224+6+56))) return(TO);
if ((x > (2*125)+6) && (x < (2*125)+6+122) &&
(y > (224+6)) && (y < (224+6+56))) return(OVER);
if ((x > (3*125)+6) && (x < (3*125)+6+122) &&
(y > (224+6)) && (y < (224+6+56))) return(THE);
if ((x > (0*125)+6) && (x < (0*125)+6+122) &&
(y > (224+60+6)) && (y < (224+60+6+56))) return(BOY);
if ((x > (1*125)+6) && (x < (1*125)+6+122) &&
(y > (224+60+6)) && (y < (224+60+6+56))) return(HOUSE);
if ((x > (2*125)+6) && (x < (2*125)+6+122) &&
(y > (224+60+6)) && (y < (224+60+6+56))) return(CAT);
if ((x > (3*125)+6) && (x < (3*125)+6+122) &&
(y > (224+60+6)) && (y < (224+60+6+56))) return(RABBIT);
if ((x > (0*125)+6) && (x < (0*125)+6+122) &&
(y > (224+2*60+6)) && (y < (224+2*60+6+56))) return(DOG);
if ((x > (1*125)+6) && (x < (1*125)+6+122) &&
(y > (224+2*60+6)) && (y < (224+2*60+6+56))) return(TREE);
if ((x > (2*125)+6) && (x < (2*125)+6+122) &&
(y > (224+2*60+6)) && (y < (224+2*60+6+56))) return(GIRL);
if ((x > (3*125)+6) && (x < (3*125)+6+122) &&
(y > (224+2*60+6)) && (y < (224+2*60+6+56))) return(CAR);
if ((x > (0*125)+6) && (x < (0*125)+6+122) &&
(y > (224+3*60+6)) && (y < (224+3*60+6+56))) return(JUMPS);
if ((x > (1*125)+6) && (x < (1*125)+6+122) &&
(y > (224+3*60+6)) && (y < (224+3*60+6+56))) return(CARRIES);
if ((x > (2*125)+6) && (x < (2*125)+6+122) &&
(y > (224+3*60+6)) && (y < (224+3*60+6+56))) return(WALKS);
if ((x > (3*125)+6) && (x < (3*125)+6+122) &&
(y > (224+3*60+6)) && (y < (224+3*60+6+56))) return(RUNS);
return(NO_WORD);
}
public int findFunction(int x, int y) { // Match coordinates to an action.
if ((x > 36) && (x < (36+100)) &&
(y > 474) && (y < (474+28))) return(ERASE_FUNC);
if ((x > 376) && (x < (376+100)) &&
(y > 474) && (y < (474+28))) return(RUN_FUNC);
return(NO_FUNC);
}
public String capString(String source) { // Capitalize 1st letter of word.
int i, len = source.length();
char c;
StringBuffer dest = new StringBuffer(len);
for (i = 0; i < len; i++) {
if (i == 0) c = (char) (source.charAt(i) - 32); // capitalize 1st letter
else c = source.charAt(i);
dest.append(c);
}
return dest.toString();
}
public int setClass(int type) { // classify parts of the sentence.
switch (type) {
case FROM :
case TO :
case OVER : return(MODIFIER);
case THE : return(ARTICLE);
case BOY :
case HOUSE :
case CAT :
case RABBIT :
case DOG :
case TREE :
case GIRL :
case CAR : return(NOUN);
case JUMPS :
case CARRIES :
case WALKS :
case RUNS : return(VERB);
}
return(NO_CLASS);
}
public void animateObject(Graphics g, int noun, int verb,
int carryNoun, boolean carryFlag) {
Image nounImage, carryImage, jumpImage;
int i;
int hops, dist;
long doze;
// This routine actually does the work of animating the object by
// moving the object of the noun across the screen according to the
// rules of the verb.
nounImage = Objects[noun];
carryImage = nounImage; // kill compiler warnings :)
jumpImage = Objects[JUMP];
if (carryFlag) {
carryImage = Objects[carryNoun];
}
if ((verb == JUMPS) || (verb == RUNS)) {
hops = 12;
dist = 32;
doze = 50;
}
else /* WALKS or CARRIES */ {
hops = 48;
dist = 8;
doze = 60;
}
for (i = 0; i < hops; i++) {
g.drawImage(nounImage, i*dist+48, 92, this);
if (carryFlag) {
g.drawImage(carryImage, i*dist+48, 92-nounImage.getWidth(this)-4, this);
}
try { Thread.sleep(doze); }
catch (Exception e) {}
g.setColor(Color.black);
if (i < hops-1) {
g.fillRect(i*dist+48, 92, nounImage.getWidth(this), nounImage.getHeight(this));
if (carryFlag) {
g.fillRect(i*dist+48, 92-nounImage.getHeight(this)-4,
carryImage.getWidth(this), carryImage.getHeight(this));
}
if (verb == JUMPS) {
g.drawImage(jumpImage, i*dist+(dist/2)+48, 72, this);
try { Thread.sleep(doze); }
catch (Exception e) {}
g.fillRect(i*dist+(dist/2)+48, 72,
jumpImage.getWidth(this), jumpImage.getHeight(this));
}
}
}
}
public void setupModifier(Graphics g, int modifier, int noun) {
Image nounImage;
int i;
// This routine sets up the stage for the animation, by handling
// any of the modifiers ("from", "to", and "over").
nounImage = Objects[noun];
g.setColor(Color.white);
if (modifier == FROM) {
g.drawImage(nounImage, 4, 92, this);
}
if (modifier == TO) {
g.drawImage(nounImage, 480, 92, this);
}
if (modifier == OVER) {
g.drawImage(nounImage, 248, 148, this);
g.drawLine(32, 122, 484, 122);
}
}
public void checkModifiers(Graphics g, int s) {
// Perform rudimentary checks on the sentence structure
// to see if it fits any of the known categories for animation.
if ((Class[s] == MODIFIER) &&
(Class[s+1] == ARTICLE) &&
(Class[s+2] == NOUN)) {
setupModifier(g, Sentence[s], Sentence[s+2]);
}
if ((Class[s+3] == MODIFIER) &&
(Class[s+4] == ARTICLE) &&
(Class[s+5] == NOUN)) {
setupModifier(g, Sentence[s+3], Sentence[s+5]);
}
if ((Class[s+6] == MODIFIER) &&
(Class[s+7] == ARTICLE) &&
(Class[s+8] == NOUN)) {
setupModifier(g, Sentence[s+6], Sentence[s+8]);
}
}
public void animateSentence(Graphics g) {
boolean carryFlag;
// Animate the current sentence, if possible.
if ((Class[0] == ARTICLE) &&
(Class[1] == NOUN) &&
(Class[2] == VERB)) {
if (Sentence[2] == CARRIES) { // "the(0) x(1) carries(2) the(3) y(4)"
carryFlag = true;
}
else {
carryFlag = false;
}
if (carryFlag) checkModifiers(g, 5);
else checkModifiers(g, 3);
animateObject(g, Sentence[1], Sentence[2], Sentence[4], carryFlag);
}
else {
g.drawString("Invalid sentence - try again!", 102, 92);
initSentence();
}
}
public void paint(Graphics g) { // main redraw routine & display driver
String word;
String sentence;
int i;
g.setColor(Color.black);
g.fillRect(0, 0, 512, 512);
g.setColor(Color.yellow);
for (i = 0; i < 4; i++) {
drawBox(g, (i*125)+6, 224+6, 122, 56);
drawBox(g, (i*125)+6, 224+60+6, 122, 56);
drawBox(g, (i*125)+6, 224+2*60+6, 122, 56);
drawBox(g, (i*125)+6, 224+3*60+6, 122, 56);
}
drawBox(g, 36, 474, 100, 28);
drawBox(g, 376, 474, 100, 28);
g.setColor(Color.red);
g.drawString("ERASE", 66, 492);
g.setColor(Color.green);
g.drawString("RUN", 412, 492);
g.setColor(Color.white);
g.drawString("from", 40, 264);
g.drawString("to", 182, 264);
g.drawString("over", 297, 264);
g.drawString("the", 428, 264);
g.drawString("boy", 45, 340);
g.drawImage(Objects[BOY], 45, 300, this);
g.drawString("house", 168, 340);
g.drawImage(Objects[HOUSE], 168, 292, this);
g.drawString("cat", 297, 340);
g.drawImage(Objects[CAT], 297, 300, this);
g.drawString("rabbit", 415, 340);
g.drawImage(Objects[RABBIT], 415, 300, this);
g.drawString("dog", 45, 400);
g.drawImage(Objects[DOG], 45, 360, this);
g.drawString("tree", 180, 400);
g.drawImage(Objects[TREE], 174, 352, this);
g.drawString("girl", 300, 400);
g.drawImage(Objects[GIRL], 296, 358, this);
g.drawString("car", 430, 400);
g.drawImage(Objects[CAR], 420, 364, this);
g.drawString("jumps", 40, 444);
g.drawString("carries", 162, 444);
g.drawString("walks", 292, 444);
g.drawString("runs", 418, 444);
sentence = "";
g.setColor(Color.cyan);
for (i = 0; i < nWords; i++) {
word = "NoWord";
switch (Sentence[i]) {
case FROM : word = "from"; break;
case TO : word = "to"; break;
case OVER : word = "over"; break;
case THE : word = "the"; break;
case BOY : word = "boy"; break;
case HOUSE : word = "house"; break;
case CAT : word = "cat"; break;
case RABBIT : word = "rabbit"; break;
case DOG : word = "dog"; break;
case TREE : word = "tree"; break;
case GIRL : word = "girl"; break;
case CAR : word = "car"; break;
case JUMPS : word = "jumps"; break;
case CARRIES : word = "carries"; break;
case WALKS : word = "walks"; break;
case RUNS : word = "runs"; break;
}
if (i == 0) {
word = capString(word);
}
sentence = sentence + " " + word;
}
g.drawString(sentence, 15 + 2, 204);
if (runSentence) {
animateSentence(g);
runSentence = false;
}
}
public boolean mouseDown(java.awt.Event evt, int x, int y) {
int newWord;
// Handle mouse input & perform appropriate action.
requestFocus();
if ((newWord = findFunction(x, y)) != NO_FUNC) {
switch (newWord) {
case ERASE_FUNC :
nWords -= 1;
if (nWords < 0) initSentence();
Class[nWords] = NO_CLASS;
Sentence[nWords] = NO_WORD;
break;
case RUN_FUNC :
runSentence = true;
break;
}
repaint();
return true;
}
if ((newWord = findWord(x, y)) != NO_WORD) {
Class[nWords] = setClass(newWord);
Sentence[nWords++] = newWord;
if (nWords > NWORDS) nWords = NWORDS;
}
repaint();
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -