📄 btpanel.java
字号:
case 1: // '\001'
{
int state = setSpeed(cmd) ? 0 : 1;
tool.setState(command, state);
break;
}
case 2: // '\002'
{
thoughtful = !thoughtful;
int state = thoughtful ? 0 : 1;
tool.setState(command, state);
displayMessage("", MSG.THINKING[state], 1);
break;
}
case 3: // '\003'
{
direction = -direction;
int state = direction != 1 ? 1 : 0;
tool.setState(command, state);
displayMessage("", MSG.ASCEND[state], 1);
break;
}
case 4: // '\004'
{
classic = !classic;
int state = classic ? 0 : 1;
tool.setState(command, state);
displayMessage("", MSG.SHAPE[state], 1);
break;
}
case 5: // '\005'
{
mute = !mute;
int state = mute ? 0 : 1;
tool.setState(command, state);
displayMessage("", MSG.SOUND[state], 1);
break;
}
case 9: // '\t'
{
if(tree.isAVL())
{
factors = !factors;
int state = factors ? 0 : 1;
tool.setState(command, state);
displayMessage("", MSG.FACTORS[state], 1);
} else
{
displayMessage("", MSG.FACTORS[2], 3);
}
break;
}
case 6: // '\006'
{
setTreeMode(2, !tree.isSPL());
int state = tree.isSPL() ? 0 : 1;
tool.setTreeMode(command, state);
displayMessage("", MSG.SPL[state], 0);
break;
}
case 7: // '\007'
{
setTreeMode(3, !tree.isRBL());
int state = tree.isRBL() ? 0 : 1;
tool.setTreeMode(command, state);
displayMessage("", MSG.RBL[state], 0);
break;
}
case 8: // '\b'
{
setTreeMode(1, !tree.isAVL());
int state = tree.isAVL() ? 0 : 1;
tool.setTreeMode(command, state);
displayMessage("", MSG.AVL[state], 0);
break;
}
case 10: // '\n'
{
if(!rotate(direction * cmd))
break;
int state;
if(tree.isAVL())
{
state = tree.isAVLcompliant(tree.getRoot()) ? 0 : 1;
addMessage("", MSG.AVLCMPL[state], 0);
if(state == 1)
{
setTreeMode(1, false);
tool.setTreeMode(8, 1);
}
break;
}
if(!tree.isRBL())
break;
state = tree.isRBLcompliant(tree.getRoot()) ? 0 : 1;
addMessage("", MSG.RBLCMPL[state], 0);
if(state == 1)
{
setTreeMode(3, false);
tool.setTreeMode(7, 1);
}
break;
}
case 11: // '\013'
{
if(!splay())
break;
if(tree.isAVL())
{
setTreeMode(1, false);
tool.setTreeMode(8, 1);
break;
}
if(tree.isRBL())
{
setTreeMode(3, false);
tool.setTreeMode(7, 1);
}
break;
}
}
}
public void setTreeMode(int mode, boolean state)
{
tree.setMode(mode, state);
if(tree.isAVL())
{
if(!tree.isAVLcompliant(tree.getRoot()))
reinsertTree(1);
else
resetNodes();
} else
{
factors = false;
}
if(tree.isSPL())
resetNodes();
if(tree.isRBL())
reinsertTree(3);
else
resetColors();
}
public void reinsertTree(int mode)
{
stopFID();
BSTree temp = new BSTree(mode);
for(BTNode node = tree.getRoot(); node != null; node = node.nextPrO())
temp.insert(node.getData());
tree = temp;
temp = null;
resetNodes();
}
public void setTreeSize(int cmd)
{
if(cmd == 4)
{
if(xstep > 8)
xstep--;
else
displayMessage("", "Minimum size. ", 3);
} else
if(cmd == 6)
{
if(xstep < 48)
xstep++;
else
displayMessage("", "Maximum size. ", 3);
} else
if(cmd == 8)
{
if(ystep < 96)
ystep++;
else
displayMessage("", "Maximum size. ", 3);
} else
if(cmd == 2)
{
if(ystep > 16)
ystep--;
else
displayMessage("", "Minimum size. ", 3);
} else
if(cmd == 5)
{
xstep = 16;
ystep = 32;
treetop.setLocation(400, 56);
shakeNodes();
displayMessage("", "Default tree size. ", 0);
} else
if(cmd == 1)
treetop.move(0, -2);
else
if(cmd == 3)
treetop.move(0, 2);
else
if(cmd == 7)
treetop.move(-2, 0);
else
if(cmd == 9)
treetop.move(2, 0);
}
public boolean setSpeed(int cmd)
{
if(cmd == 4)
{
if(SPEED > 1)
{
SPEED--;
displayMessage(String.valueOf(SPEED), "Animation speed: {0}. ", 0);
} else
{
displayMessage(String.valueOf(1), "Minimum speed: {0}. ", 3);
}
speed = SPEED;
} else
if(cmd == 6)
{
if(SPEED < 10)
{
SPEED++;
displayMessage(String.valueOf(SPEED), "Animation speed: {0}. ", 0);
} else
{
displayMessage(String.valueOf(10), "Maximum speed: {0}. ", 3);
}
speed = SPEED;
} else
if(cmd == 5)
{
if(speed == 0)
speed = SPEED;
else
speed = 0;
int state = speed <= 0 ? 1 : 0;
displayMessage("", MSG.PAUSED[state], 1);
} else
if(cmd == 2)
{
speed = SPEED = 10;
tool.setState(1, 0);
displayMessage(String.valueOf(10), "Maximum speed: {0}. ", 0);
} else
if(cmd == 8)
{
speed = SPEED = 1;
tool.setState(1, 0);
displayMessage(String.valueOf(1), "Minimum speed: {0}. ", 0);
}
thinkcount = 48 + 160 / SPEED;
return speed > 0;
}
public boolean rotate(int side)
{
if(!getPermission(4))
return false;
if(lastpick == null)
{
displayMessage("", "Pick a node first. ", 3);
return false;
}
String key = lastpick.getKey();
if(lastpick.getChild(-side) == null)
{
displayMessage(key, "Cannot rotate {0} " + getSideName(side), 3);
return false;
} else
{
lastpick = rotate(lastpick, side);
displayMessage(key, "{0} rotated " + getSideName(side), 1);
applet.setText(lastpick.getKey());
return true;
}
}
public boolean splay()
{
int i = 0;
if(!getPermission(10))
return false;
target = lastpick;
if(target == null)
{
displayMessage("", "Pick a node first. ", 3);
return false;
} else
{
pinball.setLocation(target);
pinball.setData(target.getData());
startSPL();
return true;
}
}
public boolean getPermission(int mode)
{
if(speed == 0)
{
displayMessage("", "Animation paused. Resume? ", 3);
return false;
}
if(pinball.getMode() != 0 && pinball.getMode() != mode || pinball.getEvent() != 0)
{
displayMessage("", "Another operation is in progress. ", 3);
return false;
}
if(tree.isEmpty() && mode != 5)
{
displayMessage("", "The tree is empty. Operation not permitted. ", 3);
return false;
} else
{
return true;
}
}
public void start()
{
if(engine == null)
{
engine = new Thread(this);
engine.start();
}
createBackImage();
createMainImage();
populate();
}
public void run()
{
for(Thread thread = Thread.currentThread(); engine == thread;)
{
try
{
Thread.sleep(80L);
}
catch(InterruptedException _ex) { }
animateTree();
repaint();
}
}
public void stop()
{
if(engine != null && engine.isAlive())
engine.stop();
engine = null;
}
public synchronized void update(Graphics g)
{
paint(g);
}
public boolean cycle(int count, int n)
{
return (float)(count / n) == (float)count / (float)n;
}
public String getSideName(int side)
{
int i = direction * side + 1;
return MSG.SIDENAME[i];
}
public void createBackImage()
{
backImage = createImage(800, 340);
Graphics g = backImage.getGraphics();
for(int i = 0; i < MODE.BALL.length; i++)
g.drawImage(applet.getBall(i), 0, 0, null);
applet.playSound(2);
g.setColor(Color.white);
g.fill3DRect(0, 0, 800, 340, true);
g.setColor(Color.black);
g.drawRect(0, 0, 799, 339);
}
public void createMainImage()
{
mainImage = createImage(800, 340);
bg = mainImage.getGraphics();
fm = bg.getFontMetrics();
tool.setFontMetrics(fm);
}
public void populate()
{
new Random();
for(int i = 0; i < 12; i++)
{
BTData data = new BTData(applet.getTextRandom());
tree.insert(data);
}
resetNodes();
}
public void setToolState()
{
tool.setState(0, 0);
tool.setState(1, speed <= 0 ? 1 : 0);
tool.setState(2, thoughtful ? 0 : 1);
tool.setState(3, direction != 1 ? 1 : 0);
tool.setState(4, classic ? 0 : 1);
tool.setState(5, mute ? 0 : 1);
tool.setState(6, tree.isSPL() ? 0 : 1);
tool.setState(7, tree.isRBL() ? 0 : 1);
tool.setState(8, tree.isAVL() ? 0 : 1);
tool.setState(9, factors ? 0 : 1);
tool.setState(10, 0);
tool.setState(11, 0);
}
static final int XSIZE = 800;
static final int YSIZE = 340;
static final int XOFF = 2;
static final int YOFF = 6;
static final int ON = 0;
static final int OFF = 1;
static final int NA = 2;
static final int YSTEP = 32;
static final int XSTEP = 16;
static final int BALLSIZE = 13;
static final int CIRCLESIZE = 18;
static final int BOUNCESIZE = 6;
static final int NODECOUNT = 12;
static final int BOUNCECOUNT = 1;
static final int THINKCOUNT = 48;
static final int SLEEPTIME = 80;
static final Color GRAYCOLOR = new Color(224, 224, 224);
public int xstep;
public int ystep;
public int speed;
public int SPEED;
public boolean thoughtful;
public int thinkcount;
public boolean classic;
public boolean mute;
public boolean factors;
public int direction;
public int minmax;
BSTree tree;
BTNode treetop;
BTNode pinball;
BTNode pinfall;
BTNode target;
BTNode picknode;
BTNode lastpick;
BTNode lastnode;
BTNode swapnode;
BTNode rootnode;
BTNode fallnode;
int order;
int count;
boolean rotatequeued;
boolean rotating;
String message;
Thread engine;
BTApplet applet;
BTTool tool;
Image backImage;
Image mainImage;
Graphics bg;
FontMetrics fm;
int animation;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -