// // FILE: CatchFrame.java // AUTHOR: Eugene Wallingford // DATE: 2012/10/20 // COMMENT: This class is adapted from the CannonGameFrame // studied in CS 2530. // // MODIFIED: 2012/10/31 -- Eugene Wallingford // CHANGE : Added an instance variable and a constructor to handle // the image, and modified the listener to create a new // kind of ball. // import java.awt.Button; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.Scrollbar; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; public class CatchFrame extends CloseableFrame { private static final int FrameWidth = 600; private static final int FrameHeight = 400; private static final int PitcherCenterX = 30; private static final int PitcherCenterY = FrameHeight/2; private static final int CatcherCenterX = FrameWidth-50; private static final int CatcherHeight = 40; private static final int DefaultBallSpeed = 10; private static final int MaximumBallSpeed = 100; private Ball ball; private int ballSpeed; private Disk pitcher; private int catcherCenterY; private String message; private Scrollbar speedSlider; private Scrollbar positionSlider; private int catches; private int tosses; private Image ballImage; // --- added public CatchFrame() { super(); setSize ( FrameWidth, FrameHeight ); setTitle( "A Game of Catch" ); catches = 0; tosses = 0; ball = null; ballImage = null; ballSpeed = DefaultBallSpeed; int pitcherRadius = 10; pitcher = new Disk( PitcherCenterX - pitcherRadius, PitcherCenterY - pitcherRadius, pitcherRadius, Color.green ); catcherCenterY = FrameHeight/2; message = "Are you ready to play? Hit the throw button!"; speedSlider = new Scrollbar( Scrollbar.VERTICAL, MaximumBallSpeed - ballSpeed, 5, 1, MaximumBallSpeed+1 ); speedSlider.addAdjustmentListener( new SpeedSliderListener() ); add( "West", speedSlider ); positionSlider = new Scrollbar( Scrollbar.VERTICAL, catcherCenterY, 1, 50, FrameHeight-49 ); positionSlider.addAdjustmentListener( new PositionSliderListener() ); add( "East", positionSlider ); Button pitcher = new Button( "throw the ball" ); pitcher.addActionListener( new PitchButtonListener() ); add( "South", pitcher ); } public CatchFrame( Image im ) // --- added { this(); ballImage = im; } public void paint( Graphics g ) { drawPitcher ( g ); drawCatcher ( g ); drawBall ( g ); writeMessage( g ); } /* ---------------- paint helper methods ---------------- */ protected void drawPitcher( Graphics g ) { pitcher.paint( g ); } protected void drawCatcher( Graphics g ) { int width = 10; int xUpperLeft = CatcherCenterX - width/2; int yUpperLeft = catcherCenterY - CatcherHeight/2; g.setColor( Color.red ); g.fillRect( xUpperLeft, yUpperLeft, width, CatcherHeight ); } protected void drawBall( Graphics g ) { if ( ball == null ) return; ball.move(); ball.paint( g ); if ( ball.x() >= CatcherCenterX ) { int ballY = ball.y(); int targetY = catcherCenterY - CatcherHeight/2; if ( ballY >= targetY && ballY <= (targetY + CatcherHeight) ) recordHit(); else recordMiss(); updateMessage(); ball = null; } repaint(); } protected void writeMessage( Graphics g ) { g.setColor( Color.black ); g.drawString( message, FrameWidth/2-150, 50 ); } /* ------------- helper methods for score ----------------- */ private void recordHit() { catches++; tosses++; } private void recordMiss() { tosses++; } private void updateMessage() { message = "You have caught " + catches + " out of " + tosses + " throws."; } /* ------------------- listener classes ------------------- */ private class PitchButtonListener implements ActionListener { /* * I changed this method to create a different kind of ball. */ public void actionPerformed( ActionEvent e ) { updateMessage(); int ballRadius = 5; int yDirection = (Math.random() < 0.5) ? 1 : -1; double changeInY = 0.5 * DefaultBallSpeed * Math.random() * yDirection; // ball = new BoundedBall( PitcherCenterX - ballRadius, // PitcherCenterY - ballRadius, // 5, ballSpeed, changeInY, // CatchFrame.this ); ball = new BallWithImage( PitcherCenterX - ballRadius, PitcherCenterY - ballRadius, 5, ballSpeed, changeInY, CatchFrame.this, ballImage ); repaint(); } } private class SpeedSliderListener implements AdjustmentListener { public void adjustmentValueChanged( AdjustmentEvent e ) { ballSpeed = MaximumBallSpeed - speedSlider.getValue(); } } private class PositionSliderListener implements AdjustmentListener { public void adjustmentValueChanged( AdjustmentEvent e ) { catcherCenterY = positionSlider.getValue(); System.out.println( catcherCenterY ); repaint(); } } }