home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Symantec Visual Cafe for Java 2.5
/
symantec-visual-cafe-2.5-database-dev-edition.iso
/
Extras
/
ODesign
/
SetupPSE.exe
/
data.z
/
BitMaps.java
< prev
next >
Encoding:
Amiga
Atari
Commodore
DOS
FM Towns/JPY
Macintosh
Macintosh JP
Macintosh to JP
NeXTSTEP
RISC OS/Acorn
Shift JIS
UTF-8
Wrap
Java Source
|
1997-05-08
|
35.3 KB
|
1,363 lines
package COM.odi.demo.OSDraw;
/**
* <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
*/
import COM.odi.*;
import java.applet.Applet;
import java.awt.*;
/*
* The only purpose of BitMaps is to start applet and create MainFrame instance
*/
public class BitMaps extends Applet {
// Create and show the main window (MainFrame)
public void init() {
MainFrame frame = new MainFrame(this);
frame.show();
}
}
/*
* Subclass of java.awt.Frame
* User communicates to database through this window interface
* Handles menus and directs dialog boxes
* Handles all relevent events
*/
class MainFrame extends Frame {
private Applet parent;
private Database db; // Current database
private Transaction trans; // Current transaction
private Thread initialized; // Indicates if ObjectStore has been initialized
// figure variable to indicate present figure and its constants
private int figure;
private CheckboxMenuGroup figureGroup;
private static final int LINE = 1;
private static final int RECTANGLE = 2;
private static final int OVAL = 3;
private static final int POLYGON = 4;
private static final int TEXT = 5;
private static final int IMAGE = 6;
private boolean dragging; // Indicates if mouse is being dragged
// Coordinates used when the mouse is dragging
private int startX;
private int startY;
private int presentX;
private int presentY;
// Used to record present polygon while it is drawn
private Polygon poly;
// Settings for present text and its menu groups
private String presentString;
private TextSettings textSettings;
private CheckboxMenuGroup textNameGroup;
private CheckboxMenuGroup textStyleGroup;
private CheckboxMenuGroup textSizeGroup;
// Settings for present image and its meny groups
private ImageSettings imageSettings;
private CheckboxMenuGroup XDirectionGroup;
private CheckboxMenuGroup YDirectionGroup;
private CheckboxMenuGroup XFactorGroup;
private CheckboxMenuGroup YFactorGroup;
// Fill Status and its menu group
private boolean fillStatus;
private CheckboxMenuGroup fillGroup;
// Present colors
private Color foreColor;
private Color backColor;
// Menu group for colors and an array of color constants
private CheckboxMenuGroup colorGroup;
private static final Color colors[] = {
Color.white,
Color.lightGray,
Color.gray,
Color.darkGray,
Color.black,
Color.red,
Color.pink,
Color.orange,
Color.yellow,
Color.green,
Color.magenta,
Color.cyan,
Color.blue
};
private BitMapObject root; // Database root
// Variable process used to lock app in a process, also its constants
private int process;
private static final int NO_PROCESS = 0;
private static final int DELIVER_MESSAGE = 1;
private static final int OPEN_DATABASE = 2;
// Menu labels which lead to event-handling methods
private static final String newLabel = "New Database...";
private static final String openLabel = "Open Database...";
private static final String saveLabel = "Save Database";
private static final String closeLabel = "Close Database...";
private static final String repaintLabel = "Refresh Display";
private static final String destroyLastLabel = "Destroy Last Object";
private static final String clearLabel = "Clear Database";
private static final String setImageLabel = "Set Image...";
private static final String currentImageLabel = "Current Image...";
private static final String exitLabel = "Exit";
private AppletIOSupport AIOS; // Control of I/O
// Constructor to set default values
public MainFrame(Applet parent) {
super("Empty");
this.parent = parent;
db = null;
trans = null;
initialized = null;
figure = LINE;
figureGroup = new CheckboxMenuGroup();
dragging = false;
process = NO_PROCESS;
poly = new Polygon();
textSettings = new TextSettings();
textNameGroup = new CheckboxMenuGroup();
textStyleGroup = new CheckboxMenuGroup();
textSizeGroup = new CheckboxMenuGroup();
imageSettings = new ImageSettings();
XDirectionGroup = new CheckboxMenuGroup();
YDirectionGroup = new CheckboxMenuGroup();
XFactorGroup = new CheckboxMenuGroup();
YFactorGroup = new CheckboxMenuGroup();
fillStatus = false;
fillGroup = new CheckboxMenuGroup();
foreColor = Color.black;
setForeground(foreColor);
colorGroup = new CheckboxMenuGroup();
backColor = Color.cyan;
setBackground(backColor);
setSize(600, 400);
setLocation(5, 150);
Font textFont = new Font("Arial", Font.PLAIN, 14);
setFont(textFont);
AIOS = new AppletIOSupport();
AIOS.registerFontMetrics(getFontMetrics(textFont));
AIOS.initializeMessageBox(this, 25, 25);
AIOS.initializeMessageBox(this, 25, 25, true);
buildMenuBar();
}
// Long method to build the menu bar and its menus and nested menus
private void buildMenuBar() {
MenuBar menuBar = new MenuBar();
setMenuBar(menuBar);
Menu menu, sub;
CheckboxMenuItem checkItem;
menu = new Menu("File");
menuBar.add(menu);
menu.add(new MenuItem(newLabel));
menu.add(new MenuItem(openLabel));
menu.add(new MenuItem(saveLabel));
menu.add(new MenuItem(closeLabel));
menu.addSeparator();
menu.add(new MenuItem(repaintLabel));
menu.addSeparator();
menu.add(new MenuItem(exitLabel));
menu = new Menu("Edit");
menuBar.add(menu);
menu.add(new MenuItem(destroyLastLabel));
menu.add(new MenuItem(clearLabel));
menu = new Menu("Settings");
menuBar.add(menu);
sub = new Menu("Shape");
checkItem = new CheckboxMenuItem("Line");
checkItem.setState(true);
sub.add(checkItem);
figureGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Rectangle");
sub.add(checkItem);
figureGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Oval");
sub.add(checkItem);
figureGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Polygon");
sub.add(checkItem);
figureGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Text");
sub.add(checkItem);
figureGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Image");
sub.add(checkItem);
figureGroup.add(checkItem);
menu.add(sub);
sub = new Menu("Color");
checkItem = new CheckboxMenuItem("White");
sub.add(checkItem);
colorGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Light Gray");
sub.add(checkItem);
colorGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Gray");
sub.add(checkItem);
colorGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Dark Gray");
sub.add(checkItem);
colorGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Black");
checkItem.setState(true);
sub.add(checkItem);
colorGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Red");
sub.add(checkItem);
colorGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Pink");
sub.add(checkItem);
colorGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Orange");
sub.add(checkItem);
colorGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Yellow");
sub.add(checkItem);
colorGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Green");
sub.add(checkItem);
colorGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Magenta");
sub.add(checkItem);
colorGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Cyan");
sub.add(checkItem);
colorGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Blue");
sub.add(checkItem);
colorGroup.add(checkItem);
menu.add(sub);
menu.addSeparator();
sub = new Menu("Fill Status");
checkItem = new CheckboxMenuItem("On");
sub.add(checkItem);
fillGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Off");
checkItem.setState(true);
sub.add(checkItem);
fillGroup.add(checkItem);
menu.add(sub);
menu = new Menu("Text");
menuBar.add(menu);
sub = new Menu("Font Name");
for (int i = 0; i < textSettings.fontNames.length; i++) {
checkItem = new CheckboxMenuItem(textSettings.fontNames[i]);
sub.add(checkItem);
textNameGroup.add(checkItem);
}
textNameGroup.select(0);
menu.add(sub);
sub = new Menu("Font Style");
checkItem = new CheckboxMenuItem("Regular");
checkItem.setState(true);
sub.add(checkItem);
textStyleGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Bold");
sub.add(checkItem);
textStyleGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Italic");
sub.add(checkItem);
textStyleGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Bold Italic");
sub.add(checkItem);
textStyleGroup.add(checkItem);
menu.add(sub);
sub = new Menu("Font Size");
for (int i = 0; i < 10; i++) {
checkItem = new CheckboxMenuItem(Integer.toString(getSizeFromIndex(i)));
sub.add(checkItem);
textSizeGroup.add(checkItem);
}
textSizeGroup.select(3); // Selects font 16
menu.add(sub);
menu = new Menu("Image");
menuBar.add(menu);
menu.add(new MenuItem(setImageLabel));
menu.addSeparator();
menu.add(new MenuItem(currentImageLabel));
menu.addSeparator();
sub = new Menu("X Direction");
checkItem = new CheckboxMenuItem("X enlarged");
sub.add(checkItem);
XDirectionGroup.add(checkItem);
checkItem = new CheckboxMenuItem("X reduced");
checkItem.setState(true);
sub.add(checkItem);
XDirectionGroup.add(checkItem);
menu.add(sub);
sub = new Menu("Y Direction");
checkItem = new CheckboxMenuItem("Y enlarged");
sub.add(checkItem);
YDirectionGroup.add(checkItem);
checkItem = new CheckboxMenuItem("Y reduced");
checkItem.setState(true);
sub.add(checkItem);
YDirectionGroup.add(checkItem);
menu.add(sub);
sub = new Menu("X Factor");
for (int i = 1; i <= 10; i++) {
checkItem = new CheckboxMenuItem(Integer.toString(i));
sub.add(checkItem);
XFactorGroup.add(checkItem);
}
XFactorGroup.select(0);
menu.add(sub);
sub = new Menu("Y Factor");
for (int i = 1; i <= 10; i++) {
checkItem = new CheckboxMenuItem(Integer.toString(i));
sub.add(checkItem);
YFactorGroup.add(checkItem);
}
YFactorGroup.select(0);
menu.add(sub);
}
// Override java.awt.Component.show method to move the window
public void setVisible(boolean b) {
super.setVisible(b);
setLocation(5, 150);
}
// Override java.awt.Component.paint method to paint all database figures
public void paint(Graphics g) {
if (initialized != null) {
ObjectStore.initialize(initialized);
}
super.paint(g);
if (db == null) {
return;
}
BitMapObject obj = root;
while (obj != null) {
if (obj instanceof OSImageFile) {
((OSImageFile)obj).drawFigure(g, this);
}
else {
obj.drawFigure(g);
}
g.setColor(foreColor);
obj = obj.getNextObject();
}
if (dragging) {
g.setColor(foreColor);
if (figure == LINE) {
g.drawLine(startX, startY, presentX, presentY);
}
if (figure == RECTANGLE) {
Point topLeft = getTopLeft(startX, startY, presentX, presentY);
Point bottomRight = getBottomRight(startX, startY, presentX, presentY);
g.drawRect(topLeft.x, topLeft.y, bottomRight.x - topLeft.x,
bottomRight.y - topLeft.y);
}
if (figure == OVAL) {
Point topLeft = getTopLeft(startX, startY, presentX, presentY);
Point bottomRight = getBottomRight(startX, startY, presentX, presentY);
g.drawOval(topLeft.x, topLeft.y, bottomRight.x - topLeft.x,
bottomRight.y - topLeft.y);
}
if (figure == POLYGON) {
g.drawPolygon(poly);
}
if (figure == TEXT) {
g.setFont(textSettings.getFont());
g.drawString(presentString, startX, startY);
}
}
// Stop locking process in OPEN_DATABASE (if necessary)
if (process == OPEN_DATABASE) {
process = NO_PROCESS;
}
}
// Method to deal with user dragging the mouse
public boolean mouseDrag(Event event, int x, int y) {
if (db == null) {
return true;
}
// Make sure there is no process
if (process != NO_PROCESS) {
return true;
}
if (figure == LINE || figure == RECTANGLE || figure == OVAL) {
if (!dragging) {
dragging = true;
startX = x;
startY = y;
return true;
}
// Compute dimensions of largest rectangle
int largeX;
int largeY;
if (abs(presentX - startX) > abs(x - startX)) {
largeX = presentX;
}
else {
largeX = x;
}
if (abs(presentY - startY) > abs(y - startY)) {
largeY = presentY;
}
else {
largeY = y;
}
largeX += 10 * direction(largeX - startX);
largeY += 10 * direction(largeY - startY);
presentX = x;
presentY = y;
Point topLeft = getTopLeft(startX + 10 * direction(startX - largeX),
startY + 10 * direction(startY - largeY),
largeX, largeY);
Point bottomRight =
getBottomRight(startX + 10 * direction(startX - largeX),
startY + 10 * direction(startY - largeY),
largeX, largeY);
repaint(topLeft.x, topLeft.y, bottomRight.x - topLeft.x,
bottomRight.y - topLeft.y);
}
return true;
}
// Method to deal with user releasing mouse
public boolean mouseUp(Event event, int x, int y) {
if (db == null) {
return true;
}
// Make sure there is no process
if (process != NO_PROCESS) {
return true;
}
if (figure == POLYGON) {
dragging = true;
poly.addPoint(x, y);
Rectangle rect = getPolyRect(poly);
repaint(rect.x - 5, rect.y - 5, rect.width + 10, rect.height + 10);
return true;
}
if (figure == TEXT) {
dragging = true;
startX = x;
startY = y;
presentString = "";
return true;
}
if (figure == IMAGE && imageSettings.getName() != null) {
addFigure(new OSImageFile(imageSettings.getName(), x, y));
repaint();
return true;
}
if (dragging) {
dragging = false;
if (startX == x && startY == y) {
return true;
}
if (figure == LINE) {
addFigure(new OSLine(startX, startY, x, y));
}
else if (figure == RECTANGLE) {
Point topLeft = getTopLeft(startX, startY, x, y);
Point bottomRight = getBottomRight(startX, startY, x, y);
addFigure(new OSRectangle(topLeft.x, topLeft.y,
bottomRight.x, bottomRight.y));
}
else if (figure == OVAL) {
Point topLeft = getTopLeft(startX, startY, x, y);
Point bottomRight = getBottomRight(startX, startY, x, y);
addFigure(new OSOval(topLeft.x, topLeft.y, bottomRight.x - topLeft.x,
bottomRight.y - topLeft.y));
}
repaint();
}
return true;
}
// Method to handle keyboard event
public boolean keyDown(Event event, int key) {
// Make sure there is no process
if (process != NO_PROCESS) {
return true;
}
if (figure == TEXT && dragging) {
if ((char)key == '\n') {
dragging = false;
addFigure(new OSText(presentString, startX, startY));
}
else if (key == 8) {
// Backspace key
if (presentString.length() > 0) {
FontMetrics metrics = getFontMetrics(textSettings.getFont());
int textWidth = metrics.stringWidth(presentString);
presentString = presentString.substring(0, presentString.length() - 1);
repaint(startX - 5, startY - metrics.getHeight(),
textWidth + 10, metrics.getHeight() + 10);
}
return true;
} else if (key == 27) {
// Esc key was pressed
dragging = false;
}
else {
presentString += (char)key;
FontMetrics metrics = getFontMetrics(textSettings.getFont());
repaint(startX - 5, startY - metrics.getHeight(),
metrics.stringWidth(presentString) + 10,
metrics.getHeight() + 10);
return true;
}
repaint();
return true;
}
if (figure == POLYGON && dragging) {
if ((char)key == '\n') {
finishPolygon();
}
else if (key == 27) {
// Esc key was pressed
dragging = false;
poly = new Polygon();
repaint();
return true;
}
}
return true;
}
// Overloaded method addFigure to add each figure to the database
private void addFigure(OSLine line) {
if (db == null) {
return;
}
if (root == null) {
root = line;
db.setRoot("Root", root);
((OSLine)root).setColor(foreColor);
return;
}
BitMapObject node = root;
while (node.getNextObject() != null) {
node = node.getNextObject();
}
node.setNextObject(line);
line.setColor(foreColor);
}
private void addFigure(OSRectangle rec) {
if (db == null) {
return;
}
if (root == null) {
root = rec;
db.setRoot("Root", root);
((OSRectangle)root).setColor(foreColor);
((OSRectangle)root).setFillStatus(fillStatus);
return;
}
BitMapObject node = root;
while (node.getNextObject() != null) {
node = node.getNextObject();
}
node.setNextObject(rec);
rec.setColor(foreColor);
rec.setFillStatus(fillStatus);
}
public void addFigure(OSOval oval) {
if (db == null) {
return;
}
if (root == null) {
root = oval;
db.setRoot("Root", root);
((OSOval)root).setColor(foreColor);
((OSOval)root).setFillStatus(fillStatus);
return;
}
BitMapObject node = root;
while (node.getNextObject() != null) {
node = node.getNextObject();
}
node.setNextObject(oval);
oval.setColor(foreColor);
oval.setFillStatus(fillStatus);
}
private void addFigure(OSPolygon pol) {
if (db == null) {
return;
}
if (root == null) {
root = pol;
db.setRoot("Root", root);
((OSPolygon)root).setColor(foreColor);
((OSPolygon)root).setFillStatus(fillStatus);
return;
}
BitMapObject node = root;
while (node.getNextObject() != null) {
node = node.getNextObject();
}
node.setNextObject(pol);
pol.setColor(foreColor);
pol.setFillStatus(fillStatus);
}
private void addFigure(OSText text) {
if (db == null) {
return;
}
if (root == null) {
root = text;
db.setRoot("Root", root);
((OSText)root).setColor(foreColor);
((OSText)root).setFont(textSettings.getFont());
return;
}
BitMapObject node = root;
while (node.getNextObject() != null) {
node = node.getNextObject();
}
node.setNextObject(text);
text.setColor(foreColor);
text.setFont(textSettings.getFont());
}
private void addFigure(OSImageFile imageFile) {
if (db == null) {
return;
}
if (root == null) {
root = imageFile;
db.setRoot("Root", root);
((OSImageFile)root).setEnlargeX(imageSettings.getEnlargeX());
((OSImageFile)root).setEnlargeY(imageSettings.getEnlargeY());
((OSImageFile)root).setFactorX(imageSettings.getFactorX());
((OSImageFile)root).setFactorY(imageSettings.getFactorY());
return;
}
BitMapObject node = root;
while (node.getNextObject() != null) {
node = node.getNextObject();
}
node.setNextObject(imageFile);
imageFile.setEnlargeX(imageSettings.getEnlargeX());
imageFile.setEnlargeY(imageSettings.getEnlargeY());
imageFile.setFactorX(imageSettings.getFactorX());
imageFile.setFactorY(imageSettings.getFactorY());
}
// Methods to handle menu events
private void newDatabase() {
if (db != null) {
showMessageBox("Database already open.");
return;
}
if (initialized == null) {
ObjectStore.initialize(null, null);
initialized = Thread.currentThread();
}
FileDialog dlg = new FileDialog(this, "New Database", FileDialog.SAVE);
dlg.setFile("*.odb");
dlg.setVisible(true);
if (dlg.getFile() == null) {
// Cancel button was pressed
return;
}
String fname = dlg.getFile();
// Variable junk needed for Windows NT 4.0 beta
// For this release, extra junk is added to .odb file names
boolean junk = true;
if (fname.length() > 4) {
int size = fname.length();
String extension = "";
char buf[] = fname.toCharArray();
for (int i = 0; i < 4; i++) {
extension += buf[size - 4 + i];
}
extension = extension.toLowerCase();
if (extension.equals(".odb")) {
junk = false;
}
}
if (junk) {
fname = dlg.getFile().substring(0, dlg.getFile().length() - 4);
}
String dbname = dlg.getDirectory() + fname;
setTitle(dbname);
try {
db = Database.create(dbname, 0664);
} catch (AccessViolationException e) {
setTitle("Empty");
showMessageBox("File system access violation.");
return;
} catch (DatabaseAlreadyExistsException e) {
setTitle("Empty");
showMessageBox("Database already exists.");
return;
} catch (DatabaseException e) {
setTitle("Empty");
showMessageBox("Invalid database: " + dbname);
return;
}
// Create a database root and start transaction
if (db != null) {
trans = Transaction.begin(ObjectStore.UPDATE);
db.createRoot("Root", null);
trans.commit();
trans = Transaction.begin(ObjectStore.UPDATE);
root = null;
poly = new Polygon();
}
}
private void openDatabase() {
if (db != null) {
showMessageBox("Database already open.");
return;
}
if (initialized == null) {
ObjectStore.initialize(null, null);
initialized = Thread.currentThread();
}
FileDialog dlg = new FileDialog(this, "Open Database", FileDialog.LOAD);
dlg.setFile("*.odb");
dlg.setVisible(true);
if (dlg.getFile() == null) {
return; // Cancel button was pressed
}
process = OPEN_DATABASE; // No response to other events
String dbname = dlg.getDirectory() + dlg.getFile();
setTitle(dbname);
try {
db = Database.open(dbname, ObjectStore.OPEN_UPDATE);
} catch (DatabaseNotFoundException e) {
setTitle("Empty");
showMessageBox("Database not found.");
return;
} catch (AccessViolationException e) {
setTitle("Empty");
showMessageBox("File system access violation.");
return;
} catch (IncompatibleOpenTypeException e) {
setTitle("Empty");
showMessageBox("Database open with a different specification.");
return;
} catch (DatabaseException e) {
setTitle("Empty");
showMessageBox("Invalid database.");
return;
}
if (db != null) {
trans = Transaction.begin(ObjectStore.UPDATE);
root = (BitMapObject)db.getRoot("Root");
if (root != null) {
BitMapObject node = root;
while (node.getNextObject() != null) {
node = node.getNextObject();
}
if (node instanceof OSLine) {
figure = LINE;
figureGroup.select(0);
foreColor = ((OSLine)node).getColor();
colorGroup.select(getColorIndex(foreColor));
}
else if (node instanceof OSRectangle) {
figure = RECTANGLE;
figureGroup.select(1);
foreColor = ((OSRectangle)node).getColor();
colorGroup.select(getColorIndex(foreColor));
fillStatus = ((OSRectangle)node).getFillStatus();
}
else if (node instanceof OSOval) {
figure = OVAL;
figureGroup.select(2);
foreColor = ((OSOval)node).getColor();
colorGroup.select(getColorIndex(foreColor));
fillStatus = ((OSOval)node).getFillStatus();
}
else if (node instanceof OSPolygon) {
figure = POLYGON;
figureGroup.select(3);
foreColor = ((OSPolygon)node).getColor();
colorGroup.select(getColorIndex(foreColor));
fillStatus = ((OSPolygon)node).getFillStatus();
}
else if (node instanceof OSText) {
figure = TEXT;
figureGroup.select(4);
foreColor = ((OSText)node).getColor();
colorGroup.select(getColorIndex(foreColor));
Font font = ((OSText)node).getFont();
textSettings.setName(font.getName());
textNameGroup.select(textSettings.getNameIndex());
textSettings.setStyle(font.getStyle());
textStyleGroup.select(textSettings.getStyleIndex());
textSettings.setSize(font.getSize());
textSizeGroup.select(getIndexFromSize(font.getSize()));
}
else if (node instanceof OSImageFile) {
figure = IMAGE;
figureGroup.select(5);
foreColor = Color.black;
colorGroup.select(getColorIndex(foreColor));
imageSettings.setName(((OSImageFile)node).getFile());
imageSettings.setEnlargeX(((OSImageFile)node).getEnlargeX());
if (imageSettings.getEnlargeX()) {
XDirectionGroup.select(0);
}
else {
XDirectionGroup.select(1);
}
imageSettings.setEnlargeY(((OSImageFile)node).getEnlargeY());
if (imageSettings.getEnlargeY()) {
YDirectionGroup.select(0);
}
else {
YDirectionGroup.select(1);
}
imageSettings.setFactorX(((OSImageFile)node).getFactorX());
XFactorGroup.select(imageSettings.getFactorX() - 1);
imageSettings.setFactorY(((OSImageFile)node).getFactorY());
YFactorGroup.select(imageSettings.getFactorY() - 1);
}
poly = new Polygon();
}
if (fillStatus) {
fillGroup.select(0);
}
else {
fillGroup.select(1);
}
dragging = false;
repaint();
}
}
private void saveDatabase() {
if (db == null) {
showMessageBox("Database not open.");
return;
}
trans.commit();
trans = Transaction.begin(ObjectStore.UPDATE);
root = (BitMapObject)db.getRoot("Root");
}
private void closeDatabase() {
if (db == null) {
showMessageBox("No database to close.");
return;
}
trans.abort();
db.close();
db = null;
setTitle("Empty");
repaint();
}
private void exitApp() {
if (db != null) {
trans.abort();
db.close();
db = null;
repaint();
}
System.exit(0);
}
private void destroyLast() {
if (db == null) {
showMessageBox("Database not open here.");
return;
}
if (root == null) {
showMessageBox("Empty database.");
return;
}
BitMapObject node = root;
BitMapObject temp = node.getNextObject();
if (temp == null) {
root = null;
db.setRoot("Root", root);
ObjectStore.destroy(node);
repaint();
return;
}
while (temp.getNextObject() != null) {
node = temp;
temp = temp.getNextObject();
}
node.setNextObject(null);
ObjectStore.destroy(temp);
repaint();
}
private void clearDatabase() {
if (db == null) {
showMessageBox("Database not open.");
return;
}
if (root == null) {
showMessageBox("Empty database.");
return;
}
while (root != null) {
destroyNode(1);
}
repaint();
showMessageBox("Database is now empty.");
}
private void destroyNode(int place) {
if (place == 1) {
BitMapObject temp = root;
root = root.getNextObject();
db.setRoot("Root", root);
ObjectStore.destroy(temp);
return;
}
BitMapObject node = root;
for (int i = 2; i < place; i++) {
node = node.getNextObject();
}
BitMapObject temp = node.getNextObject();
node.setNextObject(temp.getNextObject());
ObjectStore.destroy(temp);
}
private void setImage() {
if (db == null) {
showMessageBox("Database not open.");
return;
}
FileDialog dlg = new FileDialog(this, "Set Image", FileDialog.LOAD);
dlg.setFile("*.gif");
dlg.setVisible(true);
if (dlg.getFile() == null) {
// Cancel button was pressed
return;
}
imageSettings.setName(dlg.getDirectory() + dlg.getFile());
}
private void currentImage() {
if (db == null) {
showMessageBox("Database not open.");
return;
}
if (imageSettings.getName() == null) {
showMessageBox("Please set image.");
return;
}
AIOS.resetMessageBox();
AIOS.addMessageBoxLabel("Image Name: " + imageSettings.getName(),
Label.CENTER);
if (imageSettings.getEnlargeX()) {
AIOS.addMessageBoxLabel("X is enlarged by a factor of " +
Integer.toString(imageSettings.getFactorX()),
Label.CENTER);
}
else {
AIOS.addMessageBoxLabel("X is reduced by a factor of " +
Integer.toString(imageSettings.getFactorX()),
Label.CENTER);
}
if (imageSettings.getEnlargeY()) {
AIOS.addMessageBoxLabel("Y is enlarged by a factor of " +
Integer.toString(imageSettings.getFactorY()),
Label.CENTER);
}
else {
AIOS.addMessageBoxLabel("Y is reduced by a factor of " +
Integer.toString(imageSettings.getFactorY()),
Label.CENTER);
}
showMessageBox();
}
// Methods to handle java.awt.CheckboxMenuItem events
private void doFillGroup(int index) {
fillStatus = (index == 0);
}
private void doColorGroup(int index) {
foreColor = colors[index];
}
private void doFigureGroup(int index) {
figure = index + 1;
}
private void doTextNameGroup(int index) {
textSettings.setName(index);
}
private void doTextStyleGroup(int index) {
textSettings.setStyle(index);
}
private void doTextSizeGroup(int index) {
textSettings.setSize(getSizeFromIndex(index));
}
private void doXDirectionGroup(int index) {
imageSettings.setEnlargeX(index == 0);
}
private void doYDirectionGroup(int index) {
imageSettings.setEnlargeY(index == 0);
}
private void doXFactorGroup(int index) {
imageSettings.setFactorX(index + 1);
}
private void doYFactorGroup(int index) {
imageSettings.setFactorY(index + 1);
}
// Two useful methods to return a rectangle's top left and bottom right
private Point getTopLeft(int x1, int y1, int x2, int y2) {
if (x1 < x2 && y1 < y2) {
return new Point(x1, y1);
}
if (x1 >= x2 && y1 < y2) {
return new Point(x2, y1);
}
if (x1 < x2 && y1 >= y2) {
return new Point(x1, y2);
}
return new Point(x2, y2);
}
private Point getBottomRight(int x1, int y1, int x2, int y2) {
if (x1 < x2 && y1 < y2) {
return new Point(x2, y2);
}
if (x1 >= x2 && y1 < y2) {
return new Point(x1, y2);
}
if (x1 < x2 && y1 >= y2) {
return new Point(x2, y1);
}
return new Point(x1, y1);
}
// Method used to save currently drawn polygon
private void finishPolygon() {
if (db == null) {
return;
}
if (figure != POLYGON) {
return;
}
if (!dragging) {
return;
}
if (poly.npoints <= 2) {
return;
}
dragging = false;
OSPolygon addend = new OSPolygon();
for (int i = 0; i < poly.npoints; i++) {
addend.addPoint(poly.xpoints[i], poly.ypoints[i]);
}
addFigure(addend);
poly = new Polygon();
repaint();
}
// Method for finding ordinal value of a color in the colors array
private int getColorIndex(Color color) {
boolean found = false;
int i = 0;
while (!found && i < colors.length) {
found = colors[i].equals(color);
if (!found) {
i++;
}
}
if (found) {
return i;
}
return -1;
}
// Two methods to understand formula for 10 font sizes
// Method for getting proper font size from index
private int getSizeFromIndex(int index) {
if (index < 8) {
return index * 2 + 10;
}
if (index == 8) {
return 36;
}
return 48;
}
// Method for getting proper index from size
private int getIndexFromSize(int size) {
if (size < 36) {
return (size - 10) / 2;
}
if (size == 36) {
return 8;
}
return 9;
}
// Useful method for computing absolute value
private int abs(int n) {
if (n < 0) {
return -1 * n;
}
return n;
}
// Useful method for computing direction of an integer (+ or -)
private int direction(int n) {
if (n < 0) {
return -1;
}
return 1;
}
// getBoundingBox method of java.awt.Polygon does not work
// Here is the replacement
private Rectangle getPolyRect(Polygon pol) {
Rectangle rect = new Rectangle(10000, 10000, 0, 0);
for (int i = 0; i < pol.npoints; i++) {
for (int j = i + 1; j < pol.npoints; j++) {
if (abs(pol.xpoints[i] - pol.xpoints[j]) > rect.width) {
rect.width = abs(pol.xpoints[i] - pol.xpoints[j]);
}
if (abs(pol.ypoints[i] - pol.ypoints[j]) > rect.height) {
rect.height = abs(pol.ypoints[i] - pol.ypoints[j]);
}
}
if (pol.xpoints[i] < rect.x) {
rect.x = pol.xpoints[i];
}
if (pol.ypoints[i] < rect.y) {
rect.y = pol.ypoints[i];
}
}
if (rect.width < 2) {
rect.width = 2;
}
if (rect.height < 2) {
rect.height = 2;
}
return rect;
}
// Methods which prepare color settings before displaying a dialog box
private void showMessageBox(String message) {
setForeground(Color.black);
setBackground(Color.lightGray);
process = DELIVER_MESSAGE;
AIOS.showMessageBox(message);
}
private void showMessageBox() {
setForeground(Color.black);
setBackground(Color.lightGray);
process = DELIVER_MESSAGE;
AIOS.showMessageBox();
}
// Override java.awt.Component.handleEvent method to close Window
public boolean handleEvent(Event event) {
if (event.id == Event.WINDOW_DESTROY) {
exitApp();
return true;
}
return super.handleEvent(event);
}
// Override java.awt.Component.action method to catch relevent events
public boolean action(Event event, Object obj) {
if (event.target instanceof MenuItem) {
String label = ((MenuItem) event.target).getLabel();
if (label.equals(newLabel)) {
newDatabase();
return true;
}
if (label.equals(openLabel)) {
openDatabase();
return true;
}
if (label.equals(saveLabel)) {
saveDatabase();
return true;
}
if (label.equals(closeLabel)) {
closeDatabase();
return true;
}
if (label.equals(repaintLabel)) {
repaint();
return true;
}
if (label.equals(destroyLastLabel)) {
destroyLast();
return true;
}
if (label.equals(clearLabel)) {
clearDatabase();
return true;
}
if (label.equals(setImageLabel)) {
setImage();
return true;
}
if (label.equals(currentImageLabel)) {
currentImage();
return true;
}
if (label.equals(exitLabel)) {
exitApp();
return true;
}
}
if (event.target instanceof CheckboxMenuItem) {
CheckboxMenuItem check = (CheckboxMenuItem)(event.target);
int index;
if ((index = fillGroup.select(check)) >= 0) {
doFillGroup(index);
return true;
}
if ((index = XDirectionGroup.select(check)) >= 0) {
doXDirectionGroup(index);
return true;
}
if ((index = YDirectionGroup.select(check)) >= 0) {
doYDirectionGroup(index);
return true;
}
if ((index = figureGroup.select(check)) >= 0) {
doFigureGroup(index);
return true;
}
if ((index = textNameGroup.select(check)) >= 0) {
doTextNameGroup(index);
return true;
}
if ((index = textStyleGroup.select(check)) >= 0) {
doTextStyleGroup(index);
return true;
}
if ((index = textSizeGroup.select(check)) >= 0) {
doTextSizeGroup(index);
return true;
}
if ((index = XFactorGroup.select(check)) >= 0) {
doXFactorGroup(index);
return true;
}
if ((index = YFactorGroup.select(check)) >= 0) {
doYFactorGroup(index);
return true;
}
if ((index = colorGroup.select(check)) >= 0) {
doColorGroup(index);
return true;
}
}
if (event.target instanceof Button) {
String label = (String)obj;
if (label.equals("OK")) {
if (process == DELIVER_MESSAGE) {
AIOS.hideMessageBox();
}
}
else if (label.equals("Cancel")) {
process = NO_PROCESS; // Unlock process
AIOS.hideAll();
}
else {
return false;
}
process = NO_PROCESS; // Unlock process
setForeground(foreColor);
setBackground(backColor);
repaint();
return true;
}
return false;
}
}