/* * @(#)CircleSample0.java 6 October 2005 * * Copyright 2005 * College of Computer Science * Northeastern University * Boston, MA 02115 * * This software may be used for educational purposes as long as * this copyright notice is retained intact at the top of all files. * * Should this software be modified, the words "Modified from * Original" must be included as a comment below this notice. * * All publication rights are retained. This software or its * documentation may not be published in any media either in whole * or in part without explicit permission. * * Contact information: * Richard Rasala rasala@ccs.neu.edu * Viera Proulx vkp@ccs.neu.edu * * Telephone: 617-373-2462 * * This software was created with support from Northeastern * University and from NSF grant DUE-9950829. */ import edu.neu.ccs.*; import edu.neu.ccs.gui.*; import edu.neu.ccs.util.*; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import javax.swing.*; public class CircleSample0 extends DisplayPanel implements JPTConstants { // constant: buffer size protected final int BUFFERSIZE = 400; // square window for painting protected BufferedPanel window = new BufferedPanel(BUFFERSIZE, BUFFERSIZE); // radius text field view protected TextFieldView radiusTFV = new TextFieldView("20", 200); // center x text field view protected TextFieldView xTFV = new TextFieldView("" + (BUFFERSIZE / 2), 200); // center y text field view protected TextFieldView yTFV = new TextFieldView("" + (BUFFERSIZE / 2), 200); // table for input text fields protected TablePanel inputPanel = new TablePanel( new Object[][] { { "Radius:", new Halo(radiusTFV) }, { "Center X:", new Halo(xTFV) }, { "Center Y:", new Halo(yTFV) } }, 5, 5, EAST); // action: graph circle protected Action graphCircle = new SimpleAction("Graph Circle") { public void perform() { graphCircle(); } }; // action: clear graphics window protected Action clear = new SimpleAction("Clear") { public void perform() { clear(); } }; // action: clear graphics window and reset controls protected Action reset = new SimpleAction("Reset") { public void perform() { reset(); } }; // button panel protected ActionsPanel buttonPanel = new ActionsPanel(new Action[] { graphCircle, clear, reset }); // controls panel protected TablePanel controls = new TablePanel( new Object[] { inputPanel, buttonPanel }, VERTICAL, 15, 15, NORTH); // main panel protected TablePanel mainPanel = new TablePanel( new Object[] { new Display(controls, null, "Controls"), new Display(window, null, "Graphics") }, HORIZONTAL, 5, 5, NORTH); // data model correspoding to the input text fields protected double radius, x, y; // circle protected XCircle circle = new XCircle(); // constructor public CircleSample0() { add(mainPanel); addActions(); } // add various actions protected void addActions() { // add the graph circle actions for the text fields radiusTFV.addActionListener(graphCircle); xTFV.addActionListener(graphCircle); yTFV.addActionListener(graphCircle); } // extract radius from GUI protected void extractRadius() { radius = radiusTFV.demandDouble(); // adjust radius if it is too small if (radius < 0.5) radius = 0.5; } // extract radius, x, y from GUI protected void extractRadiusXY() { extractRadius(); x = xTFV.demandDouble(); y = yTFV.demandDouble(); } // method: graph circle protected void graphCircle() { // extract the user data: radius, x, y extractRadiusXY(); // define the circle circle.setXYR(x, y, radius); // define the paintable Paintable paintable = new ShapePaintable(circle, PaintMode.FILL); // get the graphics context of the buffered panel Graphics2D G = window.getBufferGraphics(); // paint paintable.paint(G); // repaint the graphics window window.repaint(); } // method: clear graphics window protected void clear() { window.clearPanelAndSequence(); window.repaint(); } // method: clear graphics window and reset controls public void reset() { clear(); controls.reset(); } // main program public static void main(String[] args) { JPTFrame.createQuickJPTFrame("Circle Sample 0", new CircleSample0()); } }