I am trying to convert an applet into java app. But I m getting nothing except an empty frame. I would be grateful if anyone could help me through this. Pleaseeee!
My modified code here:
import java.awt.event.WindowEvent;
import java.util.*;
import java.awt.*;
import java.awt.event.WindowListener;
class node {
int key;
node left;
node right;
node parent;
float x, y;
float incx, incy;
boolean highlight_node;
Color highlight_colour;
boolean hide_edge;
}
public class try2 extends Frame{
node root;
node rotation_node;
int num_edges;
float edges[][] = new float[100][4];
rotate rotate_thread;
search search_thread;
boolean only_root_children;
boolean alternate_nodes;
boolean allow_rotation;
boolean allow_search;
final int MAX_ROTATABLE_NODES = 100;
int rotatable_nodes[] = new int[MAX_ROTATABLE_NODES];
int num_rotatable_nodes = 0;
TextField search_field;
int search_key;
Font f = new Font( "TimesRoman", Font.PLAIN, 14 );
FontMetrics fm = getFontMetrics( f );
final Color LineColor = Color.black;
final Color HighlightColor = Color.orange;
final Color NodeColor = Color.cyan;
final int NODE_WIDTH = 40;
final int NODE_HEIGHT = 30;
public static void main(String[] args)
{
try2 app=new try2();
app.setSize(400,400);
app.setVisible(true);
app.setLayout(new FlowLayout());
}
try2() {
position_tree( root, 0, 0 );
if (allow_search) {
setLayout( new FlowLayout(FlowLayout.LEFT) );
search_field = new TextField( 4 );
search_field.setEditable( true );
add( new Label( "Enter search key:", Label.RIGHT ) );
add( search_field );
}
}
public void start() {
if (rotate_thread != null && rotate_thread.isAlive())
rotate_thread.resume();
if (search_thread != null && search_thread.isAlive())
search_thread.resume();
}
public void stop() {
if (rotate_thread != null && rotate_thread.isAlive())
rotate_thread.suspend();
if (search_thread != null && search_thread.isAlive())
search_thread.suspend();
}
public void paintComponent( Graphics g ) {
if (num_edges > 0) {
g.setColor( LineColor );
for (int i=0; i<num_edges; i++)
g.drawLine( (int) edges[i][0], (int) edges[i][1],
(int) edges[i][2], (int) edges[i][3] );
}
draw_tree( root, g );
}
Image buffer_image;
Graphics buffer_graphics;
Dimension buffer_size;
public synchronized void update( Graphics g ) {
if (buffer_image == null) {
buffer_size = size();
buffer_image = createImage( buffer_size.width, buffer_size.height );
buffer_graphics = buffer_image.getGraphics();
buffer_graphics.setFont( getFont() );
}
buffer_graphics.setColor( Color.white );
buffer_graphics.fillRect( 0, 0, buffer_size.width, buffer_size.height );
paint( buffer_graphics );
g.drawImage( buffer_image, 0, 0, null );
}
@Override
public boolean mouseDown( Event evt, int x, int y ) {
if (! allow_rotation)
return false;
if (rotate_thread != null && rotate_thread.isAlive())
return false;
rotation_node = find_closest_node( root, x, y );
if (rotation_node == root)
rotation_node = null;
else if (only_root_children && rotation_node.parent != root)
rotation_node = null;
else if (alternate_nodes && rotatable_nodes[0] != rotation_node.key)
rotation_node = null;
else if (num_rotatable_nodes != 0) {
int i;
for (i=0; i<num_rotatable_nodes; i++)
if (rotatable_nodes[i] == rotation_node.key)
break;
if (i == num_rotatable_nodes)
rotation_node = null;
}
if (alternate_nodes && rotation_node != null) {
int temp = rotatable_nodes[0];
rotatable_nodes[0] = rotatable_nodes[1];
rotatable_nodes[1] = temp;
}
if (rotation_node != null) {
rotation_node.highlight_node = true;
rotation_node.parent.highlight_node = true;
repaint();
}
return true;
}
@Override
public boolean mouseUp( Event evt, int x, int y ) {
if (rotate_thread != null && rotate_thread.isAlive())
return false;
if (rotation_node == null)
return true;
rotation_node.highlight_node = false;
rotation_node.parent.highlight_node = false;
rotate_thread = new rotate(this);
rotate_thread.start();
return true;
}
@Override
public boolean action( Event evt, Object arg ) {
if (! allow_search)
return false;
if (search_thread != null && search_thread.isAlive())
return false;
if (evt.target instanceof TextField) {
try{
search_key = Integer.parseInt( search_field.getText() ); }
catch (NumberFormatException e) {
System.out.println( "ERROR in BST applet: `" + search_field.getText() + "' is not an integer" );
return true;
}
} else
System.out.println( "ERROR in BST applet: Unrecognized UI event" );
return true;
}
void parse_parameters() {
String keys;
keys = getParameter( "keys" );
if (keys != null) {
StringTokenizer t = new StringTokenizer( keys );
while (t.hasMoreTokens())
insert( Integer.parseInt(t.nextToken()) );
}
keys = getParameter( "action" );
if (keys != null) {
StringTokenizer t = new StringTokenizer( keys );
while (t.hasMoreTokens()) {
String s = t.nextToken();
if (s.equals("rotate"))
allow_rotation = true;
else if (s.equals("search"))
allow_search = true;
else
System.out.println( "Error in BST applet: unrecognized action `" + s + "' in param list." );
}
}
only_root_children = (getParameter( "only_root_children" ) != null);
keys = getParameter( "alternate_nodes" );
if (keys != null) {
alternate_nodes = true;
StringTokenizer t = new StringTokenizer( keys );
while (t.hasMoreTokens()) {
int i = Integer.parseInt(t.nextToken());
if (num_rotatable_nodes < MAX_ROTATABLE_NODES)
rotatable_nodes[ num_rotatable_nodes++ ] = i;
}
if (num_rotatable_nodes != 2)
System.out.println( "Error in BST applet: expected 2 keys for `alternate_nodes' param, but got " + num_rotatable_nodes );
}
keys = getParameter( "rotatable_nodes" );
if (keys != null) {
StringTokenizer t = new StringTokenizer( keys );
while (t.hasMoreTokens()) {
int i = Integer.parseInt(t.nextToken());
if (num_rotatable_nodes < MAX_ROTATABLE_NODES)
rotatable_nodes[ num_rotatable_nodes++ ] = i;
}
}
}
node find_closest_node( node root, int x, int y ) {
node l_closest, r_closest;
int l_dist, r_dist, root_dist;
if (root == null)
return null;
l_closest = find_closest_node( root.left, x, y );
r_closest = find_closest_node( root.right, x, y );
l_dist = dist_to_node( l_closest, x, y );
r_dist = dist_to_node( r_closest, x, y );
root_dist = dist_to_node( root, x, y );
if (l_dist <= r_dist && l_dist <= root_dist)
return l_closest;
if (r_dist <= l_dist && r_dist <= root_dist)
return r_closest;
return root;
}
int dist_to_node( node n, int x, int y ) {
if (n == null)
return Integer.MAX_VALUE;
return Math.abs( x - (int) n.x ) + Math.abs( y - (int) n.y );
}
int position_tree( node root, int upper, int left ) {
int l_width, r_width;
if (root == null)
return 0;
l_width = position_tree( root.left, upper + NODE_HEIGHT, left );
root.x = left + l_width + NODE_WIDTH/2;
root.y = upper + NODE_HEIGHT/2;
r_width = position_tree( root.right, upper + NODE_HEIGHT,
left + l_width + NODE_WIDTH );
return l_width + NODE_WIDTH + r_width;
}
void draw_tree( node root, Graphics g ) {
if (root == null)
return;
draw_tree( root.left, g );
draw_tree( root.right, g );
if (root.parent != null)
draw_parent_edge( root, g );
draw_node( root, g );
}
void draw_parent_edge( node n, Graphics g ) {
if (! n.hide_edge) {
g.setColor( LineColor );
g.drawLine( (int) n.x, (int) n.y, (int) n.parent.x, (int) n.parent.y );
}
}
void draw_node( node n, Graphics g ) {
String key = Integer.toString( n.key );
int width = fm.stringWidth( key );
int height = fm.getAscent();
if (n.highlight_node)
if (n.highlight_colour == null)
g.setColor( HighlightColor );
else
g.setColor( n.highlight_colour );
else
g.setColor( NodeColor );
g.fillOval( (int) n.x - NODE_WIDTH/2, (int) n.y - NODE_HEIGHT/2,
NODE_WIDTH, NODE_HEIGHT );
g.setColor( LineColor );
g.drawOval( (int) n.x - NODE_WIDTH/2, (int) n.y - NODE_HEIGHT/2,
NODE_WIDTH, NODE_HEIGHT );
g.drawString( key, (int) n.x - width/2, (int) n.y - height/2 + fm.getAscent() - 1 );
}
void insert( int k ) {
node x, y, new_node;
y = null;
x = root;
while (x != null) {
y = x;
if (k == x.key)
return;
else if (k < x.key)
x = x.left;
else
x = x.right;
}
new_node = new node();
new_node.key = k;
new_node.parent = y;
new_node.left = null;
new_node.right = null;
if (y == null)
root = new_node;
else
if (k < y.key)
y.left = new_node;
else
y.right = new_node;
}
void rotate( node c ) {
node p = c.parent;
node gp = p.parent;
if (c == p.left) {
p.left = c.right;
if (p.left != null)
p.left.parent = p;
c.right = p;
p.parent = c;
} else {
p.right = c.left;
if (p.right != null)
p.right.parent = p;
c.left = p;
p.parent = c;
}
if (gp == null)
root = c;
else if (p == gp.left)
gp.left = c;
else
gp.right = c;
c.parent = gp;
}
private String getParameter(String string) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
class rotate extends Thread implements Runnable {
final int ROTATE_TIME = 1000;
try2 tree;
rotate( try2 t ) {
tree = t;
}
public void run() {
node c, p, gc, gp;
c = tree.rotation_node;
p = c.parent;
gp = p.parent;
if (c == p.right)
gc = c.left;
else
gc = c.right;
c.highlight_node = true;
p.highlight_node = true;
for (int i=0; i<tree.NODE_HEIGHT; i++) {
c.y --;
if (c == p.left)
shift_up( c.left );
else
shift_up( c.right );
p.y ++;
if (c == p.right)
shift_down( p.left );
else
shift_down( p.right );
tree.repaint();
try{
Thread.sleep( ROTATE_TIME/3/tree.NODE_HEIGHT );}
catch (InterruptedException e) {}
}
c.highlight_node = false;
p.highlight_node = false;
tree.num_edges = 0;
if (gc != null) {
tree.num_edges = 1;
gc.highlight_node = true;
gc.hide_edge = true;
tree.edges[0][0] = gc.x;
tree.edges[0][1] = gc.y;
tree.edges[0][2] = c.x;
tree.edges[0][3] = c.y + tree.NODE_HEIGHT/2;
float dx = p.x - tree.edges[0][2];
float dy = p.y - tree.edges[0][3];
float loops = (Math.abs(dx) > Math.abs(dy) ? Math.abs(dx) : Math.abs(dy)) / 2;
int sleep_time = (int) (ROTATE_TIME/3/loops);
if (sleep_time < 5)
sleep_time = 5;
dx = dx/loops;
dy = dy/loops;
tree.repaint();
for (int i=0; i < loops; i++) {
tree.edges[0][2] += dx;
tree.edges[0][3] += dy;
tree.repaint();
try {
Thread.sleep( sleep_time );}
catch (InterruptedException e) {}
}
gc.highlight_node = false;
}
if (gp != null) {
tree.num_edges ++;
gp.highlight_node = true;
p.hide_edge = true;
tree.edges[tree.num_edges-1][0] = gp.x;
tree.edges[tree.num_edges-1][1] = gp.y;
tree.edges[tree.num_edges-1][2] = p.x;
tree.edges[tree.num_edges-1][3] = p.y - tree.NODE_HEIGHT/2;
float dx = c.x - tree.edges[tree.num_edges-1][2];
float dy = c.y - tree.edges[tree.num_edges-1][3];
float loops = (Math.abs(dx) > Math.abs(dy) ? Math.abs(dx) : Math.abs(dy)) / 2;
int sleep_time = (int) (ROTATE_TIME/3/loops);
if (sleep_time < 5)
sleep_time = 5;
dx = dx/loops;
dy = dy/loops;
tree.repaint();
for (int i=0; i < loops; i++) {
tree.edges[tree.num_edges-1][2] += dx;
tree.edges[tree.num_edges-1][3] += dy;
tree.repaint();
try {
Thread.sleep( sleep_time ); }
catch (InterruptedException e) {}
}
p.hide_edge = false;
gp.highlight_node = false;
}
if (gc != null)
gc.hide_edge = false;
tree.num_edges = 0;
tree.rotate( c );
tree.repaint();
}
void shift_up( node n ) {
if (n == null)
return;
shift_up( n.left );
shift_up( n.right );
n.y --;
}
void shift_down( node n ) {
if (n == null)
return;
shift_down( n.left );
shift_down( n.right );
n.y ++;
}
}
class search extends Thread implements Runnable {
final int SEARCH_TIME = 800;
try2 tree;
search( try2 t ) {
tree = t;
}
public void run() {
node n, pn;
if (tree.root != null ) {
clear_highlights( (node) tree.root);
tree.repaint();
try{
Thread.sleep( SEARCH_TIME );}
catch (InterruptedException e) {}
}
pn = null;
n = (node) tree.root;
while (n != null && n.key != tree.search_key) {
n.highlight_node = true;
tree.repaint();
try{
Thread.sleep( SEARCH_TIME );}
catch (InterruptedException e) {}
pn = n;
if (tree.search_key < n.key)
n = n.left;
else
n = n.right;
}
if (n != null) {
n.highlight_node = true;
tree.repaint();
try {
Thread.sleep( SEARCH_TIME ); }
catch (InterruptedException e) {}
n.highlight_colour = Color.green;
} else if (pn != null) {
pn.highlight_node = true;
pn.highlight_colour = Color.red;
}
tree.repaint();
}
void clear_highlights( node n ) {
if (n == null)
return;
clear_highlights( n.left );
clear_highlights( n.right );
n.highlight_node = false;
n.highlight_colour = null;
}
public class try2 extends Frame implements WindowListener{
private int search_key;
private Object root;
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
this.addWindowListener(this);
}
public void windowOpened(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
}
}