Morphic Molecules
Technotes
MsgPanel Applet
©Mona M. Everett, Ph.D.; MsgPanel Applet ©Mona M. Everett, Ph.D. All rights reserved. MsgPanel Applet May be used for any NONCOMMERCIAL USE. Java is a copyrighted product and licensed trademark of Sun Microsystems. Javascript is a copyrighted product and licensed trademark of Netscape, Inc.
Please note that some parameters are listed under more than one topic. For example, Font Color is listed under both Colors and Fonts.
- Integer Parameters
- Parameters which are translated into integers internally:
- Margin Widths, Border Width, Font Size
- Colors
- Parameters which are translated into Color objects internally:
- Font Color, Border Color, BGColor
- Fonts
- Parameters which have to do with the Font used to print the message:
- Font Face, Font Size, Font Color, Font Style
- Strings
- Values passed into internal strings. These are usually but not always restricted to values in an acceptable list:
- Inset, Justification, Font Face, Message
- Commented Paint () method of the msgPanel applet
- The paint() method actually draws the applet. This method is presented line by line with comments.
- Source Code for the Entire Applet
- A link to msgPanel.java file. You can save it to your hard disk.
Integer
Parameters:
Margin and Border Widths; Font Size
| Internal Variable | Set Method | Get Method | Default | Applet Param |
| vmargin | setVMargin(string) | string = getVMargin() | 10 | vmargin |
| hmargin | setHMargin(string) | string = getHMargin() | 10 | hmargin |
| borderwidth | setBorderWidth(string) | string = getBorderWidth | 1 | borderwidth |
| theFontsize | setFontSize(string) | string = getFontSize | 14 | fontsize |
General Method Notes
Java Implementation
Set Method
public void setVMargin(String what)
{
if (what == null) { vmargin = 10; }
else
{
try
{
vmargin = Integer.parseInt(what);
}
catch (NumberFormatException e)
{
vmargin = 10;
System.err.println("Received vmargin " + what + " is not an integer! Set to default of 10");
}
}
repaint();
}
Get Method
public String getVMargin()
{
String astr = new String();
return astr.valueOf(vmargin);
}
Javascript Manipulation
function setMargins()
{
// get the values
var hm = document.forms[0].hrmarg.value
var vm = document.forms[0].vtmarg.value
var bm = document.forms[0].bwid.value
//alert('hm = ' + hm +'\nvm = ' + vm + '\nbm = ' + bm)
// change to integers
var _hm = parseInt(hm)
var _vm = parseInt(vm)
var _bm = parseInt(bm)
//alert('_hm = ' + _hm +'\n_vm = ' + _vm + '\n_bm = ' + _bm)
// check for validity and set values
if (isNaN(_hm) || hm == '' ) alert ("Hmargin setting is not a number or is empty.")
else document.theMsg.setHMargin(_hm.toString)
if (isNaN(_vm) || vm == '' ) alert ("Vmargin setting is not a number or is empty.")
else document.theMsg.setVMargin(_vm.toString())
if (isNaN(_bm) || bm == '' ) alert ("Borderwidth setting is not a number or is empty.")
else document.theMsg.setBorderWidth(_bm.toString())
}
Colors:
Font,
Background, and Border Colors
| Internal Variable | Set Method | Get Method | Default | Applet Param |
| theFontColor | setFontColor(string) | string = getFontColor() | blue | fontcolor |
| theBorderColor | setBorderColor(string) | string = getBorderColor() | yellow | bordercolor |
| theBGColor | setBGColor(string) | string = getBGColor() | blue | bgcolor |
General Method Notes
Java Implementation
Set Method
public void setFontColor(String what)
{
if (what == null)
{
what = "blue";
System.err.println("Null value was passed for font color; setting it to default blue");
}
theFontColor = chooseColor(what);
repaint();
}
Get Method
public String getFontColor()
{
return theFontColor.toString();
}
color = ChooseColor (string)
(private internal function)
Color chooseColor(String thecolor)
{
Color what;
System.err.println("You passed the color " + thecolor );
if (thecolor != null)
{
if (thecolor.equals("red")) { what = Color.red; }
else if (thecolor.equals("green")) { what = Color.green; }
else if (thecolor.equals("blue")) { what = Color.blue; }
else if (thecolor.equals("pink")) { what = Color.pink; }
else if (thecolor.equals("orange")) { what = Color.orange; }
else if (thecolor.equals("magenta")) { what = Color.magenta; }
else if (thecolor.equals("cyan")) { what = Color.cyan; }
else if (thecolor.equals("white")) { what = Color.white; }
else if (thecolor.equals("yellow")) { what = Color.yellow; }
else if (thecolor.equals("gray")) { what = Color.gray; }
else if (thecolor.equals("black")) { what = Color.black; }
else if (thecolor.equals("darkgray")) {what = Color.darkGray; }
else if (thecolor.equals("lightgray")) {what = Color.lightGray; }
else
{
System.err.println("Valid colors are: \nred, \ngreen, \nblue, \npink, \norange, \nmagenta, \ncyan, \nwhite, \nyellow, \ngray, \ngray, \nlightgray, \ndarkgray.");
System.err.println("Color set to default gray.");
what = Color.darkGray;
}
}
else {
System.err.println("No color was passed to function chooseColor.");
what=Color.darkGray;
}
return what;
}
Javascript Manipulation
function doColor(which)
{
var wc = which.name
var i = which.selectedIndex
var ac = which.options[i].text
if (wc == 'bcolor' ) document.theMsg.setBorderColor(ac)
if (wc == 'fcolor' ) document.theMsg.setFontColor(ac)
if (wc == 'bgcolor' ) document.theMsg.setBGColor(ac)
}
Fonts:
Font
Face (name), Font Size, Font Style and Font Color
| Internal Variable | Set Method | Get Method | Default | Applet Param |
| theFontStyle | setFontStyle(string) | string = getFontStyle | bold | fontstyle |
| See String Parameters for Font Face Details | ||||
| theFontName | setFontName(string | string = getFontName | Courier | fontname |
| See Integer Parameters for Font Size Details | ||||
| theFontSize | setFontSize(string) | string= getFontSize() | 14 | fontsize |
| See Colors for Font Color Details | ||||
| theFontColor | setFontColor(string) | string= getFontColor() | dark gray | fontcolor |
General Method Notes
Java Implementation
Set Method
public void setFontStyle(String what)
{
int astyle;
astyle = Font.PLAIN;
if (what == null || what.equals("")) { what = "Helvetica"; }
what = what.toLowerCase();
if (what.equals("plain") || what.equals("bold") || what.equals("italic") || what.equals("italics") || what.equals("bolditalic") || what.equals("bolditalics") || what.equals("italicbold") || what.equals("italicsbold") )
{
if (what.equals("plain"))
{ astyle = Font.PLAIN ;}
else if (what.equals("bold"))
{ astyle = Font.BOLD ; }
else if (what.equals("italics") || what.equals("italic") )
{ astyle = Font.ITALIC ; }
else if (what.equals("bolditalic") || what.equals("bolditalics") || what.equals("italicbold") || what.equals("italicsbold") )
{ astyle = Font.BOLD | Font.ITALIC; }
else astyle = Font.PLAIN;
System.err.println("Setting font style to " + what );
}
else
{
System.out.println("The style you suggested is " + what + ". The only styles allowed are plain, bold, italics, and bolditalics.");
what = "bold";
}
theFontStyle = astyle;
repaint();
}
Get Method
public String getFontStyle()
{
String astylestr;
astylestr = "";
if (theFontStyle == Font.PLAIN )
{ astylestr = "plain"; }
else if (theFontStyle == Font.BOLD )
{ astylestr = "bold"; }
else if (theFontStyle == Font.ITALIC )
{ astylestr = "italic"; }
else if (theFontStyle == (Font.BOLD | Font.ITALIC))
{ astylestr = "bolditalic"; }
else astylestr = "Not a recognized font style!";
return astylestr;
}
Javascript Manipulation
function doFontStyle(which)
{
var i = which.selectedIndex;
var ff = which.options[i].text;
if (ff != 'plain' && ff != 'bold' && ff != 'italic' && ff != 'bolditalic') alert("Font Style must be plain, bold, italic, or bolditalic.")
else document.theMsg.setFontStyle(ff);
}
String
Variables:
Justification, Inset, FontFace, Font Style and Msg
| Internal Variable | Set Method | Get Method | Default | Applet Param |
| justification | setJustification(string) | string = getJustification() | center | justification |
| theInset | setInset(string) | string = getInset() | none | inset |
| See Font Parameters for Font Face Details | ||||
| theFontName | setFontName | string = getFontName() | courier | fontname |
| theMsg | setMsg(string) | string == getMsg | "" | message |
General Method Notes
Java Implementation
Set Method
public void setInset(String what)
{
//System.err.println("Parameter inset is " + ps);
if (what == null || what == "") { what = "none"; }
what = what.toLowerCase();
if (what == "none" || what == "raised" || what == "lowered")
{
theInset = "none";
System.err.println("Valid inset values are none, raised, or lowered.");
System.err.println("Inset set to default none");
}
theInset = what;
repaint();
}
Get Method
public String getInset()
{
return theInset;
}
Javascript Manipulation
function doInset(which)
{
var i = which.selectedIndex
var is = which.options[i].text
var is = is.toLowerCase()
alert( 'is = ' + is)
if (is != 'none' && is != 'raised' && is != 'lowered') alert("Inset must be raised, lowered, or none.")
else document.theMsg.setInset(is)
}
All of the changes implemented by set routines are actually realized in the paint() method which actually renders the face of the application. Needless to say, this can be quite complex for a graphics object with many properties. Here we ill dissect the paint routine of MsgPanel line by line.
public synchronized void paint()
Declares the paint method as
{ beginning delineator of the method
int i, j,w,h , c;
int xs,ys,xw,yh;
int cx, cy;
char l[] = new char[1];
local
variables established
w = size().width;
Gets the width of the applet into w.
h = size().height;
Gets the height of the applet into h.
// Draw the Applet Border
xs = 0 ; ys = 0;
left and top coordinates of intial border rectangle in
relation to applet
xw = w-1 ; yh = h-1;
width and height of initial border rectangle as
defined by applet border width(w) and height(h)
g.setColor(theBorderColor);
set the graphics pen; i.e., the color that any
subsequent drawing will use, to the color designated for border (theBorderColor)
for ( c = 0 ; c < borderwidth ; c++ )
Set up a for loop which will draw rectangles whose
dimensions decrease by two pixels and width and whose top,left coodinates move
down and right by one pixel for every iteration of the loop.
{ starting delimiter of for loop routines
g.drawRect(xs,ys,xw,yh);
"g" represents the graphics context (object)
of the applet's canvas; drawRect(left, top, width,height) draws an unfilled
rectangle in the current color.
xs += 1;
increase left by one
ys += 1;
increase y by one
xw -= 2;
decrease width by two; i.e., one pixel on each side of
the rectangle
yh -= 2;
decrease height by two; i.e., one pixel each on top
and bottom of rectangle
} ending delimiter of for loop statements
// draw colored rect;
cx = (w - (2 * hmargin ) );
set cx to the width for the colored background
rectangle; cx will be the applet width (w) less twice the horizontal
margin(hmargin).
cy = (h - (2 * vmargin ) );
set cy to the height for the colored background
rectangle; cyx will be the applet height (w) less twice the vertical
margin(vmargin).
g.setColor(theBGColor);
set the color of the graphics object (g) pen to the
background rectangle color
g.fillRect(hmargin,vmargin, cx , cy );
use the graphics object( g ) fillRect(x, y, width,
height) routine to draw a filled rectangle in the current color.
//draw the borders which will make the rectangle appear raised or lowered
if ( theInset.equals("none") )
check to see if you need to raise or lower the box.
String objects cannot be directly compared to a string literal representation of
the string. You must use the String.equals("thestring") method of
string to do the test.
{ beginning delimiter of if test block statemetns
System.err.println("No inset.");
just prints a system message; could simply be left
blank.
} ending delimiter of if true block
else
if theInset is not "none" then we have to do
some drawing.
{begining delimiter of else statement block
g.setColor(Color.black);
set the graphic object (g) pen to black; we draw a
black rectangle first whether the rectangle is to be raised or lowered.
g.drawRect(hmargin,vmargin,cx,cy);
use drawRect(left, top, width, height) to draw a
non-filled rectangle in the current color (black). cx and cy were previously
calculated to be with width and height respectively of the inset colored block
g.drawRect(hmargin + 1,vmargin + 1 , cx -2, cy -
2 );
the colored block will
always be raised or lowered by two pixels so draw another unfilled rectangle
which has shrunk by one pixel on each side.
g.setColor(Color.white);
set the current graphics object (g) color to the stock
color white; the hilited sides will be drawn in white but which sides are hilted
differ between a raised and lowered rectangle. The light source is always off
of the upper left corner of the object being rendered.
if ( theInset.equals("raised") )
the top and left sides of the block will be hilited
for a raised block.
{ starting delimiter of inset = raised block
g.drawLine(hmargin,vmargin,hmargin, vmargin +
cy);
use drawLine(startX, startY,
endX, endY) to draw a line down the left side of the colored block; this is
covering up some of the black rectangle. Both start and end x coordinates are
hmargin.
g.drawLine(hmargin,vmargin,hmargin + cx, vmargin
);
use drawLine to draw a line
across the top of the colored block. Both start and end y coordinates are
hmargin
g.drawLine(hmargin+1,vmargin+1,hmargin+1, vmargin
+ cy - 1);
draw another vertical
line, one pixel to the right of the original and one pixel shorter on each end.
g.drawLine(hmargin+1,vmargin+1,hmargin + cx - 1,
vmargin +1);
draw another
horizontal line, one pixel beneath the original and one pixel shorter on each
end.
} ending delimiter of raised inset = true block
else
if not raised it must be lowered; if the rectangle is
lowered, the right and bottom sides are hilited.
{ beginning delimiter of lowered inset = true block
g.drawLine(hmargin + cx ,vmargin ,hmargin + cx ,
vmargin + cy);
use
drawLine(startX, startY, endX, endY) to draw a white vertical line over the
right side of the black rectangle previously drawn; startX and endX are now both
hmargin + cx ; i.e., the line has moved to the right side of the rectangle.
g.drawLine(hmargin,vmargin + cy,hmargin + cx,
vmargin + cy);
draw a white line
across the bottom of the rectangle
g.drawLine(hmargin - 1 + cx, vmargin + 1,hmargin
- 1 + cx, vmargin + cy - 1);
draw
a second white vertical line one pixel left of the original and one pixel
shorter on each end
g.drawLine(hmargin+1,vmargin + cy - 1,hmargin +
cx - 1, vmargin - 1 + cy);
draw a
second white horizontal line one pixel above the original and one pixel shorter
on each end
} ending delimiter of the else (inset raised = false) statement block
} ending delimiter of the else block for (inset none = false) block
// set everything up to draw the string
g.setColor(theFontColor);
set the graphics object ( g) pen to the color in which
you will draw the string
theFont = new java.awt.Font(theFontName,
theFontStyle, theFontSize);
create
the font using the variables which you have established for font face (name)
style, and size
theFontMetrics = getFontMetrics(theFont);
get the fontMetrics structure (object) for the font
you just created
i = theFontMetrics.stringWidth(theMsg);
get the width, in pixels, for the string you want to
draw; if this is longer than the inset block, there is no clipping and it will
draw as much of it into the applet as it can. It is up to the user to make it
fit.
j = theFont.getSize();
determine the height of the font
g.setFont(theFont);
set the font of the graphics object (g) to the font
which you have created
cx = (w - i ) / 2;
reuse cx to calculate the x coordinate at which to
start drawing the string if the string is to be centered horizontally.
Distribute the difference between the width of the applet and the width of the
string to both sides of the string. You will have recalculate this for right and
left justification.
cy = (h + (j/2) ) / 2;
reuse cy to calculate the y coordinate at which to
start drawing the string if the string is to be centered vertically. The
string will always be centered vertically.
// finally, draw the string
At last, you get to draw the string; it will always be
vertically in the middle. Its left coordinate must be adjusted to fit the
justification type
if ( justification.equals("center") )
if the string is to be centered
{ g.drawString(theMsg,cx,cy); }
use drawString(theString, startX,startY) to draw the
string; the cx and cy coordinates that you previous calculated are used
else if ( justification.equals("left")
)
if the string is to be left
justified
{ g.drawString(theMsg,hmargin + 5, cy); }
draw the string using hmargin instead of cx as startX
else if ( justification.equals("right")
)
if the string is to be right
justified
{ start of right justification statement block
cx =( (w - (2 * hmargin)) - i ) - 5;
recalculate cx so that the string will end five pixels
to the left of the right edge of the colored block
g.drawString(theMsg, cx,cy);
draw the string using the adjusted cx
} end of right justify statement block
} end of paint method