more on graphics animations l.
Skip this Video
Loading SlideShow in 5 Seconds..
More on Graphics Animations PowerPoint Presentation
Download Presentation
More on Graphics Animations

Loading in 2 Seconds...

play fullscreen
1 / 16

More on Graphics Animations - PowerPoint PPT Presentation

  • Uploaded on

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'More on Graphics Animations' - paul2

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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
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
first variation of the program
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
graphics coordinate system
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
rendering images
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.
cannonball class
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
adding user interaction
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
gui components added
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.
inner classes
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.
listeners needed
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.
the slider
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
creating the slider and listener
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() );

slider listener
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
cannonworld program
CannonWorld Program

import java.awt.*;

public class BallWorld extends Frame {

public static void main (String [ ] args)


BallWorld world = new BallWorld (; ();


private static final int FrameWidth = 600;

private static final int FrameHeight = 400;

private Ball aBall;

private int counter = 0;

cannonworld program15
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);


cannonworld program16
CannonWorld Program

public void paint (Graphics g) {

// first, draw the ball

aBall.paint (g);

// then move it slightly


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);