|
CSC 148 Java Programming // Exercise 10.36 Solution // Draw8.java // NOTE: This does not implement // rubber-banding. import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.util.*; import com.deitel.jhtp2.ch10.*; public class Draw8 extends Applet implements ActionListener, ItemListener { private int topX, topY, shape; private int width, height, upperX, upperY; private int bottomX, bottomY, color; private Choice choice, choice2; private Checkbox filled, empty; private CheckboxGroup radio; private boolean fillOn, clear; public static final int RECTANGLE = 0; public static final int OVAL = 1; public static final int LINE = 2; public static final int ROUND = 3; private Vector shapes; private Button undo; private Color colors[] = { Color.black, Color.blue, Color.red }; public void init() { shapes = new Vector( 3 ); undo = new Button( "Undo" ); undo.addActionListener( this ); radio = new CheckboxGroup(); filled = new Checkbox( "filled", radio, false ); empty = new Checkbox( "empty", radio, true ); filled.addItemListener( this ); empty.addItemListener( this ); choice = new Choice(); choice.addItem( "Rectangle" ); choice.addItem( "Oval" ); choice.addItem( "Line" ); choice.addItem( "Rounded Rectangle" ); choice.addItemListener( this ); choice2 = new Choice(); choice2.addItem( "Black" ); choice2.addItem( "Blue" ); choice2.addItem( "Red" ); choice2.addItemListener( this ); addMouseListener( new MouseHandler( this ) ); addMouseMotionListener( new MouseHandler2( this ) ); undo.setEnabled( false ); add( choice ); add( choice2 ); add( empty ); add( filled ); add( undo ); } public int getTopX() { return topX; } public int getTopY() { return topY; } public Vector getShapes() { return shapes; } public boolean getFillOn() { return fillOn; } public Color getColor() { return colors[ color ]; } public int getShape() { return shape; } public void setShape( int s ) { shape = s; } public void setColor( int c ) { color = c; } public void setTopX( int x ) { topX = x; } public void setTopY( int y ) { topY = y; } public void setBottomX( int x ) { bottomX = x; } public void setBottomY( int y ) { bottomY = y; } public void setUndoEnable() { undo.setEnabled( true ); } public void paint( Graphics g ) { MyShape s = null; if ( clear == true ) { g.setColor( getBackground() ); g.fillRect( 0, 0, getSize().width, getSize().height ); clear = false; } for ( int i = 0; i < shapes.size(); i++ ) { s = ( MyShape ) shapes.elementAt( i ); g.setColor( s.getColor() ); s.draw( g ); } } public void actionPerformed( ActionEvent e ) { if ( shapes.size() > 0 ) { shapes.removeElement( shapes.lastElement() ); clear = true; if ( shapes.size() == 0 ) undo.setEnabled( false ); repaint(); } } public void itemStateChanged( ItemEvent e ) { if ( e.getSource() instanceof Choice ) if ( e.getItemSelectable() == choice ) setShape( choice.getSelectedIndex() ); else setColor( choice2.getSelectedIndex() ); else // radio button if ( empty.getState() == true ) fillOn = false; else fillOn = true; } public void update( Graphics g ) { paint( g ); } } class MouseHandler extends MouseAdapter { private Draw8 draw; public MouseHandler( Draw8 d ) { draw = d; } public void mouseReleased( MouseEvent e ) { int x = e.getX(); int y = e.getY(); int w = Math.abs( draw.getTopX() - x ); int h = Math.abs( draw.getTopY() - y ); int ux = Math.min( draw.getTopX(), x ); int uy = Math.min( draw.getTopY(), y ); MyShape s = null; boolean f = draw.getFillOn(); Color c = draw.getColor(); if ( draw.getShape() == Draw8.OVAL ) s = new MyOval( ux, uy, w, h, c, f ); else if ( draw.getShape() == Draw8.RECTANGLE ) s = new MyRectangle( ux, uy, w, h, c, f ); else if ( draw.getShape() == Draw8.ROUND ) s = new MyRoundedRectangle( ux, uy, w, h, c, f ); else if ( draw.getShape() == Draw8.LINE ) s = new MyLine( draw.getTopX(), draw.getTopY(), x, y, c ); draw.setUndoEnable(); draw.getShapes().addElement( s ); draw.repaint(); } public void mousePressed( MouseEvent e ) { draw.setTopX( e.getX() ); draw.setTopY( e.getY() ); } } class MouseHandler2 extends MouseMotionAdapter { private Draw8 draw; public MouseHandler2( Draw8 d ) { draw = d; } public void mouseDragged( MouseEvent e ) { draw.setBottomX( e.getX() ); draw.setBottomY( e.getY() ); draw.repaint(); } }
import java.awt.*; public class MyLine extends MyShape { private int endX, endY; public MyLine( int x, int y, int x1, int y1, Color c ) { setX( x ); setY( y ); setColor( c ); endX = x1; endY = y1; } public void draw( Graphics g ) { g.drawLine( getX(), getY(), endX, endY ); } }
import java.awt.*; public class MyOval extends MyShape { public MyOval( int x, int y, int w, int h, Color c, boolean f ) { setX( x ); setY( y ); setWidth( w ); setHeight( h ); setColor( c ); setFilled( f ); } public void draw( Graphics g ) { if ( getFilled() ) g.fillOval( getX(), getY(), getWidth(), getHeight() ); else g.drawOval( getX(), getY(), getWidth(), getHeight() ); } }
import java.awt.*; public class MyRectangle extends MyShape { public MyRectangle( int x, int y, int w, int h, Color c, boolean f ) { setX( x ); setY( y ); setWidth( w ); setHeight( h ); setColor( c ); setFilled( f ); } public void draw( Graphics g ) { if ( getFilled() ) g.fillRect( getX(), getY(), getWidth(), getHeight() ); else g.drawRect( getX(), getY(), getWidth(), getHeight() ); } }
import java.awt.*; public class MyRoundedRectangle extends MyShape { public MyRoundedRectangle( int x, int y, int w, int h, Color c, boolean f ) { setX( x ); setY( y ); setWidth( w ); setHeight( h ); setColor( c ); setFilled( f ); } public void draw( Graphics g ) { if ( getFilled() ) g.fillRoundRect( getX(), getY(), getWidth(), getHeight(), 10, 20 ); else g.drawRoundRect( getX(), getY(), getWidth(), getHeight(), 10, 20 ); } }
import java.awt.*; public abstract class MyShape { private int x, y; private int width, height; private Color color; private boolean filled; // public set methods of class public void setX( int x ) { this.x = x; } public void setY( int y ) { this.y = y; } public void setWidth( int w ) { width = w; } public void setHeight( int h ) { height = h; } public void setColor( Color c ) { color = c; } public void setFilled( boolean b ) { filled = b; } // public get methods of class public int getX() { return x; } public int getY() { return y; } public int getWidth() { return width; } public int getHeight() { return height; } public Color getColor() { return color; } public boolean getFilled() { return filled; } // abstract method(s) public abstract void draw( Graphics g ) ; }
package com.deitel.jhtp2.ch10; import java.awt.*; public class MyLine extends MyShape { private int endX, endY; public MyLine( int x, int y, int x1, int y1, Color c ) { setX( x ); setY( y ); setColor( c ); endX = x1; endY = y1; } public void draw( Graphics g ) { g.drawLine( getX(), getY(), endX, endY ); } }
package com.deitel.jhtp2.ch10; import java.awt.*; public class MyOval extends MyShape { public MyOval( int x, int y, int w, int h, Color c, boolean f ) { setX( x ); setY( y ); setWidth( w ); setHeight( h ); setColor( c ); setFilled( f ); } public void draw( Graphics g ) { if ( getFilled() ) g.fillOval( getX(), getY(), getWidth(), getHeight() ); else g.drawOval( getX(), getY(), getWidth(), getHeight() ); } }
package com.deitel.jhtp2.ch10; import java.awt.*; public class MyRectangle extends MyShape { public MyRectangle( int x, int y, int w, int h, Color c, boolean f ) { setX( x ); setY( y ); setWidth( w ); setHeight( h ); setColor( c ); setFilled( f ); } public void draw( Graphics g ) { if ( getFilled() ) g.fillRect( getX(), getY(), getWidth(), getHeight() ); else g.drawRect( getX(), getY(), getWidth(), getHeight() ); } }
package com.deitel.jhtp2.ch10; import java.awt.*; public class MyRoundedRectangle extends MyShape { public MyRoundedRectangle( int x, int y, int w, int h, Color c, boolean f ) { setX( x ); setY( y ); setWidth( w ); setHeight( h ); setColor( c ); setFilled( f ); } public void draw( Graphics g ) { if ( getFilled() ) g.fillRoundRect( getX(), getY(), getWidth(), getHeight(), 10, 20 ); else g.drawRoundRect( getX(), getY(), getWidth(), getHeight(), 10, 20 ); } }
package com.deitel.jhtp2.ch10; import java.awt.*; public abstract class MyShape { private int x, y; private int width, height; private Color color; private boolean filled; // public set methods of class public void setX( int x ) { this.x = x; } public void setY( int y ) { this.y = y; } public void setWidth( int w ) { width = w; } public void setHeight( int h ) { height = h; } public void setColor( Color c ) { color = c; } public void setFilled( boolean b ) { filled = b; } // public get methods of class public int getX() { return x; } public int getY() { return y; } public int getWidth() { return width; } public int getHeight() { return height; } public Color getColor() { return color; } public boolean getFilled() { return filled; } // abstract method(s) public abstract void draw( Graphics g ) ; }
|