-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeDisplayPanel.java
More file actions
231 lines (204 loc) · 7.53 KB
/
TreeDisplayPanel.java
File metadata and controls
231 lines (204 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPanel;
/**
* A panel that maintains a picture of a binary tree.
*/
public class TreeDisplayPanel extends JPanel implements MouseMotionListener, MouseListener {
private BinaryTreeNode< ? > tree;
private BinaryTreeNode< ? > selectedNode;
private Point selectedPoint = null;
private int gridwidth;
private int gridheight;
/**
* Stores the pixel values for each node in the tree.
*/
private Map< BinaryTreeNode< ? >, Point > coordinates = new HashMap< BinaryTreeNode< ? >, Point >( );
/**
* Constructs a panel, saving the tree and drawing parameters.
*/
public TreeDisplayPanel( BinaryTreeNode< ? > tree, int gridwidth,
int gridheight ) {
this( tree );
this.tree = tree;
this.gridwidth = gridwidth;
this.gridheight = gridheight;
}
/**
* Constructs a panel, saving the tree and drawing parameters.
*/
public TreeDisplayPanel( BinaryTreeNode< ? > tree ) {
this.tree = tree;
this.gridwidth = 25;
this.gridheight = 45;
this.addMouseMotionListener( this );
this.addMouseListener( this );
}
/**
* Changes the tree rendered by this panel.
*/
public void setTree( BinaryTreeNode< ? > root ) {
tree = root;
repaint( );
}
/**
* Changes the tree rendered by this panel.
*/
public void setSelected( BinaryTreeNode< ? > node ) {
selectedNode = node;
repaint( );
}
/**
* Draws the tree in the panel. First it computes the coordinates
* of all the nodes with an inorder traversal, then draws them
* with a postorder traversal.
*/
public void paintComponent( final Graphics g ) {
super.paintComponent( g );
if ( tree == null ) {
return;
}
tree.traverseInorder( new BinaryTreeNode.Visitor( ) {
private int x = 3 * gridwidth;
public void visit( BinaryTreeNode node ) {
String data = node.getData( ).toString( );
if ( coordinates.get( node ) == null ) {
FontMetrics fm = g.getFontMetrics( );
Rectangle r = fm.getStringBounds( data, g ).getBounds( );
gridheight = Math.max( gridheight, 2 * ( int ) r.getHeight( ) + 2 );
if ( x - r.getWidth( ) / 2 < 0 ) {
x = ( int ) ( r.getWidth( ) / 2 + gridwidth );
}
int y = gridheight * ( depth( node ) + 1 );
int myWidth = Math.max( getWidth( ), x + 2 * gridwidth );
int myHeight = Math.max( getWidth( ), y + 2 * gridheight );
coordinates.put( node, new Point( x, y ) );
x += Math.min( gridwidth, r.getWidth( ) ) + ( 1.5 * gridwidth );
setPreferredSize( new Dimension( myWidth, myHeight ) );
}
}
} );
g.setColor( Color.LIGHT_GRAY );
g.fillRect( 0, 0, getWidth( ), getHeight( ) );
tree.traversePostorder( new BinaryTreeNode.Visitor( ) {
public void visit( BinaryTreeNode node ) {
String data = node.getData( ).toString( );
Point center = ( Point ) coordinates.get( node );
if ( node.getParent( ) != null ) {
Point parentPoint = ( Point ) coordinates
.get( node.getParent( ) );
g.setColor( Color.black );
g.drawLine( center.x, center.y, parentPoint.x, parentPoint.y );
g.setColor( Color.white );
String label = "YES";
if ( node.getParent( ).getLeft( ) == node ) {
label = "NO";
}
g.setFont( new Font( "Monospaced", Font.BOLD, 12 ) );
FontMetrics fm = g.getFontMetrics( );
g.drawString( label, ( int ) ( ( center.x + parentPoint.x ) / 2.0 ), ( int ) ( ( center.y + parentPoint.y + fm.getLineMetrics( label, g ).getHeight( ) ) / 2.0 ) );
}
g.setFont( new Font( "Monospaced", Font.BOLD, 12 ) );
FontMetrics fm = g.getFontMetrics( );
Rectangle r = fm.getStringBounds( data, g ).getBounds( );
r.setLocation( center.x - r.width / 2, center.y - r.height / 2 );
Color color = getNodeColor( node );
Color textColor = ( color.getRed( ) + color.getBlue( )
+ color.getGreen( ) < 382 )
? Color.white
: Color.black;
g.setColor( color );
g.fillRect( r.x - 2, r.y - 2, r.width + 4, r.height + 4 );
if ( node == selectedNode ) {
g.setColor( Color.RED );
g.drawRect( r.x - 4, r.y - 4, r.width + 8, r.height + 8 );
}
g.setColor( textColor );
g.drawString( data, r.x, r.y + r.height );
}
} );
}
/**
* Returns a color for the node. If the node is of a class with a
* field called "color", and that field currently contains a
* non-null value, then that value is returned. Otherwise
* a default color of yellow is returned.
*/
Color getNodeColor( BinaryTreeNode< ? > node ) {
if ( node.isLeaf() ) {
return Color.CYAN; //.GREEN;
}
return Color.yellow;
}
private int depth( BinaryTreeNode< ? > node ) {
return ( node.getParent( ) == null ) ? 0 : 1 + depth( node.getParent( ) );
}
@Override
public void mouseDragged( MouseEvent e ) {
BinaryTreeNode< ? > n = selectedNode;
if ( n != null ) {
// System.out.println( "DRAGGED: " + n.getData( ).toString( ) );
if ( selectedNode == null ) {
selectedPoint = coordinates.get( n );
}
Point center = coordinates.get( n );
selectedPoint = new Point( ( int ) ( center.getX( ) + ( e.getX( ) - selectedPoint.getX( ) ) ), ( int ) ( center.getY( ) + ( e.getY( ) - selectedPoint.getY( ) ) ) );
coordinates.put( n, selectedPoint );
this.invalidate( );
this.repaint( );
}
}
@Override
public void mouseMoved( MouseEvent e ) {
}
@Override
public void mouseClicked( MouseEvent e ) {
}
@Override
public void mousePressed( MouseEvent e ) {
selectedNode = null;
selectedPoint = null;
for ( BinaryTreeNode< ? > n : coordinates.keySet( ) ) {
Point center = ( Point ) coordinates.get( n );
String data = n.getData( ).toString( );
Graphics g = this.getGraphics( );
g.setFont( new Font( "Monospaced", Font.BOLD, 12 ) );
FontMetrics fm = g.getFontMetrics( );
Rectangle r = fm.getStringBounds( data, g ).getBounds( );
r.setLocation( center.x - r.width / 2, center.y - r.height / 2 );
if ( r.contains( e.getPoint( ) ) ) {
// System.out.println( "CLICKED: " + data );
selectedNode = n;
selectedPoint = e.getPoint( );
break;
}
}
this.invalidate( );
this.repaint( );
}
@Override
public void mouseReleased( MouseEvent e ) {
selectedNode = null;
selectedPoint = null;
this.invalidate( );
this.repaint( );
}
@Override
public void mouseEntered( MouseEvent e ) {
}
@Override
public void mouseExited( MouseEvent e ) {
}
}