// RectangleAWTApplet.java (c) 2005 Kari Laitinen // 2005-06-07 File created. // 2006-10-03 Last modification. // This program is a traditional, non-swing, Java applet that // works in the same way as RectangleApplet.java // This program shows how objects of classes Scrollbar, Checkbox, and // CheckboxGroup can be used in an applet. import java.awt.* ; import java.awt.event.* ; public class RectangleAWTApplet extends java.applet.Applet implements AdjustmentListener, ItemListener { int applet_width, applet_height ; int rectangle_width, rectangle_height ; int rectangle_center_position_x, rectangle_center_position_y ; Scrollbar width_scrollbar, height_scrollbar ; Color rectangle_color = Color.cyan ; Checkbox cyan_checkbox, magenta_checkbox, green_checkbox, lightGray_checkbox ; CheckboxGroup color_selection_checkboxes = new CheckboxGroup() ; public void init() { setLayout( new BorderLayout() ) ; applet_width = getSize().width ; applet_height = getSize().height ; rectangle_center_position_x = applet_width / 2 ; rectangle_center_position_y = applet_height / 2 ; rectangle_width = applet_width / 4 ; rectangle_height = applet_height / 4 ; height_scrollbar = new Scrollbar( Scrollbar.VERTICAL, rectangle_height, 0, 0, applet_height ) ; height_scrollbar.addAdjustmentListener( this ) ; add( "East", height_scrollbar ) ; width_scrollbar = new Scrollbar( Scrollbar.HORIZONTAL, rectangle_width, 0, 0, applet_width ) ; width_scrollbar.addAdjustmentListener( this ) ; Panel panel_for_checkboxes_and_scrollbar = new Panel() ; panel_for_checkboxes_and_scrollbar.add( width_scrollbar ) ; cyan_checkbox = new Checkbox( "cyan", true, color_selection_checkboxes ) ; magenta_checkbox = new Checkbox( "magenta", false, color_selection_checkboxes ) ; green_checkbox = new Checkbox( "green", false, color_selection_checkboxes ) ; lightGray_checkbox = new Checkbox( "lightGray", false, color_selection_checkboxes ) ; cyan_checkbox.addItemListener( this ) ; magenta_checkbox.addItemListener( this ) ; green_checkbox.addItemListener( this ) ; lightGray_checkbox.addItemListener( this ) ; panel_for_checkboxes_and_scrollbar.add( cyan_checkbox ) ; panel_for_checkboxes_and_scrollbar.add( magenta_checkbox ) ; panel_for_checkboxes_and_scrollbar.add( green_checkbox ) ; panel_for_checkboxes_and_scrollbar.add( lightGray_checkbox ) ; add( "South", panel_for_checkboxes_and_scrollbar ) ; } public void adjustmentValueChanged( AdjustmentEvent event ) { if ( event.getSource() == height_scrollbar ) { rectangle_height = height_scrollbar.getValue() ; } else if ( event.getSource() == width_scrollbar ) { rectangle_width = width_scrollbar.getValue() ; } repaint() ; } public void itemStateChanged( ItemEvent event ) { if ( event.getItemSelectable() == cyan_checkbox ) { rectangle_color = Color.cyan ; } else if ( event.getItemSelectable() == magenta_checkbox ) { rectangle_color = Color.magenta ; } else if ( event.getItemSelectable() == green_checkbox ) { rectangle_color = Color.green ; } else if ( event.getItemSelectable() == lightGray_checkbox ) { rectangle_color = Color.lightGray ; } repaint() ; } public void paint( Graphics graphics ) { graphics.setColor( rectangle_color ) ; graphics.fillRect( rectangle_center_position_x - rectangle_width / 2, rectangle_center_position_y - rectangle_height / 2, rectangle_width, rectangle_height ) ; graphics.setColor( Color.black ) ; graphics.drawRect( rectangle_center_position_x - rectangle_width / 2, rectangle_center_position_y - rectangle_height / 2, rectangle_width, rectangle_height ) ; graphics.drawString( "Width: " + rectangle_width + " Height: " + rectangle_height, 20, 20 ) ; } }