More on graphics animations
Download
1 / 16

cannonw - PowerPoint PPT Presentation


  • 194 Views
  • Updated 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

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

PowerPoint Slideshow about 'cannonw' - 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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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;


Cannonworld program15 l.jpg
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 l.jpg
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);

}

}


ad