📄 adaptivehuff.java
字号:
import java.awt.*;
import java.util.*;
import InputField;
import OutputField;
import Send;
import BitStats;
public class AdaptiveHuff extends AdaptiveTree
{
public void init()
{
super.init();
setLayout(null);
size = size();
super.setFont(defaultFont);
inputField = new InputField();
add(inputField);
inputField.reshape(5, 7, (size.width/3), (_height + _descent) );
outputField = new OutputField();
add(outputField);
outputField.reshape( (size.width*2/3), 7, (size.width/3), (_height + _descent) );
sender = new Send();
add(sender);
sender.reshape( 5, (size.height*2/3 + _height+_descent), (size.width - 10), (_height + _descent) );
dispRect = new Rectangle(5, (3*_ascent),
size.width - 10,
size.height*2/3);
message = new List();
message.setMultipleSelections(false);
add(message);
message.reshape( 5, (size.height*2/3 + 2*(_height+_descent)), (size.width/2), (size.height/3 - 2*(_height+_descent)) );
addMessage("Adaptive tree initialized");
this.setBackground(Color.white);
waitforclick = true;
mode = new CheckboxGroup();
playpause = new Checkbox("Play-Pause", mode, waitforclick);
continuous = new Checkbox("Continuous", mode, !waitforclick);
add(playpause);
add(continuous);
playpause.reshape( (size.width/2)+10, (size.height*2/3 + 2*(_height+_descent)), (size.width/5), (_height+_descent) );
continuous.reshape( (size.width/2)+10, (size.height*2/3 + 3*(_height+_descent)), (size.width/5), (_height+_descent) );
next = new Button("Next Step");
next.hide();
add(next);
next.reshape( (size.width/2)+10, (size.height*2/3 + 4*(_height+_descent)), (size.width/5), (_height+_descent) );
stats = new BitStats(Color.white, Color.black);
add(stats);
stats.reshape( (size.width/2 + size.width/5 + 15), (size.height*2/3 + 2*(_height+_descent)), (3*size.width/10)-20, 4*(_height+_descent) );
AdaptiveTree(51, dispRect);
}
public void updateTree(char newchar)
{
int current;
int max;
try { // first appearance for symbol
current = findChar(newchar); // Go to symbol external node
max = highestInBlock(tree[current].count); // Node number max in block?
if (current != max && tree[current].parent != max) {
addMessage(" Swapping nodes " +current+ " and " +max);
swap(current, max); // Switch node with highest node in block
pause();
current = max;
}
addMessage(" Increasing count for '" +newchar+ "'");
tree[current].count++; // Increment node weight
pause();
} catch (NoSuchElementException e) { // Yes
addMessage(" Spawning new node for '" +newchar+ "'");
current = spawn(newchar); // NYT gives birth to new NYT and external node
current = tree[current].parent; // Go to old NYT node
tree[current].count++; // Increment count of old NYT node
pause();
}
while (current != root) // Is this the root node?
{
current = tree[current].parent; // Go to parent node
max = highestInBlock(tree[current].count); // Node number max in block?
if (current != max && tree[current].parent != max) {
addMessage(" Swapping nodes " +current+ " and " +max);
swap(current, max); // Switch node with highest node in block
pause();
current = max;
}
tree[current].count++; // Increment node weight
}
drawTrie(dispRect, null);
}
public void addMessage(String line)
{
message.addItem(line);
message.makeVisible(message.countItems() - 1 );
}
public void run()
{
String code;
char hold;
int bitsent;
try {
hold = inputField.whatKey(_key);
try {
code = char2code( hold );
addMessage("Character '" +hold+ "' FOUND:");
addMessage(" Sending code for '" +hold+ "'");
bitsent = char2code(hold).length();
} catch (NoSuchElementException d) {
addMessage("Character '" + hold + "' not found:");
code = char2code(NYT) + (char)_key;
addMessage (" Sending NYT and character '" + hold + "'");
pause();
bitsent = char2code(NYT).length() + 8;
}
sender.wire(code);
stats.addBits(8, bitsent);
outputField.addString("" + hold );
addMessage("Updating the tree:");
updateTree(hold);
} catch (NotValidChar d) {
}
update(gc);
running = false;
}
/*
* Pause
*
* waits a few seconds. May also wait for a button press.
*/
public synchronized void pause()
{
if (waitforclick) {
try {
next.show();
update(gc);
wait();
next.hide();
} catch (InterruptedException e) {};
} else {
try {
wait(1000);
} catch (InterruptedException e) {};
}
drawTrie(dispRect, null);
update(gc);
}
public synchronized boolean handleEvent(Event event) {
if (event.target == next && event.id == Event.ACTION_EVENT) {
notify();
}
if (event.target == continuous && event.id == Event.ACTION_EVENT) {
waitforclick = false;
}
if (event.target == playpause && event.id == Event.ACTION_EVENT) {
waitforclick = true;
}
return super.handleEvent(event);
}
public boolean keyDown(Event e, int key) {
_event = e;
_key = key;
if (!running) {
running = true;
t = new Thread(this);
t.start();
}
return true;
}
boolean running;
Event _event;
int _key;
Rectangle dispRect; // The area where the tree is displayed
InputField inputField;
OutputField outputField;
Send sender;
List message;
CheckboxGroup mode;
Checkbox playpause;
Checkbox continuous;
Button next;
BitStats stats;
Dimension size; // The size of the applet
Thread t;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -