12 package org.bitstorm.gameoflife;
14 import java.awt.AWTEvent;
15 import java.awt.Color;
16 import java.awt.Dimension;
17 import java.awt.Event;
18 import java.awt.Frame;
19 import java.awt.GridBagConstraints;
20 import java.awt.GridBagLayout;
21 import java.awt.Image;
23 import java.awt.MenuBar;
24 import java.awt.MenuItem;
25 import java.awt.Point;
26 import java.awt.Toolkit;
27 import java.awt.datatransfer.DataFlavor;
28 import java.awt.datatransfer.Transferable;
29 import java.awt.dnd.DnDConstants;
30 import java.awt.dnd.DropTarget;
31 import java.awt.dnd.DropTargetDragEvent;
32 import java.awt.dnd.DropTargetDropEvent;
33 import java.awt.dnd.DropTargetEvent;
34 import java.awt.dnd.DropTargetListener;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
38 import java.io.FileNotFoundException;
39 import java.io.IOException;
41 import java.util.Enumeration;
42 import java.util.Properties;
43 import java.util.Vector;
66 public static void main(String args[]) {
77 public void init( Frame parent ) {
82 setBackground(
new Color(0x999999));
95 }
catch (NoClassDefFoundError e) {
104 GridBagLayout gridbag =
new GridBagLayout();
105 GridBagConstraints canvasContraints =
new GridBagConstraints();
107 canvasContraints.fill = GridBagConstraints.BOTH;
108 canvasContraints.weightx = 1;
109 canvasContraints.weighty = 1;
110 canvasContraints.gridx = GridBagConstraints.REMAINDER;
111 canvasContraints.gridy = 0;
112 canvasContraints.anchor = GridBagConstraints.CENTER;
115 GridBagConstraints controlsContraints =
new GridBagConstraints();
116 canvasContraints.gridx = GridBagConstraints.REMAINDER;
117 canvasContraints.gridy = 1;
118 controlsContraints.gridx = GridBagConstraints.REMAINDER;
119 gridbag.setConstraints(
controls, controlsContraints);
132 if ( args.length > 0 ) {
149 return System.getProperty( parm );
157 new AlertBox( appletFrame,
"Alert", s );
186 private final DataFlavor
urlFlavor =
new DataFlavor(
"application/x-java-url; class=java.net.URL",
"Game of Life URL");
193 if ( event.isDataFlavorSupported( DataFlavor.javaFileListFlavor ) ||
event.isDataFlavorSupported( urlFlavor ) ) {
221 public void drop(DropTargetDropEvent event) {
223 if ( event.isDataFlavorSupported( urlFlavor ) ) {
225 event.acceptDrop(DnDConstants.ACTION_COPY);
226 Transferable trans =
event.getTransferable();
227 URL url = (URL)( trans.getTransferData( urlFlavor ) );
228 String urlStr = url.toString();
231 event.dropComplete(
true);
232 }
catch (Exception e) {
233 event.dropComplete(
false);
235 }
else if ( event.isDataFlavorSupported( DataFlavor.javaFileListFlavor ) ) {
237 event.acceptDrop(DnDConstants.ACTION_COPY);
238 Transferable trans =
event.getTransferable();
239 java.util.List list = (java.util.List)( trans.getTransferData( DataFlavor.javaFileListFlavor ) );
240 File droppedFile = (File) list.get(0);
241 gridIO.
openShape( droppedFile.getPath() );
243 event.dropComplete(
true);
244 }
catch (Exception e) {
245 event.dropComplete(
false);
283 boolean nextLine =
false;
286 if ( filename != null ) {
289 file =
new EasyFile( appletFrame,
"Open Game of Life file" );
292 }
catch (FileNotFoundException e) {
293 new AlertBox( appletFrame,
"File not found",
"Couldn't open this file.\n"+e.getMessage());
294 }
catch (IOException e) {
295 new AlertBox( appletFrame,
"File read error",
"Couldn't read this file.\n"+e.getMessage());
308 boolean nextLine =
false;
316 }
catch (FileNotFoundException e) {
317 new AlertBox( appletFrame,
"URL not found",
"Couldn't open this URL.\n"+e.getMessage());
318 }
catch (IOException e) {
319 new AlertBox( appletFrame,
"URL read error",
"Couldn't read this URL.\n"+e.getMessage());
342 if ( shapeDim.width > gridDim.width || shapeDim.height > gridDim.height ) {
344 Toolkit toolkit = getToolkit();
345 Dimension screenDim = toolkit.getScreenSize();
346 Dimension frameDim = appletFrame.getSize();
349 width = frameDim.width + cellSize*(shapeDim.width - gridDim.width);
350 height = frameDim.height + cellSize*(shapeDim.height - gridDim.height);
352 if ( width > screenDim.width || height > screenDim.height ) {
356 width = frameDim.width + newCellSize*shapeDim.width - cellSize*gridDim.width;
357 height = frameDim.height + newCellSize*shapeDim.height - cellSize*gridDim.height;
369 appletFrame.setSize( width, height );
392 Vector cells =
new Vector();
394 if ( text.length() == 0 )
400 while ( enm.hasMoreElements() ) {
401 String line = (String) enm.nextElement();
402 if ( line.startsWith(
"#") || line.startsWith(
"!") )
405 char[] ca = line.toCharArray();
406 for ( col=0; col < ca.length; col++ ) {
421 cells.addElement(
new int[] {col, row});
426 cellArray =
new int[cells.size()][];
427 for (
int i=0; i<cells.size(); i++ )
428 cellArray[i] = (
int[]) cells.get(i);
429 return new Shape( name, cellArray );
439 int colStart = dim.width;
440 int rowStart = dim.height;
442 String lineSeparator = System.getProperty(
"line.separator" );
443 StringBuffer text =
new StringBuffer(
"!Generator: Game of Life (http://www.bitstorm.org/gameoflife/)"+lineSeparator+
"!Variation: 23/3"+lineSeparator+
"!"+lineSeparator);
445 for (
int row = 0; row < dim.height; row++ ) {
446 for (
int col = 0; col < dim.width; col++ ) {
447 if ( grid.
getCell( col, row ) ) {
448 if ( row < rowStart )
450 if ( col < colStart )
460 for (
int row = rowStart; row <= rowEnd; row++ ) {
461 for (
int col = colStart; col <= colEnd; col++ ) {
462 text.append( grid.
getCell( col, row ) ?
'O' :
'-' );
464 text.append( lineSeparator );
468 file =
new EasyFile( appletFrame,
"Save Game of Life file" );
472 }
catch (FileNotFoundException e) {
473 new AlertBox( appletFrame,
"File error",
"Couldn't open this file.\n"+e.getMessage());
474 }
catch (IOException e) {
475 new AlertBox( appletFrame,
"File error",
"Couldn't write to this file.\n"+e.getMessage());
497 URL iconURL = this.getClass().getResource(
"icon.gif");
498 Image icon = Toolkit.getDefaultToolkit().getImage( iconURL );
499 this.setIconImage( icon );
501 enableEvents(Event.WINDOW_DESTROY);
503 MenuBar menubar =
new MenuBar();
504 Menu fileMenu =
new Menu(
"File",
true);
505 MenuItem readMenuItem =
new MenuItem(
"Open...");
506 readMenuItem.addActionListener(
507 new ActionListener() {
508 public synchronized void actionPerformed(ActionEvent e) {
515 MenuItem writeMenuItem =
new MenuItem(
"Save...");
516 writeMenuItem.addActionListener(
517 new ActionListener() {
518 public synchronized void actionPerformed(ActionEvent e) {
524 MenuItem quitMenuItem =
new MenuItem(
"Exit");
525 quitMenuItem.addActionListener(
526 new ActionListener() {
527 public synchronized void actionPerformed(ActionEvent e) {
533 Menu helpMenu =
new Menu(
"Help",
true);
534 MenuItem manualMenuItem =
new MenuItem(
"Manual");
535 manualMenuItem.addActionListener(
536 new ActionListener() {
537 public synchronized void actionPerformed(ActionEvent e) {
542 MenuItem licenseMenuItem =
new MenuItem(
"License");
543 licenseMenuItem.addActionListener(
544 new ActionListener() {
545 public synchronized void actionPerformed(ActionEvent e) {
550 MenuItem aboutMenuItem =
new MenuItem(
"About");
551 aboutMenuItem.addActionListener(
552 new ActionListener() {
553 public synchronized void actionPerformed(ActionEvent e) {
558 fileMenu.add(readMenuItem);
559 fileMenu.add(writeMenuItem);
560 fileMenu.addSeparator();
561 fileMenu.add(quitMenuItem);
562 helpMenu.add(manualMenuItem);
563 helpMenu.add(licenseMenuItem);
564 helpMenu.add(aboutMenuItem);
565 menubar.add(fileMenu);
566 menubar.add(helpMenu);
568 GridBagLayout gridbag =
new GridBagLayout();
569 GridBagConstraints appletContraints =
new GridBagConstraints();
571 appletContraints.fill = GridBagConstraints.BOTH;
572 appletContraints.weightx = 1;
573 appletContraints.weighty = 1;
574 gridbag.setConstraints(applet, appletContraints);
578 Toolkit screen = getToolkit();
579 Dimension screenSize = screen.getScreenSize();
581 if ( screenSize.width >= 640 && screenSize.height >= 480 )
582 setLocation((screenSize.width-550)/2, (screenSize.height-400)/2);
598 if ( e.getID() == Event.WINDOW_DESTROY )
606 Properties properties = System.getProperties();
607 String jvmProperties =
"Java VM "+properties.getProperty(
"java.version")+
" from "+properties.getProperty(
"java.vendor");
608 Point p = getLocation();
609 new AboutDialog(
this,
"About the Game of Life",
new String[] {
"Version 1.5 - Copyright 1996-2004 Edwin Martin",
"http://www.bitstorm.org/gameoflife/", jvmProperties},
"about.jpg", p.x+100, p.y+60 );
616 Point p = getLocation();
617 new TextFileDialog(
this,
"Game of Life Manual",
"manual.txt", p.x+60, p.y+60 );
624 Point p = getLocation();
625 new TextFileDialog(
this,
"Game of Life License",
"license.txt", p.x+60, p.y+60 );
Shape makeShape(String name, String text)
GameOfLifeGrid gameOfLifeGrid
static void main(String args[])
GameOfLifeControls controls
synchronized boolean getCell(int col, int row)
void dragOver(DropTargetDragEvent event)
String getParameter(String parm)
void processEvent(AWTEvent e)
void setFileExtension(String s)
void showStatus(String s)
synchronized void setShape(Shape shape)
void dropActionChanged(DropTargetDragEvent event)
static Shape getShapeByName(String name)
void dragExit(DropTargetEvent event)
CellGridCanvas gameOfLifeCanvas
GameOfLifeGridIO getGameOfLifeGridIO()
synchronized void clear()
void openShape(EasyFile file)
void setShape(Shape shape)
void setAfterWindowResize(Shape newShape, int newCellSize)
AppletFrame(String title, StandaloneGameOfLife applet)
final String FILE_EXTENSION
void openShape(String filename)
final DataFlavor urlFlavor
void drop(DropTargetDropEvent event)
void setFileName(String s)
void writeText(String text)
void setShape(Shape shape)
static final String SMALL
void addGameOfLifeControlsListener(GameOfLifeControlsListener listener)
static final int SIZE_SMALL
StandaloneGameOfLife getStandaloneGameOfLife()
GameOfLifeGridIO(GameOfLifeGrid grid)
void dragEnter(DropTargetDragEvent event)