// RectangleApplet.java (c) 2005-2006 Kari Laitinen // http://www.naturalprogramming.com // 2005-06-07 File created. // 2006-10-23 Last modification. // This program shows how objects of classes JScrollBar, JRadioButton, and // ButtonGroup can be used in an applet. import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; public class RectangleApplet extends JApplet implements AdjustmentListener, ItemListener { int applet_width, applet_height ; int rectangle_width, rectangle_height ; int rectangle_center_point_x, rectangle_center_point_y ; JScrollBar width_scrollbar, height_scrollbar ; Color rectangle_color = Color.cyan ; JRadioButton cyan_button, magenta_button, green_button, lightGray_button ; ButtonGroup color_selection_buttons = new ButtonGroup() ; public void init() { setLayout( new BorderLayout() ) ; applet_width = getSize().width ; applet_height = getSize().height ; rectangle_center_point_x = applet_width / 2 ; rectangle_center_point_y = applet_height / 2 ; rectangle_width = applet_width / 4 ; rectangle_height = applet_height / 4 ; height_scrollbar = new JScrollBar( Scrollbar.VERTICAL, rectangle_height, // initial value 10, // extent 0, // minimum value applet_height ) ; // maximum value height_scrollbar.addAdjustmentListener( this ) ; add( "East", height_scrollbar ) ; width_scrollbar = new JScrollBar( Scrollbar.HORIZONTAL, rectangle_width, 10, 0, applet_width ) ; width_scrollbar.addAdjustmentListener( this ) ; JPanel panel_for_buttons_and_scrollbar = new JPanel() ; panel_for_buttons_and_scrollbar.add( width_scrollbar ) ; cyan_button = new JRadioButton( "cyan", true ) ; magenta_button = new JRadioButton( "magenta", false ) ; green_button = new JRadioButton( "green", false ) ; lightGray_button = new JRadioButton( "lightGray", false ) ; color_selection_buttons.add( cyan_button ) ; color_selection_buttons.add( magenta_button ) ; color_selection_buttons.add( green_button ) ; color_selection_buttons.add( lightGray_button ) ; cyan_button.addItemListener( this ) ; magenta_button.addItemListener( this ) ; green_button.addItemListener( this ) ; lightGray_button.addItemListener( this ) ; panel_for_buttons_and_scrollbar.add( cyan_button ) ; panel_for_buttons_and_scrollbar.add( magenta_button ) ; panel_for_buttons_and_scrollbar.add( green_button ) ; panel_for_buttons_and_scrollbar.add( lightGray_button ) ; add( "South", panel_for_buttons_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_button ) { rectangle_color = Color.cyan ; } else if ( event.getItemSelectable() == magenta_button ) { rectangle_color = Color.magenta ; } else if ( event.getItemSelectable() == green_button ) { rectangle_color = Color.green ; } else if ( event.getItemSelectable() == lightGray_button ) { rectangle_color = Color.lightGray ; } repaint() ; } public void paint( Graphics graphics ) { super.paint( graphics ) ; graphics.setColor( rectangle_color ) ; graphics.fillRect( rectangle_center_point_x - rectangle_width / 2, rectangle_center_point_y - rectangle_height / 2, rectangle_width, rectangle_height ) ; graphics.setColor( Color.black ) ; graphics.drawRect( rectangle_center_point_x - rectangle_width / 2, rectangle_center_point_y - rectangle_height / 2, rectangle_width, rectangle_height ) ; graphics.drawString( "Width: " + rectangle_width + " Height: " + rectangle_height, 20, 20 ) ; } }