home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Net: Ultimate Internet Guide
/
WWLCD1.ISO
/
pc
/
java
/
prmjatgt
/
editdraw.java
< prev
next >
Encoding:
Amiga
Atari
Commodore
DOS
FM Towns/JPY
Macintosh
Macintosh JP
NeXTSTEP
RISC OS
UTF-8
Wrap
Java Source
|
1996-08-14
|
30.2 KB
|
1,247 lines
import java.awt.*;
import java.applet.*;
/**
* A applet that edits vector drawings
*
* @version 1.0 03/24/96
* @author Kang, Dae Woong (namu@star.elim.net)
*/
public class EditDraw extends Applet
{
public void init()
{
Panel pnlDraw = new Panel();
Panel pnlSouth = new Panel();
pnlDraw.setLayout(new FlowLayoutCust(FlowLayoutCust.VERTICAL));
pnlSouth.setLayout(new FlowLayoutCust(FlowLayoutCust.HORIZONTAL));
WorkPanel work = new WorkPanel(this);
work.setLayout(null);
work.tfInput.resize(0, 0);
work.tfInput.hide();
work.add(work.tfInput);
ScrollPanel pnlCenter = new ScrollPanel(work);
pnlDraw.add(new DrawMethodBar(work, Bar.VERTICAL));
pnlDraw.add(new DrawContentBar(work, Bar.VERTICAL));
pnlDraw.add(new DrawShapeBar(work, Bar.VERTICAL));
pnlSouth.add(new ColorBar(work, Bar.HORIZONTAL));
pnlSouth.add(new ChoiceFont(work));
pnlSouth.add(new FontStyleBar(work, Bar.HORIZONTAL));
pnlSouth.add(new RunCommandBar(work, Bar.HORIZONTAL));
pnlSouth.add(work.lbPosition);
setLayout(new BorderLayout());
add("Center", pnlCenter);
add("South", pnlSouth);
add("West", pnlDraw);
}
public String[][] getParameterInfo()
{
String pinfo[][] =
{
{"draw", "String", "directory where exist draw files"},
{"image", "String", "directory where exist image files(*.gif, *.jpg ..)"},
{"port", "int", "port number for writing draw files"}
};
return pinfo;
}
public String getAppletInfo()
{
return new String("Web Draw 1.0 Author:Kang, Daewoong");
}
}
/**
* A container class that can scroll area with scrollbar
*
* @version 1.0 03/24/96
* @author Kang, Dae Woong (gothic@star.elim.net)
*/
class ScrollPanel extends Panel
{
/**
* The target panel that will be scrolled
*/
WorkPanel target;
/**
* Scrollbars that will be displayed in south, east.
*/
Scrollbar scbVertical, scbHorizontal;
/**
* Constructs a ScrollPanel with a WorkPanel
* @param target that will be scrolled
*/
public ScrollPanel(WorkPanel target)
{
this.target = target;
setLayout(new BorderLayout());
add("Center", target);
add("South", scbHorizontal = new Scrollbar(Scrollbar.HORIZONTAL,0, 100, 0, 200));
add("East", scbVertical = new Scrollbar(Scrollbar.VERTICAL));
}
/**
* set Scrollbar values
*/
public void layout()
{
Dimension dm;
super.layout();
dm = target.size();
if (dm.height < target.dimension.height)
{
scbVertical.enable();
scbVertical.setValues(scbVertical.getValue(), dm.height, 0, target.dimension.height);
}
else
{
scbVertical.disable();
}
if (dm.width < target.dimension.width)
{
scbHorizontal.enable();
scbHorizontal.setValues(scbHorizontal.getValue(), dm.width, 0, target.dimension.width);
}
else
{
scbHorizontal.disable();
}
}
/**
* handle Scrollbar event
*/
public boolean handleEvent(Event e)
{
if (e.target == scbVertical || e.target == scbHorizontal)
{
Scrollbar scr = (Scrollbar) e.target;
int val = 0;
int tmp;
boolean isDefault = false;
switch (e.id)
{
case e.SCROLL_ABSOLUTE:
val = ((Integer)e.arg).intValue();
break;
case e.SCROLL_LINE_DOWN:
tmp = scr.getValue() + scr.getVisible()/10;
scr.setValue((tmp + scr.getVisible() > scr.getMaximum()) ? scr.getMaximum() - scr.getVisible() : tmp);
val = scr.getValue();
break;
case e.SCROLL_LINE_UP:
scr.setValue(scr.getValue() - scr.getVisible()/10);
val = scr.getValue();
break;
case e.SCROLL_PAGE_DOWN:
tmp = scr.getValue() + scr.getVisible();
scr.setValue((tmp + scr.getVisible() > scr.getMaximum()) ? scr.getMaximum() - scr.getVisible() : tmp);
val = scr.getValue();
break;
case e.SCROLL_PAGE_UP:
scr.setValue(scr.getValue() - scr.getVisible());
val = scr.getValue();
break;
default:
isDefault = true;
}
if (!isDefault)
{
if (e.target == scbVertical)
target.ptScroll.y = val;
else
target.ptScroll.x = val;
target.repaint();
return true;
}
}
return super.handleEvent(e);
}
}
/**
* A Bar class that set drawing' method
*
* @version 1.0 03/24/96
* @author Kang, Dae Woong (gothic@star.elim.net)
*/
class DrawMethodBar extends ChoiceBar
{
/**
* drawing place
*/
WorkPanel target;
/**
* it will be displayed in status line when mouse move over thr button
*/
String[] help =
{
"Line drawing", "Rectangle drawing", "Rounded Rectangle drawing",
"Polygon drawing", "Oval drawing", "Pie drawing", "Arc drawing",
"Text writng", "Picture reference", "Draw Reference", "Select Drawings",
"Add URL"
};
/**
* Constructs a DrawMethodBar with a WorkPanel
* @param target : drawing place
* @param direction : bars direction (VERTICAL, HORIZONTAL)
* @see Bar
*/
public DrawMethodBar(WorkPanel target, int direction)
{
super(Draw.METHOD_COUNT, direction);
this.target = target;
setHelp(help);
select(0);
}
/**
* implement's abstract method in Bar
* @param g : graphics
* @param i : button index in Bar
* @param rc : drawing area
* @see Bar
*/
void drawCell(Graphics g, int i, Rectangle rc)
{
Polygon shapePolygon, shapeSelect;
g.setColor(Color.blue);
switch (i)
{
case Draw.LINE:
g.drawLine(rc.x, rc.y, rc.x + rc.width, rc.y + rc.height);
break;
case Draw.RECT:
g.drawRect(rc.x, rc.y, rc.width, rc.height);
break;
case Draw.ROUND_RECT:
g.drawRoundRect(rc.x, rc.y, rc.width, rc.height, 10, 10);
break;
case Draw.POLYGON:
shapePolygon = new Polygon();
shapePolygon.addPoint(rc.x, rc.y + rc.height);
shapePolygon.addPoint(rc.x + rc.width/2, rc.y);
shapePolygon.addPoint(rc.x + rc.width, rc.y + rc.height);
g.drawPolygon(shapePolygon);
break;
case Draw.OVAL:
g.drawOval(rc.x, rc.y, rc.width, rc.height);
break;
case Draw.PIE:
g.drawArc(rc.x, rc.y, rc.width, rc.height, 0, 270);
g.drawLine(rc.x + rc.width/2, rc.y + rc.height/2, rc.x + rc.width/2, rc.y + rc.height);
g.drawLine(rc.x + rc.width/2, rc.y + rc.height/2, rc.x + rc.width, rc.y + rc.height/2);
break;
case Draw.ARC:
g.drawArc(rc.x, rc.y, rc.width, rc.height, 0, 270);
break;
case Draw.STRING:
g.drawString("A", rc.x + 3, rc.y + rc.height);
break;
case Draw.IMAGE:
g.fillOval(rc.x, rc.y, 5, 6);
g.fillOval(rc.x + 4, rc.y + 2, 5, 5);
g.setColor(Color.green);
g.fillOval(rc.x + 4, rc.y + 7, 4, 3);
g.setColor(Color.yellow);
g.fillRect(rc.x + 7, rc.y + 6, 4, 4);
g.setColor(Color.green);
g.fillOval(rc.x + 6, rc.y + 4, 3, 3);
g.setColor(Color.red);
g.fillOval(rc.x + 8, rc.y + 8, 3, 3);
break;
case Draw.DRAW:
g.fillRect(rc.x, rc.y, rc.width*2/3, rc.height*2/3);
g.setColor(Color.green);
g.fillOval(rc.x + rc.width/3, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
break;
case Draw.SELECT:
shapeSelect = new Polygon();
shapeSelect.addPoint(rc.x + rc.width/2, rc.y);
shapeSelect.addPoint(rc.x, rc.y + rc.height/3);
shapeSelect.addPoint(rc.x + rc.width/3, rc.y + rc.height/3);
shapeSelect.addPoint(rc.x + rc.width/3, rc.y + rc.height);
shapeSelect.addPoint(rc.x + rc.width/3*2, rc.y + rc.height);
shapeSelect.addPoint(rc.x + rc.width/3*2, rc.y + rc.height/3);
shapeSelect.addPoint(rc.x + rc.width, rc.y + rc.height/3);
shapeSelect.addPoint(rc.x + rc.width/2, rc.y);
g.drawPolygon(shapeSelect);
break;
case Draw.ANCHOR:
g.drawLine(rc.x, rc.y + rc.height/3, rc.x + rc.width/2, rc.y);
g.drawLine(rc.x + rc.width/2, rc.y, rc.x + rc.width, rc.y + rc.height/3);
g.drawLine(rc.x + rc.width/2, rc.y, rc.x + rc.width/2, rc.y + rc.height*2/3);
g.drawLine(rc.x + rc.width/2 - 2, rc.y + rc.height/2, rc.x + rc.width/2 + 2, rc.y + rc.height/2);
g.drawOval(rc.x + rc.width/3, rc.y + rc.height*2/3, rc.width/3, rc.height/3);
break;
}
}
/**
* implement's abstract method in Bar
* set target's content variable
* @param i : pressed button index in Bar
* @see Bar
*/
void select(int i)
{
target.setMethod(i);
}
}
/**
* A Bar class that set drawing' content (fill, unfill)
*
* @version 1.0 03/24/96
* @author Kang, Dae Woong (gothic@star.elim.net)
*/
class DrawContentBar extends ChoiceBar
{
/**
* drawing place
*/
WorkPanel target;
/**
* it will be displayed in status line when mouse move over thr button
*/
String[] help = {"Fill drawing's contents", "Unfill drawing's contents"};
/**
* Constructs a DrawContentBar with a WorkPanel
* @param target : drawing place
* @param direction : bars direction (VERTICAL, HORIZONTAL)
* @see Bar
*/
public DrawContentBar(WorkPanel target, int direction)
{
super(Draw.CONTENT_COUNT, direction);
this.target = target;
setHelp(help);
select(0);
}
/**
* implement's abstract method in Bar
* @param g : graphics
* @param i : button index in Bar
* @param rc : drawing area
* @see Bar
*/
void drawCell(Graphics g, int i, Rectangle rc)
{
g.setColor(Color.black);
switch (i)
{
case Draw.CONTENT_FILL:
g.fillRect(rc.x, rc.y, rc.width - 1, rc.height - 1);
break;
case Draw.CONTENT_UNFILL:
g.drawRect(rc.x, rc.y, rc.width - 1, rc.height - 1);
break;
}
}
/**
* implement's abstract method in Bar
* set target's content variable
* @param i : pressed button index in Bar
* @see Bar
*/
void select(int i)
{
target.setContent(i);
}
}
/**
* A Bar class that set drawing' shape
*
* @version 1.0 03/24/96
* @author Kang, Dae Woong (gothic@star.elim.net)
*/
class DrawShapeBar extends ChoiceBar
{
/**
* drawing place
*/
WorkPanel target;
/**
* it will be displayed in status line when mouse move over thr button
*/
String[] help = {"Draw plain shape drawing", "Draw up shape drawing", "Draw down shape drawing"};
/**
* Constructs a DrawShapeBar with a WorkPanel
* @param target : drawing place
* @param direction : bars direction (VERTICAL, HORIZONTAL)
* @see Bar
*/
public DrawShapeBar(WorkPanel target, int direction)
{
super(Draw.SHAPE_COUNT, direction);
this.target = target;
setHelp(help);
select(0);
}
/**
* implement's abstract method in Bar
* @param g : graphics
* @param i : button index in Bar
* @param rc : drawing area
* @see Bar
*/
void drawCell(Graphics g, int i, Rectangle rc)
{
switch (i)
{
case Draw.SHAPE_PLAIN:
g.setColor(Color.black);
g.drawRect(rc.x, rc.y, rc.width, rc.height);
break;
case Draw.SHAPE_UP:
g.setColor(getBackground());
g.draw3DRect(rc.x, rc.y, rc.width, rc.height, true);
break;
case Draw.SHAPE_DOWN:
g.setColor(getBackground());
g.draw3DRect(rc.x, rc.y, rc.width, rc.height, false);
break;
}
}
/**
* implement's abstract method in Bar
* set target's content variable
* @param i : pressed button index in Bar
* @see Bar
*/
void select(int i)
{
target.setShape(i);
}
}
/**
* A Bar class that set drawing' Color
*
* @version 1.0 03/24/96
* @author Kang, Dae Woong (gothic@star.elim.net)
*/
class ColorBar extends ChoiceBar
{
/**
* drawing place
*/
WorkPanel target;
/**
* Colors that will be displayed
*/
final static Color[] baseColors =
{
Color.black, Color.darkGray, Color.gray, Color.lightGray, Color.white,
Color.red, Color.magenta, Color.pink,
Color.yellow, Color.orange,
Color.blue, Color.cyan,
Color.green
};
/**
* Constructs a DrawContentBar with a WorkPanel
* @param target : drawing place
* @param direction : bars direction (VERTICAL, HORIZONTAL)
* @see Bar
*/
public ColorBar(WorkPanel target, int direction)
{
super(baseColors.length, direction);
this.target = target;
select(0);
}
/**
* implement's abstract method in Bar
* @param g : graphics
* @param i : button index in Bar
* @param rc : drawing area
* @see Bar
*/
void drawCell(Graphics g, int i, Rectangle rc)
{
g.setColor(baseColors[i]);
g.fillRect(rc.x, rc.y, rc.width, rc.height);
}
/**
* implement's abstract method in Bar
* set target's color variable
* @param i : pressed button index in Bar
* @see Bar
*/
void select(int i)
{
target.setColor(baseColors[i]);
}
}
/**
* A Choice class that set drawing' FontName
*
* @version 1.0 03/24/96
* @author Kang, Dae Woong (gothic@star.elim.net)
*/
class ChoiceFont extends Choice
{
/**
* drawing place
*/
WorkPanel target;
/**
* Constructs a DrawContentBar with a WorkPanel
* @param target : drawing place
*/
public ChoiceFont(WorkPanel target)
{
this.target = target;
String[] fonts = getToolkit().getFontList();
for (int i = 0; i < fonts.length - 1; i++) // font symbol not correctly work in graphics
addItem(fonts[i]);
target.setFontName(fonts[0]);
}
/*
* set Drawing's font name When user select the font
*/
public boolean action(Event e, Object what)
{
target.setFontName((String)e.arg);
return false;
}
}
/**
* A Bar class that set drawing' FontStyle(PLAIN, BOLD, ITALIC)
*
* @version 1.0 03/24/96
* @author Kang, Dae Woong (gothic@star.elim.net)
*/
class FontStyleBar extends ChoiceBar
{
/**
* drawing place
*/
WorkPanel target;
/**
* font style array
*/
final static int[] fontStyles = { Font.PLAIN, Font.BOLD, Font.ITALIC };
/**
* it will be displayed in bar
*/
final static String[] fontTexts = { "P", "B", "I"};
/**
* it will be displayed in status line when mouse move over thr button
*/
String[] help = {"Plain font shape", "Bold font shape", "Italic font shape"};
/**
* Constructs a FontStyleBar with a WorkPanel
* @param target : drawing place
* @param direction : bars direction (VERTICAL, HORIZONTAL)
* @see Bar
*/
public FontStyleBar(WorkPanel target, int direction)
{
super(fontStyles.length, direction);
this.target = target;
setHelp(help);
select(0);
}
/**
* implement's abstract method in Bar
* @param g : graphics
* @param i : button index in Bar
* @param rc : drawing area
* @see Bar
*/
void drawCell(Graphics g, int i, Rectangle rc)
{
if (i >= fontTexts.length || i < 0)
return;
Font font = new Font("TimesRoman", fontStyles[i], rc.height);
g.setFont(font);
g.setColor(Color.blue);
g.drawString(fontTexts[i], rc.x, rc.y + rc.height);
}
/**
* implement's abstract method in Bar
* set target's font style variable
* @param i : pressed button index in Bar
* @see Bar
*/
void select(int i)
{
target.setFontStyle(fontStyles[i]);
}
}
/**
* A Bar class that run command
*
* @version 1.0 03/24/96
* @author Kang, Dae Woong (gothic@star.elim.net)
*/
class RunCommandBar extends CommandBar
{
/**
* drawing place
*/
WorkPanel target;
/**
* Command IDs
*/
final static int SET_SIZE = 0;
final static int GRID = 1;
final static int DELETE = 2;
final static int COPY = 3;
final static int READ_DRAW = 4;
final static int WRITE_DRAW = 5;
final static int ALIGN_TOP = 6;
final static int ALIGN_BOTTOM = 7;
final static int ALIGN_LEFT = 8;
final static int ALIGN_RIGHT = 9;
final static int BRING_FRONT = 10;
final static int BRING_BACK = 11;
final static int REMOVE_ALL = 12;
final static int ABOUT_DRAW = 13;
final static int RUN_COUNT = 14;
/**
* it will be displayed in status line when mouse move over thr button
*/
String[] help =
{
"Set Draw size", "Toggle Grid", "Delete selected drawing(s)", "Copy selected drawing(s)",
"Read Draw", "Save Draw",
"Align selected drawings to Top",
"Align selected drawings to Bottom",
"Align selected drawings to Left",
"Align selected drawings to Right",
"Bring selected drawing to Front",
"Bring selected drawing to Back",
"Remove All (Clear Screen)",
"About Draw?"
};
/**
* Constructs a RunCommandBar with a WorkPanel
* @param target : drawing place
* @param direction : bars direction (VERTICAL, HORIZONTAL)
* @see Bar
*/
public RunCommandBar(WorkPanel target, int direction)
{
super(RUN_COUNT, direction);
this.target = target;
setHelp(help);
}
/**
* implement's abstract method in Bar
* @param g : graphics
* @param i : button index in Bar
* @param rc : drawing area
* @see Bar
*/
void drawCell(Graphics g, int i, Rectangle rc)
{
if (i > RUN_COUNT || i < 0)
return;
switch (i)
{
case SET_SIZE:
g.setColor(Color.blue);
g.drawLine(rc.x, rc.y, rc.x + rc.width, rc.y);
g.drawLine(rc.x, rc.y + rc.height - 2, rc.x + rc.width, rc.y + rc.height - 2);
g.drawLine(rc.x, rc.y, rc.x, rc.y + rc.height);
g.drawLine(rc.x + rc.width - 2, rc.y, rc.x + rc.width - 2, rc.y + rc.height);
break;
case GRID:
int wid = rc.width/4;
int hgt = rc.height/4;
g.setColor(Color.blue);
for (int k = 0; k < 5; k++)
{
g.drawLine(rc.x, rc.y + hgt*k, rc.x + rc.width, rc.y + hgt*k);
g.drawLine(rc.x + wid*k, rc.y, rc.x + wid*k, rc.y + rc.height);
}
break;
case DELETE:
g.setFont(new Font("TimesRoman", Font.BOLD, rc.height + 4));
g.setColor(Color.blue);
g.drawString("-", rc.x + 4, rc.y + rc.height);
break;
case COPY:
g.setFont(new Font("TimesRoman", Font.BOLD, rc.height + 4));
g.setColor(Color.blue);
g.drawString("+", rc.x + 2, rc.y + rc.height);
break;
case READ_DRAW:
g.setColor(Color.green);
g.fillRect(rc.x, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
g.setColor(Color.black);
g.drawRect(rc.x, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
g.setColor(Color.red);
g.drawLine(rc.x + rc.width, rc.y, rc.x + rc.width*2/3, rc.y);
g.drawLine(rc.x + rc.width, rc.y, rc.x + rc.width, rc.y + rc.height/3);
g.drawLine(rc.x + rc.width, rc.y, rc.x + rc.width/3, rc.y + rc.height*2/3);
break;
case WRITE_DRAW:
g.setColor(Color.green);
g.fillRect(rc.x, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
g.setColor(Color.black);
g.drawRect(rc.x, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
g.setColor(Color.red);
g.drawLine(rc.x + rc.width/3, rc.y + rc.height*2/3, rc.x + rc.width/3, rc.y + rc.height/3);
g.drawLine(rc.x + rc.width/3, rc.y + rc.height*2/3, rc.x + rc.width*2/3, rc.y + rc.height*2/3);
g.drawLine(rc.x + rc.width/3, rc.y + rc.height*2/3, rc.x + rc.width, rc.y);
break;
case ALIGN_TOP:
g.setColor(Color.blue);
g.drawLine(rc.x + rc.width/2, rc.y, rc.x, rc.y + rc.height/2);
g.drawLine(rc.x + rc.width/2, rc.y, rc.x + rc.width/2, rc.y + rc.height);
g.drawLine(rc.x + rc.width/2, rc.y, rc.x + rc.width, rc.y + rc.height/2);
break;
case ALIGN_BOTTOM:
g.setColor(Color.blue);
g.drawLine(rc.x + rc.width/2, rc.y + rc.height, rc.x, rc.y + rc.height/2);
g.drawLine(rc.x + rc.width/2, rc.y + rc.height, rc.x + rc.width/2, rc.y);
g.drawLine(rc.x + rc.width/2, rc.y + rc.height, rc.x + rc.width, rc.y + rc.height/2);
break;
case ALIGN_LEFT:
g.setColor(Color.blue);
g.drawLine(rc.x, rc.y + rc.height/2, rc.x + rc.width/2, rc.y);
g.drawLine(rc.x, rc.y + rc.height/2, rc.x + rc.width, rc.y + rc.height/2);
g.drawLine(rc.x, rc.y + rc.height/2, rc.x + rc.width/2, rc.y + rc.height);
break;
case ALIGN_RIGHT:
g.setColor(Color.blue);
g.drawLine(rc.x + rc.width, rc.y + rc.height/2, rc.x + rc.width/2, rc.y);
g.drawLine(rc.x + rc.width, rc.y + rc.height/2, rc.x, rc.y + rc.height/2);
g.drawLine(rc.x + rc.width, rc.y + rc.height/2, rc.x + rc.width/2, rc.y + rc.width);
break;
case BRING_FRONT:
g.setColor(Color.black);
g.drawRect(rc.x, rc.y, rc.width*2/3, rc.height*2/3);
g.setColor(Color.blue);
g.fillRect(rc.x + rc.width/3, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
g.setColor(Color.black);
g.drawRect(rc.x + rc.width/3, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
break;
case BRING_BACK:
g.setColor(Color.blue);
g.fillRect(rc.x + rc.width/3, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
g.setColor(Color.black);
g.drawRect(rc.x + rc.width/3, rc.y + rc.height/3, rc.width*2/3, rc.height*2/3);
g.setColor(Color.lightGray);
g.fillRect(rc.x, rc.y, rc.width*2/3, rc.height*2/3);
g.setColor(Color.black);
g.drawRect(rc.x, rc.y, rc.width*2/3, rc.height*2/3);
break;
case REMOVE_ALL:
g.setColor(Color.red);
g.drawLine(rc.x, rc.y, rc.x + rc.width, rc.y + rc.height);
g.drawLine(rc.x, rc.y + rc.height, rc.x + rc.width, rc.y);
break;
case ABOUT_DRAW:
g.setColor(Color.blue);
g.drawArc(rc.x + rc.width/4, rc.y, rc.width/2, rc.height/2, 270, 270);
g.drawLine(rc.x + rc.width/2, rc.y + rc.height/2, rc.x + rc.width/2, rc.y + rc.height*3/4);
g.fillOval(rc.x + rc.width/2 - 1, rc.y + rc.height - 1, 2, 2);
break;
}
}
/**
* implement's abstract method in Bar
* set target's content variable
* @param i : pressed button index in Bar
* @see Bar
*/
void select(int i)
{
target.runCommand(i);
}
}
//////////////////////////////////////////////////////////////////////////
// Bars
//////////////////////////////////////////////////////////////////////////
/**
* root class of bar classes
*
* @version 1.0 03/24/96
* @author Kang, Dae Woong (gothic@star.elim.net)
*/
abstract class Bar extends Canvas
{
/**
* button's drawing area size
*/
final int sizeCell = 20;
/**
* bar's drawing area size
*/
final int sizeBar = 30;
/**
* gab between bar border and button border
*/
int gabBarCell = (sizeBar - sizeCell) / 2;
/**
* help string array pointer
*/
private String[] help = null; // showState
/**
* current displaying help pointer
*/
private static String curHelp = null;
/**
* bar's dispaying stylw
*/
int direction;
static final int HORIZONTAL = 1;
static final int VERTICAL = 2;
/**
* button count in bar
*/
private int count;
/**
* getCellRect help variable
*/
private int factorX, factorY;
/**
* draw button method called when paint button
* @param g : graphics
* @param i : button index in Bar
* @param rc : drawing area
*/
abstract void drawCell(Graphics g, int i, Rectangle rc);
/**
* called when mouse downd the button
* @param g : graphics
* @param i : button index in Bar
* @param rc : drawing area
*/
abstract public void pressButton(int i);
/**
* return the button state
* @param i : button index in Bar
* @return : true if button is up state
*/
abstract public boolean isButtonUp(int i);
/**
* called when button down
* set target's content variable
* @param i : pressed button index in Bar
*/
abstract void select(int i);
/**
* Constructs a Bar with button count and direction
* @param count : button's count
* @param direction : bars direction (VERTICAL, HORIZONTAL)
*/
public Bar(int count, int direction)
{
this.count = count;
setBackground(Color.lightGray);
this.direction = direction;
if (direction == HORIZONTAL)
{
factorX = 1;
factorY = 0;
resize(sizeCell*count + gabBarCell*2, sizeBar);
}
else
{
factorX = 0;
factorY = 1;
resize(sizeBar, sizeCell*count + gabBarCell*2);
}
}
/**
* set help string
* @param help : help String
*/
public void setHelp(String[] help)
{
if (help.length >= count)
this.help = help;
}
public void paint(Graphics g)
{
System.out.println("bar print");
int i;
Dimension dm = size();
// draw border
g.setColor(Color.lightGray);
g.draw3DRect(0, 0, dm.width - 1, dm.height - 1, true);
// draw each cell
for (i = 0; i < count; i++)
{
if (isButtonUp(i)) // child's method
drawUpCell(g, i);
else
drawDownCell(g, i);
}
g.dispose();
}
/**
* draw up shape button
* it call child;s function drawCell
* @param g : graphics
* @param i : button index in Bar
*/
void drawUpCell(Graphics g, int i)
{
Rectangle rc = getCellRect(i);
g.setColor(Color.lightGray);
g.draw3DRect(rc.x, rc.y, rc.width, rc.height, true);
drawCell(g, i, getCellInterior(i, true));
}
/**
* draw dwon shape button
* it call child;s function drawCell
* @param g : graphics
* @param i : button index in Bar
*/
void drawDownCell(Graphics g, int i)
{
Rectangle rc = getCellRect(i);
g.setColor(Color.lightGray);
g.draw3DRect(rc.x, rc.y, rc.width, rc.height, false);
drawCell(g, i, getCellInterior(i, false));
}
/**
* clear button
* @param g : graphics
* @param i : button index in Bar
*/
public void clearCell(Graphics g, int i)
{
Rectangle rc = getCellRect(i);
g.clearRect(rc.x, rc.y, rc.width, rc.height);
}
/**
* get area of a button
* @param i : button index in Bar
* @return button's area
*/
public Rectangle getCellRect(int i)
{
int startPos = i*sizeCell;
return new Rectangle(factorX*startPos + gabBarCell, factorY*startPos + gabBarCell,
sizeCell - 1, sizeCell - 1);
}
/**
* get drawing area of a button
* @param i : button index in Bar
* @param isUp : state of button
* @return button's area
*/
public Rectangle getCellInterior(int i, boolean isUp)
{
int startPos = i*sizeCell;
int factor = (isUp) ? 0 : 1;
return new Rectangle(factorX*startPos + gabBarCell + 4 + factor, factorY*startPos + gabBarCell + 4 + factor,
sizeCell - 8, sizeCell - 8);
}
/**
* get left top point of button's area
* @param i : button index in Bar
* @return left top point of button's area
*/
public Point getCellPos(int i)
{
int startPos;
startPos = i*sizeCell;
return new Point(factorX*startPos, factorY*startPos);
}
/**
* get button's index by position
* @param x: x position
* @param y: y position
* @return button's index
*/
protected int getButtonIndex(int x, int y)
{
if (direction == HORIZONTAL && x < gabBarCell + sizeCell*count &&
y > gabBarCell && y < sizeBar - gabBarCell)
{
return (x - gabBarCell) / sizeCell;
}
else if (direction == VERTICAL && y < gabBarCell + sizeCell*count &&
x > gabBarCell && x < sizeBar - gabBarCell)
{
return (y - gabBarCell) / sizeCell;
}
return -1;
}
public boolean mouseDown(Event e, int x, int y)
{
int i = getButtonIndex(x, y);
if (i != -1)
pressButton(i);
return true;
}
/*
* Show help in status line
*/
public boolean mouseMove(Event e, int x, int y)
{
if (help == null)
{
if (curHelp != null)
WorkPanel.applet.showStatus("");
return true;
}
int i = getButtonIndex(x, y);
if (i != -1)
{
if (curHelp != help[i])
WorkPanel.applet.showStatus(help[i]);
curHelp = help[i];
}
else
{
if (curHelp != null)
WorkPanel.applet.showStatus("");
curHelp = null;
}
return true;
}
}
/**
* A Bar class that select one button
*
* @version 1.0 03/24/96
* @author Kang, Dae Woong (gothic@star.elim.net)
*/
abstract class ChoiceBar extends Bar
{
/**
* selected item index
*/
int curSelected = 0;
/**
* Constructs a Bar with button count and direction
* @param count : button's count
* @param direction : bars direction (VERTICAL, HORIZONTAL)
*/
public ChoiceBar(int count, int direction)
{
super(count, direction);
}
/**
* implement's abstract method in Bar
* @param i : button index in Bar
* @see Bar
*/
public boolean isButtonUp(int i)
{
return i != curSelected;
}
/**
* implement's abstract method in Bar
* @param i : button index in Bar
* @see Bar
*/
public void pressButton(int i)
{
if (curSelected != i)
{
Graphics g = getGraphics();
clearCell(g, curSelected);
drawUpCell(g, curSelected);
curSelected = i;
clearCell(g, curSelected);
drawDownCell(g, curSelected);
g.dispose();
select(i); //call child's function
}
}
}
/**
* A Bar class that run command
* it act like button
*
* @version 1.0 03/24/96
* @author Kang, Dae Woong (gothic@star.elim.net)
*/
abstract class CommandBar extends Bar
{
/**
* selected item index
*/
int curSelected = -1;
/**
* Constructs a Bar with button count and direction
* @param count : button's count
* @param direction : bars direction (VERTICAL, HORIZONTAL)
*/
public CommandBar(int count, int direction)
{
super(count, direction);
}
/**
* implement's abstract method in Bar
* @param i : button index in Bar
* @see Bar
*/
public boolean isButtonUp(int i)
{
return true;
}
/**
* implement's abstract method in Bar
* @param i : button index in Bar
* @see Bar
*/
public void pressButton(int i)
{
Graphics g = getGraphics();
curSelected = i;
clearCell(g, i);
drawDownCell(g, i);
g.dispose();
}
/**
* run command
*/
public boolean mouseUp(Event e, int x, int y)
{
int i = getButtonIndex(x, y);
if (i == curSelected && i != -1)
{
select(i);
}
if (i != -1)
{
Graphics g = getGraphics();
curSelected = i;
clearCell(g, i);
drawUpCell(g, i);
g.dispose();
}
curSelected = -1;
return true;
}
/**
* cancel command if point is out of button
*/
public boolean mouseDrag(Event e, int x, int y)
{
int i = getButtonIndex(x, y);
if (curSelected != -1 && i != curSelected)
{
Graphics g = getGraphics();
clearCell(g, curSelected);
drawUpCell(g, curSelected);
g.dispose();
curSelected = -1;
}
return true;
}
}