1 / 16

More on Graphics Animations

More on Graphics Animations Shooting cannon game A cannon on the left portion of the user window A target on the right portion of the window The user provides the angle of elevation of the cannon, and fires the cannon The objective is to hit the target. Two Variations of the Game

paul2
Download Presentation

More on Graphics Animations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on Graphics Animations Shooting cannon game • A cannon on the left portion of the user window • A target on the right portion of the window • The user provides the angle of elevation of the cannon, and fires the cannon • The objective is to hit the target.

  2. Two Variations of the Game • The user enters the angle of elevation of the cannon from the command argument line • The program provides a GUI with: • a slider with which the user can change the angle of elevation of the cannon • a button for the user to fire the cannon

  3. First Variation of the Program • The program gets the value of the angle of elevation from the command line arguments CannonGame world = new CannonGame (Integer.valueOf(args[0])); • The angle of the cannon is 45 by default

  4. Graphics Coordinate System • Rendering images in Java (and other windowing systems) is complicated because the coordinate system is “upside down” • Method dy in the CannonWorld program handles the coordinate transformation before any drawing is performed • In method dy, a value for the x-position of the cannon ball is converted by subtracting it from the maximum window size

  5. Rendering Images • Recall that drawing is performed by the method paint, which is invoked implicitly when the application images are rendered, and can be requested explicitly by invoking method repaint • Also recall that drawing commands are provided by the class Graphics, an instance of which is supplied to the paint method.

  6. CannonBall Class • This class inherits class Ball • Recall that, by default, all properties and behavior of class Ball are inherited and are available in the subclass being defined • CannonBall includes additional behavior to the ball by adding the effect of gravity to the ball

  7. Adding User Interaction • In the second variation of the program, user interaction is improved with a GUI • The program provides the ability to dynamically set the angle of the cannon and to fire the cannon several times during an execution of the program

  8. GUI components Added • The normal way to set a varying quantity, such as the angle of elevation of the cannon, is with a scroll bar • The normal way to indicate that an action should start, such as firing the cannon, is with a button.

  9. Inner Classes • This application includes the declaration of two classes within the application class itself • Inner classes are allowed to access their surrounding environment, i.e., the members of the outer class.

  10. Listeners Needed • The CannonWorld program uses two listeners: • The first waits for the button to be pressed. This has the effect of firing the cannon. The listener is declared an instance of the inner class FireButtonListener • The slider that is used to control the value of the angle of elevation of the cannon.

  11. The Slider The slider is an instance of class Scrollbar. Its contructor takes the arguments: • Orientation of the slidebar, horizontal/vertical • The variable associated with the value changed • An initial value for the variable • The range of accepted values

  12. Creating the Slider and Listener The program creates a new slider and a new listener with: slider = new Scrollbar (Scrollbar.VERTICAL, angle, 5, 0, 90); slider.addAdjustmentListener(new ScrollbarListener() );

  13. Slider Listener • The slider listener must implement the AdjustmentListener interface • Unlike the situation with the button, the slider bar must be made available to the listener, so that the current value of the slide bar can be determined

  14. CannonWorld Program import java.awt.*; public class BallWorld extends Frame { public static void main (String [ ] args) { BallWorld world = new BallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball aBall; private int counter = 0;

  15. CannonWorld Program private BallWorld (Color ballColor) { // constructor for new ball world // resize our frame setSize (FrameWidth, FrameHeight); setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (3.0, 6.0); }

  16. CannonWorld Program public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() < 0) || (aBall.x() > FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() < 0) || (aBall.y() > FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(); else System.exit(0); } }

More Related