📄 asciicursor.java
字号:
package joeh.asciidraw;
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.io.*;
//this class takes requests to move like "move right"
//and determines where the cursor ends up (i.e. wrap around the screen)
//it then tells the caller where they ended up via "setCursor"
class asciiCursor {
Coordinate loc, lastLoc;
AsciiDrawPanel adp;
int maxX, maxY;
public static final int ESTABLISH_NEW_LINE_START = 1;
public static final int KEEP_SAME_NEW_LINE = 2;
int newLineVal = 0; //when they hit return, this is where it comes to
int endLineVal;
Stack le;
//give it the thing to control, and the max rows and max columns
public asciiCursor(AsciiDrawPanel parent, int mx, int my, int defaultX, int defaultY) {
adp = parent;
maxX = mx;
maxY = my;
loc = new Coordinate(defaultX, defaultY);
lastLoc = new Coordinate(defaultX, defaultY);
endLineVal = maxX;
newLineVal = 0;
le = new Stack();
//adp.setCursor();
}
//set to the coordinates of the new location
public void setLoc(int x, int y) {
lastLoc = new Coordinate(loc.getX(), loc.getY());
loc = new Coordinate(x,y);
adp.setCursor();
}
public Coordinate getLoc() {
return loc;
}
public Coordinate getLastLoc() {
return lastLoc;
}
public void pushEnd(int e) {
le.push(new Integer(e));
}
public int popEnd() {
int result;
try {
result = ((Integer)le.pop()).intValue();
} catch (EmptyStackException e) {
result = endLineVal;
}
return result;
}
public void clearEndStack() {
le = new Stack(); //clear the stack
}
//Set how far to the right the cursor goes before a newline.
public void setEnd(int newEnd) {
endLineVal = newEnd+1;
pushEnd(endLineVal);
}
public void setStart(int newStart) {
newLineVal = newStart;
endLineVal = maxX;
clearEndStack();
pushEnd(endLineVal);
}
public int getEnd() {
return endLineVal;
}
public int getStart() {
return newLineVal;
}
// moves right only if not past user's end the end otherwise goes down a line
//I use this only when a space is typed so lines are never wrapped in the middle of a word
//I think that is more important than staying strictly inside a column.
public void moveRightCheck() {
lastLoc = new Coordinate(loc.getX(), loc.getY());
int Y = loc.getY(); //y wont change
int X = loc.getX() + 1; //hmm.. is this okay
if (X >= endLineVal) {
pushEnd(X); //save the last loc!
moveNewLine();
return;
}
loc = new Coordinate(X,Y);
adp.setCursor();
}
//moves right in everycase except when at the very very end point
public void moveRight() {
lastLoc = new Coordinate(loc.getX(), loc.getY());
int Y = loc.getY(); //y wont change
int X = loc.getX() + 1; //hmm.. is this okay
//Japanese text case
if (endLineVal <= newLineVal-1) {
moveNewLine();
return;
}
if (X >= maxX) {
moveNewLine();
return;
}
loc = new Coordinate(X,Y);
adp.setCursor();
}
public void moveLeft(boolean leftWrapsUp){
lastLoc = new Coordinate(loc.getX(), loc.getY());
int Y = loc.getY(); //y wont change
int X = loc.getX() - 1; //hmm.. is this okay
if (X < newLineVal){
//X = endLineVal-1;
X = popEnd()-1; //wrap up to where they
//actually wrapped from!
if (leftWrapsUp) {
if ((Y-1) < 0)
Y = maxY-1;
else Y = Y-1;
}
}
//little used secret feature for emulating japanese text...
if (endLineVal <= newLineVal-1) {
X = endLineVal;
}
loc = new Coordinate(X,Y);
adp.setCursor();
}
public void moveDown(){
lastLoc = new Coordinate(loc.getX(), loc.getY());
int Y = loc.getY() + 1; //hmm, is this okay
int X = loc.getX(); //x wont change
if (Y >= maxY)
Y = 0;
//little used secret feature for emulating japanese text...
if (endLineVal <= newLineVal-1)
{
if ((loc.getY() + 1) >= maxY) {
Y = 0;
newLineVal = newLineVal-3;
endLineVal = newLineVal;
X = newLineVal;
if (newLineVal <= 0) {
newLineVal = maxX -2;
endLineVal = newLineVal;
X = newLineVal;
}
}
}
loc = new Coordinate(X,Y);
adp.setCursor();
}
public void moveUp(){
lastLoc = new Coordinate(loc.getX(), loc.getY());
int Y = loc.getY() - 1; //hmm, is this okay
int X = loc.getX(); //x wont change
if (Y < 0)
Y = maxY-1;
loc = new Coordinate(X,Y);
adp.setCursor();
}
// THE HARD move frunctions don't care about
// where the text bounds (newLineVal, endLineVal) are
// used for moving the cursor anywhere
public void hardLeft() {
clearEndStack(); //tough luck, you lose your smart end line places
lastLoc = new Coordinate(loc.getX(), loc.getY());
int Y = loc.getY(); //y wont change
int X = loc.getX() - 1; //hmm.. is this okay
if (X < 0)
X = maxX-1;
if (X < newLineVal)
newLineVal = X;
if (X > endLineVal)
endLineVal = X;
loc = new Coordinate(X,Y);
adp.setCursor();
}
public void hardRight() {
clearEndStack(); //tough luck, you lose your smart end line places
lastLoc = new Coordinate(loc.getX(), loc.getY());
int Y = loc.getY(); //y wont change
int X = loc.getX() + 1; //hmm.. is this okay
if (X >= maxX)
X = 0;
if (X < newLineVal)
newLineVal = X;
if (X >= endLineVal)
setEnd(X);
loc = new Coordinate(X,Y);
adp.setCursor();
}
public void hardUp() {
clearEndStack(); //tough luck, you lose your smart end line places
lastLoc = new Coordinate(loc.getX(), loc.getY());
int Y = loc.getY() - 1; //hmm.. is this okay
int X = loc.getX(); //X wont change
if (Y < 0)
Y = maxY-1;
loc = new Coordinate(X,Y);
adp.setCursor();
}
public void hardDown() {
clearEndStack(); //tough luck, you lose your smart end line places
lastLoc = new Coordinate(loc.getX(), loc.getY());
int Y = loc.getY() + 1; //hmm.. is this okay
int X = loc.getX(); //X wont change
if (Y >= maxY)
Y = 0;
loc = new Coordinate(X,Y);
adp.setCursor();
}
//The user pushes the return key
public void moveNewLineUser() {
//if it is Japanese text
if (endLineVal <= newLineVal+1)
moveNewLine();
else {
lastLoc = new Coordinate(loc.getX(), loc.getY());
int Y = loc.getY() + 1; //hmm.. is this okay
int X = newLineVal;
if (Y >= maxY) //wrap in the Y?
Y = 0;
pushEnd(loc.getX()+1); //save that place for future backspaces
loc = new Coordinate(X,Y);
adp.setCursor();
}
}
//used internally a lot for moving to a new line
public void moveNewLine() {
lastLoc = new Coordinate(loc.getX(), loc.getY());
int Y = loc.getY() + 1; //hmm.. is this okay
int X = newLineVal;
if (Y >= maxY) //wrap in the Y?
Y = 0;
//little used secret feature for emulating japanese text...
if (endLineVal <= newLineVal+1) {
if ((loc.getY() + 1) >= maxY) {
Y = 0;
newLineVal = newLineVal-3;
endLineVal = newLineVal;
X = newLineVal;
if (newLineVal <= 0) {
newLineVal = maxX;
endLineVal = newLineVal;
X = newLineVal;
}
}
}
loc = new Coordinate(X,Y);
adp.setCursor();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -