import javax.swing.JFrame; import java.awt.Graphics; import java.awt.Button; import java.awt.Panel; import java.awt.TextField; import java.awt.Canvas; import java.awt.Font; import java.awt.Container; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * This provides a graphical surface to draw on. * Used for Lab 7 * * @author Bob Broeg * @version CS 161 Lab 7 */ public class DrawingSurface implements ActionListener { private JFrame frame; private Container content; private Canvas canvas; private Graphics graphic; private Button start; private Panel panel; private TextField messageArea; private Font f; /** * Create a drawing canvas */ public DrawingSurface() { frame = new JFrame( "Cell Master" ); content = frame.getContentPane(); content.setLayout( new BorderLayout() ); panel = new Panel(); panel.setSize( new Dimension( 50, 50 )); start = new Button( "Start" ); start.addActionListener( this ); panel.add( start ); content.add( panel, BorderLayout.NORTH ); messageArea = new TextField( "", 30 ); f = new Font( "Comic Sans MS", Font.BOLD, 12 ); messageArea.setFont( f ); content.add( messageArea, BorderLayout.SOUTH ); canvas = new Canvas(); canvas.setSize( new Dimension( 400, 400) ); content.add( canvas, BorderLayout.CENTER ); frame.pack(); frame.setVisible( true ); graphic = canvas.getGraphics(); canvas.setBackground( Color.white ); } /** * This method is taken from the Canvas class * of the shapes project. * * @param color String representing the forground color */ public void setColor( String color ) { if(color.equals("red")) graphic.setColor(Color.red); else if(color.equals("black")) graphic.setColor(Color.black); else if(color.equals("blue")) graphic.setColor(Color.blue); else if(color.equals("yellow")) graphic.setColor(Color.yellow); else if(color.equals("green")) graphic.setColor(Color.green); else if(color.equals("magenta")) graphic.setColor(Color.magenta); else if(color.equals("white")) graphic.setColor(Color.white); else graphic.setColor(Color.black); } /** * Draws a filled circle of the given color. The cirle * has a radius of 5 * * @param x The x coordinate * @param y The y coordinate * @param color The color of the circle */ public void draw( int x, int y, String color ) { setColor( color ); graphic.fillOval( x, y, 10, 10 ); } /** * Erases a circle at the given coordinates by redrawing * the circle in the background color. * * @param x The x coordinate * @param y The y coordinate */ public void erase( int x, int y ) { setColor( "white" ); graphic.fillOval( x, y, 10, 10 ); } /** * Method to print message on the drawing surface. * Gives an approximate ability of showStatus() * * @param message The string to be displayed. */ public void showMessage( String message ) { messageArea.setText( message ); } /** * This is the method that drives the program */ public void go() { Cell first = new Cell( 50, 50, null, "red", this ); for( int i = 0; i < 50; i++ ) { first.fire(); showMessage( "i = " + i ); } } /** * Causes execution to begin if the start button * is pressed. */ public void actionPerformed( ActionEvent e ) { if( e.getSource() == start ) { go(); } } }