|
CSC 148 Java Programming // Exercise 10.9 Solution // Align.java // This program creates a simple GUI import java.applet.Applet; import java.awt.*; public class Align extends Applet { private Button ok, cancel, help; private TextField xValue, yValue; private Checkbox snap, show; private Label xLabel, yLabel; private Panel checkPanel, buttonPanel, fieldPanel1, fieldPanel2, fieldPanel; public void init() { // build checkPanel snap = new Checkbox( "Snap to Grid" ); show = new Checkbox( "Show Grid" ); checkPanel = new Panel(); checkPanel.setLayout( new GridLayout( 2 , 1 ) ); checkPanel.add( snap ); checkPanel.add( show ); // build field panel1 xLabel = new Label( "X: " ); xValue = new TextField( "8", 3 ); fieldPanel1 = new Panel(); fieldPanel1.setLayout( new FlowLayout( FlowLayout.CENTER, 3, 5 ) ); fieldPanel1.add( xLabel ); fieldPanel1.add( xValue ); yLabel = new Label( "Y: " ); yValue = new TextField( "8", 3 ); fieldPanel2 = new Panel(); fieldPanel2.setLayout( new FlowLayout( FlowLayout.CENTER, 3, 5 ) ); fieldPanel2.add( yLabel ); fieldPanel2.add( yValue ); fieldPanel = new Panel(); fieldPanel.setLayout( new BorderLayout() ); fieldPanel.add( fieldPanel1, BorderLayout.NORTH ); fieldPanel.add( fieldPanel2, BorderLayout.SOUTH );
// build button panel ok = new Button( "Ok" ); cancel = new Button( "Cancel" ); help = new Button( "Help" ); buttonPanel = new Panel(); buttonPanel.setLayout( new GridLayout( 3, 1, 10, 5 ) ); buttonPanel.add( ok ); buttonPanel.add( cancel ); buttonPanel.add( help ); // set layout for applet setLayout( new FlowLayout( FlowLayout.CENTER, 10, 5 ) ); add( checkPanel ); add( fieldPanel ); add( buttonPanel ); } }
// Solution exercise 10.10 // Calculator.java // This program creates a simple GUI // html: width = 270 height = 200 import java.applet.Applet; import java.awt.*; public class Calculator extends Applet { private Button keys[]; private Panel keyPad; private TextField lcd; public void init() { lcd = new TextField( 20 ); keyPad = new Panel(); keys = new Button[ 16 ]; lcd.setEditable( false ); for ( int i = 0; i <= 9; i++ ) keys[ i ] = new Button( String.valueOf( i ) ); keys[ 10 ] = new Button( "/" ); keys[ 11 ] = new Button( "*" ); keys[ 12 ] = new Button( "-" ); keys[ 13 ] = new Button( "+" ); keys[ 14 ] = new Button( "=" ); keys[ 15 ] = new Button( "." ); // set keyPad layout to grid layout keyPad.setLayout( new GridLayout( 4, 4 ) ); for ( int i = 7; i <= 10; i++ ) // 7, 8, 9, 10 keyPad.add( keys[ i ] ); // divide for ( int i = 4; i <= 6; i++ ) // 4, 5, 6 keyPad.add( keys[ i ] ); keyPad.add( keys[ 11 ] ); // multiply for ( int i = 1; i <= 3; i++ ) // 1, 2, 3 keyPad.add( keys[ i ] ); keyPad.add( keys[ 12 ] ); // subtract keyPad.add( keys[ 0 ] ); // 0 for (int i = 15; i >= 13; i--) keyPad.add( keys[ i ] ); // ., =, add // set applet layout to border layout setLayout( new BorderLayout() ); add( lcd, BorderLayout.NORTH ); add( keyPad, BorderLayout.CENTER ); } }
// Exercise 10.11 solution // ColorSelect.java // This program creates a simple GUI import java.applet.Applet; import java.awt.*; public class ColorSelect extends Applet { private Button ok, cancel; private Checkbox b, f; private Choice colorList; private Panel p, p1; public void init() { // applet setLayout( new BorderLayout() ); // north colorList = new Choice(); colorList.addItem( "RED" ); add( colorList, BorderLayout.NORTH ); // center p = new Panel(); b = new Checkbox( "background" ); f = new Checkbox( "foreground" ); p.add( b ); p.add( f ); add( p, BorderLayout.CENTER ); // south ok = new Button( "Ok" ); cancel = new Button( "Cancel" ); p1 = new Panel(); p1.add( ok ); p1.add( cancel ); add( p1, BorderLayout.SOUTH ); } }
// Exercise 10.12 Solution // Printer.java // This program creates a simple GUI // html: width = 400 height = 130 import java.applet.Applet; import java.awt.*; public class Printer extends Applet { private Button b1, b2, b3, b4; private Checkbox c1, c2, c3, c4, c5, c6, c7; private Choice q; private Label label1, label2; private Panel p1, p2, p3, p4, p5, p6, p7, p8; private CheckboxGroup cbg;
public void init() { // build left north panel label1 = new Label( "Printer: Epson EPL-7000" ); p1 = new Panel(); p1.setLayout( new FlowLayout( FlowLayout.LEFT ) ); p1.add( label1 ); // build right east panel b1 = new Button( "Ok" ); b2 = new Button( "Cancel" ); b3 = new Button( "Setup..." ); b4 = new Button( "Help" ); p2 = new Panel(); p2.setLayout( new GridLayout( 4, 1, 5, 5 ) ); p2.add( b1 ); p2.add( b2 ); p2.add( b3 ); p2.add( b4 ); // build left south panel label2 = new Label( "Print Quality: " ); q = new Choice(); q.addItem( "High" ); c1 = new Checkbox( "Print to File" ); p3 = new Panel(); p3.setLayout( new FlowLayout( FlowLayout.LEFT, 10, 0 ) ); p3.add( label2 ); p3.add( q ); p3.add( c1 ); // build left east panel c2 = new Checkbox( "Image" ); c3 = new Checkbox( "Text" ); c4 = new Checkbox( "Code" ); p4 = new Panel(); p4.setLayout( new BorderLayout( ) ); p4.add( c2, BorderLayout.NORTH ); p4.add( c3, BorderLayout.CENTER ); p4.add( c4, BorderLayout.SOUTH ); // build left west panel cbg = new CheckboxGroup(); p5 = new Panel(); p5.setLayout( new BorderLayout() ); p5.add( c5 = new Checkbox( "Selection", cbg, false ), BorderLayout.NORTH ); p5.add( c6 = new Checkbox( "All", cbg, true ), BorderLayout.CENTER ); p5.add( c7 = new Checkbox( "Applet", cbg, false ), BorderLayout.SOUTH ); // build left center p8 = new Panel(); p8.setLayout( new FlowLayout( FlowLayout.CENTER, 30, 0 ) ); p8.setBackground( Color.white ); p8.add( p4 ); p8.add( p5 );
// setup left panel p6 = new Panel(); p6.setLayout( new BorderLayout() ); p6.add( p1, BorderLayout.NORTH ); p6.add( p8, BorderLayout.CENTER ); p6.add( p3, BorderLayout.SOUTH ); // setup applet layout p7 = new Panel(); p7.setLayout( new FlowLayout( FlowLayout.CENTER, 10, 0 ) ); p7.add( p6 ); p7.add( p2 ); add( p7 ); } }
// Exercise 10.14 Solution // Convert.java // Temperature conversion program import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Convert extends Applet implements ActionListener { private Panel p; private Label label1, label2; private TextField temperatureF; private TextField temperatureC; public void init() { setLayout( new BorderLayout() ); p = new Panel(); p.setLayout( new BorderLayout() ); label1 = new Label( "Enter Fahrenheit temperature:" ); label2 = new Label( "Temperature in Celcius is:" ); temperatureF = new TextField( 10 ); temperatureF.addActionListener( this ); temperatureC = new TextField( 10 ); temperatureC.setEditable( false ); add( label1, BorderLayout.NORTH ); p.add( temperatureF, BorderLayout.NORTH ); p.add( label2, BorderLayout.SOUTH ); add( p, BorderLayout.CENTER ); add( temperatureC, BorderLayout.SOUTH ); } public void actionPerformed( ActionEvent e ) { int celcius, temp; temp = Integer.parseInt( temperatureF.getText() ); celcius = ( int ) ( 5.0f / 9.0f * ( temp - 32 ) ); temperatureC.setText( String.valueOf( celcius ) ); } }
// Exercise 10.30 Solution // Draw2.java // Program draws a rectangle with the mouse // NOTE: This solution is virtually // identical to Exercise 10.16 import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Draw2 extends Applet { private int topX, topY; private int width, height; private int bottomX, bottomY; public void init() { topX = topY = 0; addMouseListener( new MouseHandler( this ) ); } 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 ) { width = Math.abs( topX - bottomX ); height = Math.abs( topY - bottomY ); topX = Math.min( topX, bottomX ); topY = Math.min( topY, bottomY ); g.drawRect( topX, topY, width, height ); } } class MouseHandler extends MouseAdapter { private Draw2 draw; public MouseHandler( Draw2 d ) { draw = d; } public void mouseReleased( MouseEvent e ) { draw.setBottomX( e.getX() ); draw.setBottomY( e.getY() ); draw.repaint(); } public void mousePressed( MouseEvent e ) { draw.setTopX( e.getX() ); draw.setTopY( e.getY() ); } }
// Exercise 10.31 Solution // Draw3.java // Program draws a rectangle with the mouse import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Draw3 extends Applet { private int topX, topY; private int width, height, upperX, upperY; private int bottomX, bottomY; public void init() { addMouseListener( new MouseHandler( this ) ); addMouseMotionListener( new MouseHandler2( this ) ); } 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 ) { width = Math.abs( topX - bottomX ); height = Math.abs( topY - bottomY ); upperX = Math.min( topX, bottomX ); upperY = Math.min( topY, bottomY ); g.drawRect( upperX, upperY, width, height ); } } class MouseHandler extends MouseAdapter { private Draw3 draw; public MouseHandler( Draw3 d ) { draw = d; } public void mouseReleased( MouseEvent e ) { draw.setBottomX( e.getX() ); draw.setBottomY( e.getY() ); draw.repaint(); } public void mousePressed( MouseEvent e ) { draw.setTopX( e.getX() ); draw.setTopY( e.getY() ); } } class MouseHandler2 extends MouseMotionAdapter { private Draw3 draw; public MouseHandler2( Draw3 d ) { draw = d; } public void mouseDragged( MouseEvent e ) { draw.setBottomX( e.getX() ); draw.setBottomY( e.getY() ); draw.repaint(); } }
// Exercise 10.32 Solution // Draw4.java // Program draws a shape with the mouse import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Draw4 extends Applet implements ItemListener { private int topX, topY, shape; private int width, height, upperX, upperY; private int bottomX, bottomY; private Choice choice; private final int RECTANGLE = 0; private final int OVAL = 1; private final int LINE = 2; private final int ROUND = 3; public void init() { choice = new Choice(); choice.addItem( "Rectangle" ); choice.addItem( "Oval" ); choice.addItem( "Line" ); choice.addItem( "Rounded Rectangle" ); choice.addItemListener( this ); addMouseListener( new MouseHandler( this ) ); addMouseMotionListener( new MouseHandler2( this ) ); add( choice ); } 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 ) { width = Math.abs( topX - bottomX ); height = Math.abs( topY - bottomY ); upperX = Math.min( topX, bottomX ); upperY = Math.min( topY, bottomY ); switch ( shape ) { case RECTANGLE: g.drawRect( upperX, upperY, width, height ); break; case OVAL: g.drawOval( upperX, upperY, width, height ); break; case LINE: g.drawLine( topX, topY, bottomX, bottomY ); break; case ROUND: g.drawRoundRect( upperX, upperY, width, height, 10, 20 ); break; } } public void setShape( int s ) { shape = s; } public void itemStateChanged( ItemEvent e ) { setShape( choice.getSelectedIndex() ); } } class MouseHandler extends MouseAdapter { private Draw4 draw; public MouseHandler( Draw4 d ) { draw = d; } public void mouseReleased( MouseEvent e ) { draw.setBottomX( e.getX() ); draw.setBottomY( e.getY() ); draw.repaint(); } public void mousePressed( MouseEvent e ) { draw.setTopX( e.getX() ); draw.setTopY( e.getY() ); } } class MouseHandler2 extends MouseMotionAdapter { private Draw4 draw; public MouseHandler2( Draw4 d ) { draw = d; } public void mouseDragged( MouseEvent e ) { draw.setBottomX( e.getX() ); draw.setBottomY( e.getY() ); draw.repaint(); } }
// Exercise 10.33 Solution // Draw5.java // Program draws a shape with the mouse // in different colors import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Draw5 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 final int RECTANGLE = 0; private final int OVAL = 1; private final int LINE = 2; private final int ROUND = 3; private Color colors[] = { Color.black, Color.blue, Color.red }; public void init() { 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 ); } 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 ) { width = Math.abs( topX - bottomX ); height = Math.abs( topY - bottomY ); upperX = Math.min( topX, bottomX ); upperY = Math.min( topY, bottomY ); g.setColor( colors[ color ] ); switch ( shape ) { case RECTANGLE: g.drawRect( upperX, upperY, width, height ); break; case OVAL: g.drawOval( upperX, upperY, width, height ); break; case LINE: g.drawLine( topX, topY, bottomX, bottomY ); break; case ROUND: g.drawRoundRect( upperX, upperY, width, height, 10, 20 ); break; } } public void setShape( int s ) { shape = s; } public void setColor( int c ) { color = c; } public void itemStateChanged( ItemEvent e ) { if ( e.getItemSelectable() == choice ) setShape( choice.getSelectedIndex() ); else setColor( choice2.getSelectedIndex() ); } } class MouseHandler extends MouseAdapter { private Draw5 draw; public MouseHandler( Draw5 d ) { draw = d; } public void mouseReleased( MouseEvent e ) { draw.setBottomX( e.getX() ); draw.setBottomY( e.getY() ); draw.repaint(); } public void mousePressed( MouseEvent e ) { draw.setTopX( e.getX() ); draw.setTopY( e.getY() ); } } class MouseHandler2 extends MouseMotionAdapter { private Draw5 draw; public MouseHandler2( Draw5 d ) { draw = d; } public void mouseDragged( MouseEvent e ) { draw.setBottomX( e.getX() ); draw.setBottomY( e.getY() ); draw.repaint(); } }
// Exercise 10.34 Solution // Draw6.java // Program draws a shape with the mouse // in different colors; empty or filled import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Draw6 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; private final int RECTANGLE = 0; private final int OVAL = 1; private final int LINE = 2; private final int ROUND = 3; private Color colors[] = { Color.black, Color.blue, Color.red }; public void init() { 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 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 ) { width = Math.abs( topX - bottomX ); height = Math.abs( topY - bottomY ); upperX = Math.min( topX, bottomX ); upperY = Math.min( topY, bottomY ); g.setColor( colors[ color ] ); switch ( shape ) { case RECTANGLE: if ( fillOn ) g.fillRect( upperX, upperY, width, height ); else g.drawRect( upperX, upperY, width, height ); break; case OVAL: if ( fillOn ) g.fillOval( upperX, upperY, width, height ); else g.drawOval( upperX, upperY, width, height ); break; case LINE: g.drawLine( topX, topY, bottomX, bottomY ); break; case ROUND: if ( fillOn ) g.fillRoundRect( upperX, upperY, width, height, 10, 20 ); else g.drawRoundRect( upperX, upperY, width, height, 10, 20 ); break; } } public void setShape( int s ) { shape = s; } public void setColor( int c ) { color = c; } 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; } } class MouseHandler extends MouseAdapter { private Draw6 draw; public MouseHandler( Draw6 d ) { draw = d; } public void mouseReleased( MouseEvent e ) { draw.setBottomX( e.getX() ); draw.setBottomY( e.getY() ); draw.repaint(); } public void mousePressed( MouseEvent e ) { draw.setTopX( e.getX() ); draw.setTopY( e.getY() ); } } class MouseHandler2 extends MouseMotionAdapter { private Draw6 draw; public MouseHandler2( Draw6 d ) { draw = d; } public void mouseDragged( MouseEvent e ) { draw.setBottomX( e.getX() ); draw.setBottomY( e.getY() ); draw.repaint(); } }
|