CodePanel.java
/***********************************************************/
/* CodePanel class */
/* A MergeSort demonstration algorithm */
/* By JungSig Min attending Myong Ji University */
/***********************************************************/
class CodePanel extends Panel {
static final int marginX = 20; //constant variables for the positions
static final int marginY = 10; // for code panel
static final int offsetX = 1;
static final int offsetY = 1;
static final int none = -1;
String code [];
String explanations [];
Font font = new Font ("TimesRoman", Font.PLAIN, 14);
int lineHeight;
int baseLine;
int maxWidth = 0;
int highlightedLine = none;
Label explanation = new Label ();
Applet applet;
Color backgroundColor= new Color(24,255,208);
Color explainColor = Color.gray;
//constructor for CodePanel
public CodePanel (String code [], String explanations [], Applet applet) {
this.code = code;
this.explanations = explanations;
this.applet = applet;
setBackground (backgroundColor);
explanation.setBackground (explainColor);
add (explanation);
explanation.hide ();
}
//preferredSize
public Dimension preferredSize () {
return new Dimension (maxWidth + 2 * marginX
, code.length * lineHeight + 2 * marginY);
}
//addNotify
public void addNotify () {
super.addNotify ();
setFont (font);
FontMetrics metrics = getFontMetrics (font);
baseLine = metrics.getAscent ();
lineHeight = baseLine + metrics.getDescent ();
for (int i = 0; i < code.length; i++) {
maxWidth = Math.max (maxWidth, metrics.stringWidth (code [i]));
}
maxWidth+=40;
}
//reset
public void reset () {
if (highlightedLine != none) {
colorLine (highlightedLine, Color.white);
}
highlightedLine = none;
}
//highlight the currently executed line
public void highlightLine (int line) {
if (highlightedLine != none) {
colorLine (highlightedLine, backgroundColor);
}
highlightedLine = line;
if (highlightedLine != none) {
colorLine (highlightedLine, Color.orange);
}
}
//make color rectangle
public void colorLine (int line, Color color) {
Graphics g = getGraphics ();
int y = marginY + line * lineHeight;
g.setColor (color);
g.fillRect (0, y, size ().width - 1, lineHeight);
g.setColor (Color.black);
g.drawString (code [line], marginX, y + baseLine);
}
//paint the code panel
public void paint (Graphics g) {
int y = marginY + baseLine;
for (int i = 0; i < code.length; i++, y += lineHeight) {
g.drawString (code [i], marginX, y);
}
highlightLine (highlightedLine);
}
//if there's mouseExit event
public boolean mouseExit (Event event, int x, int y) {
explanation.hide ();
validate ();
return true;
}
//if there's mouseUp event
public boolean mouseUp (Event event, int x, int y) {
int line = (y - marginY) / lineHeight;
if ((line <= explanations.length)
|| (explanations [line].equals (""))) {
explanation.setText (explanations [line]);
explanation.setBackground (explainColor);
explanation.setForeground (Color.black);
explanation.validate ();
explanation.show ();
}
else {
explanation.hide ();
}
validate ();
explanation.move (marginX + offsetX
, marginY + offsetY + (line + 1) * lineHeight);
return true;
}
}