// DrawJPanel4Tester.java // Applet tests the JPanel used as a dedicated drawing area // APPLET CODE="DrawJPanel4Tester" HEIGHT=500 WIDTH=700 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DrawJPanel4Tester extends JApplet implements ActionListener { private Container c; private DrawJPanel4 pD; private JPanel pGUITop, pGUIBottom; private JLabel wLabel, hLabel; private final int NUMBALLS = 3; private String colorNames[ ] = { "Red", "Green", "Blue" }; private JLabel suspendLabels[ ]; private JButton suspendButtons[ ]; private JTextField wField, hField; private int wid, hgt; private boolean toggle[ ]; // keeps track of whether to change label colors private Font fontL; public void init( ) { c = getContentPane( ); pD = new DrawJPanel4( ); pGUITop = new JPanel( ); pGUITop.setLayout( new GridLayout( 1, 4, 5, 5 ) ); wLabel = new JLabel( "Enter the width:" ); wField = new JTextField( "50", 10 ); wField.addActionListener( this ); hLabel = new JLabel( "Enter the height:" ); hField = new JTextField( "50", 4 ); hField.addActionListener( this ); // add GUI components to pGUITop panel pGUITop.add( wLabel ); pGUITop.add( wField ); pGUITop.add( hLabel ); pGUITop.add( hField ); pGUIBottom = new JPanel( ); pGUIBottom.setLayout( new GridLayout( 2, 3, 5, 5 ) ); fontL = new Font( "TimesRoman", Font.BOLD, 24 ); suspendLabels = new JLabel[ NUMBALLS ]; suspendButtons = new JButton[ NUMBALLS ]; toggle = new boolean[ NUMBALLS ]; for ( int i = 0; i < NUMBALLS; i++ ) { suspendLabels[ i ] = new JLabel( colorNames[ i ].concat( " Ball" ) ); suspendLabels[ i ].setBackground( Color.green ); suspendLabels[ i ].setForeground( Color.black ); suspendLabels[ i ].setFont( fontL ); suspendLabels[ i ].setOpaque( true ); suspendLabels[ i ].setHorizontalAlignment( SwingConstants.CENTER ); // suspendLabels[ i ].setPreferredSize( new Dimension( 150, 40 ) ); suspendButtons[ i ] = new JButton( "Click to Suspend" ); suspendButtons[ i ].setBackground( Color.black ); suspendButtons[ i ].setForeground( Color.white ); suspendButtons[ i ].setOpaque( true ); suspendButtons[ i ].addActionListener( this ); } // add GUI components to pGUIBottom panel for ( int i = 0; i < NUMBALLS; i++ ) { pGUIBottom.add( suspendLabels[ i ] ); } for ( int i = 0; i < NUMBALLS; i++ ) { pGUIBottom.add( suspendButtons[ i ] ); } // add the panels to the container c c.add( pGUITop, BorderLayout.NORTH ); c.add( pGUIBottom, BorderLayout.SOUTH ); c.add( pD, BorderLayout.CENTER ); wid = Integer.parseInt( wField.getText( ) ); hgt = Integer.parseInt( hField.getText( ) ); pD.drawingStuff( wid, hgt ); } public void actionPerformed( ActionEvent e ) { if ( e.getSource( ) == wField || e.getSource( ) == hField ) { wid = Integer.parseInt( wField.getText( ) ); hgt = Integer.parseInt( hField.getText( ) ); pD.drawingStuff( wid, hgt ); } for ( int i = 0; i < NUMBALLS; i++ ) { if ( e.getSource( ) == suspendButtons[ i ] ) { toggle[ i ] = pD.flip( i ); // send message to JPanel to toggle suspended suspendLabels[ i ].setBackground( toggle[ i ] ? Color.red : Color.green ); suspendButtons[ i ].setText( "Click to " + ( toggle[ i ] ? "Resume" : "Suspend" ) ); } } } public synchronized void stop( ) { pD.stop( ); // call JPanel stop method } }