ShapeDrawWithMenu - The ShapeDraw applet lets the user add...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
/* The ShapeDraw applet lets the user add small colored shapes to a drawing area and then drag them around. The shapes are rectangles, ovals, and roundrects. The user adds a shape to the canvas by clicking on a button. The shape is added at the upper left corner of the canvas. The color of the shape is given by the current setting of a pop-up menu. The user can drag the shapes with the mouse. Ordinarily, the shapes maintain a given back-to-front order. However, if the user shift-clicks on a shape, that shape will be brought to the front. A menu can be popped up on a shape (by right-clicking or performing some othe platform-dependent action). This menu allows the user to change the size and color of a shape. It is also possible to delete the shape and to bring it to the front. This file defines the applet class plus several other classes used by the applet, namely: ShapeCanvas, Shape, RectShape, OvalShape, and RoundRectShape. David Eck July 28, 1998 */ * import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.util.Vector; i public class ShapeDrawWithMenu extends Applet { public void init() { // Set up the applet's GUI. It consists of a canvas, or drawing area, // plus a row of controls below the canvas. The controls include three // buttons which are used to add shapes to the canvas and a Choice menu // that is used to select the color used for a shape when it is created. // The canvas is set as the "listener" for these controls so that it can // respond to the user's actions. (The pop-up menu is created by the canvas.) setBackground(Color.lightGray); ShapeCanvas canvas = new ShapeCanvas(); // create the canvas Choice colorChoice = new Choice(); // color choice menu colorChoice.add("Red"); colorChoice.add("Green"); colorChoice.add("Blue"); colorChoice.add("Cyan"); colorChoice.add("Magenta"); colorChoice.add("Yellow"); colorChoice.add("Black"); colorChoice.add("White"); colorChoice.addItemListener(canvas); Button rectButton = new Button("Rect"); // buttons for adding shapes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
rectButton.addActionListener(canvas); Button ovalButton = new Button("Oval"); ovalButton.addActionListener(canvas); Button roundRectButton = new Button("RoundRect"); roundRectButton.addActionListener(canvas); Panel bottom = new Panel(); // a Panel to hold the control buttons bottom.setLayout(new GridLayout(1,4,3,3)); bottom.add(rectButton); bottom.add(ovalButton); bottom.add(roundRectButton); bottom.add(colorChoice); setLayout(new BorderLayout(3,3)); add("Center",canvas); // add canvas and controls to the applet add("South",bottom); } public Insets getInsets() { // Says how much space to leave between the edges of the applet and the // components in the applet. return new Insets(3,3,3,3);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

ShapeDrawWithMenu - The ShapeDraw applet lets the user add...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online