Sun Microsystems

Wake Tech

Your Guide to Java

Samples
Syllabus
Assignments
Instructors
Introduction
Samples Index
 

CSC 148 Java Programming
Sample Solutions
Chapter Ten: Problem 35

// Exercise 10.35 Solution

// Draw7.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 Draw7 extends Applet

implements 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;

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 Color colors[] = { Color.black, Color.blue,

Color.red };

public void init()

{

shapes = new Vector( 3 );

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 ) );

add( choice );

add( choice2 );

add( empty );

add( filled );

}

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 paint( Graphics g )

{

MyShape s = null;

for ( int i = 0; i < shapes.size(); i++ ) {

s = ( MyShape ) shapes.elementAt( i );

g.setColor( s.getColor() );

s.draw( g );

}

}

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 Draw7 draw;

public MouseHandler( Draw7 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() == Draw7.OVAL )

s = new MyOval( ux, uy, w, h, c, f );

else if ( draw.getShape() == Draw7.RECTANGLE )

s = new MyRectangle( ux, uy, w, h, c, f );

else if ( draw.getShape() == Draw7.ROUND )

s = new MyRoundedRectangle( ux, uy, w, h, c, f );

else if ( draw.getShape() == Draw7.LINE )

s = new MyLine( draw.getTopX(), draw.getTopY(), x, y, c );

draw.getShapes().addElement( s );

draw.repaint();

}

public void mousePressed( MouseEvent e )

{

draw.setTopX( e.getX() );

draw.setTopY( e.getY() );

}

}

class MouseHandler2 extends MouseMotionAdapter {

private Draw7 draw;

public MouseHandler2( Draw7 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 ) ;

}