COP3252 Advanced Java Programming - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

COP3252 Advanced Java Programming

Description:

Provide standardized way to handle different types of object groups ... Removes flicker, but produces a large amount of data (each picture is essentially drawn twice) ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 24
Provided by: UoD
Category:

less

Transcript and Presenter's Notes

Title: COP3252 Advanced Java Programming


1
COP3252Advanced Java Programming
  • 15-Apr-08
  • Lecture Set 22
  • Collections, Applet Game

2
Collections
  • Provide standardized way to handle different
    types of object groups
  • Typically, they represent data items that form a
    natural group, such as a poker hand (a collection
    of cards), a mail folder (a collection of
    letters), or a telephone directory (a mapping of
    names to phone numbers).
  • Allows you to manipulate many objects as if they
    were a single object without having to add more
    code to handle the objects

3
Java Collections Framework
  • Group of interfaces, implementations, and methods
    for representing and working with groups of data
    in a consistent way.
  • Interfaces These are abstract data types that
    represent collections. Interfaces allow
    collections to be manipulated independently of
    the details of their representation. In
    object-oriented languages, interfaces generally
    form a hierarchy.
  • Implementations These are the concrete
    implementations of the collection interfaces. In
    essence, they are reusable data structures.
  • Algorithms These are the methods that perform
    useful computations, such as searching and
    sorting, on objects that implement collection
    interfaces. The algorithms are said to be
    polymorphic that is, the same method can be used
    on many different implementations of the
    appropriate collection interface. In essence,
    algorithms are reusable functionality.
  • Main Interfaces
  • Collection, Set, List, Map, Queue and Iterator
  • The collections framework works between different
    APIs --- saving you from having to tailor your
    code to each API.

4
Set Interface
  • Collection of items, having no duplicates, stored
    in any order
  • Contains only methods inherited from Collection
  • Types of sets
  • SortedSet
  • Extends Set
  • Sorted in ascending (natural) order
  • HashSet
  • Implements Set
  • Stores items within a hash table
  • TreeSet
  • General implementation of SortedSet
  • Stores items in a Tree
  • Searched for in ascending order
  • Tree is balanced time it takes to change or
    search the tree increases at a rate of the
    logarithm of the size of the set.

5
Iterators
  • Interface object that goes through the contents
    of a collection, one at a time, using its
    methods.
  • hasnext Method causes interation to continue
  • next Method retrieves each item
  • remove method discards that most recent item
    retrieved.

6
Lists
  • Allows for duplicates
  • Contains items in a defined order
  • Stores it items by integer index, starting with
    zero
  • Extends the Collections interface and adds
  • Positional Access Manipulated elements based on
    their numerical position in the list
  • Search Searched for a specified object in the
    list and returns its numerical position
  • Iteration Extends Iterator semantics to take
    advantage of the lists sequential nature

7
Classes that implement List
  • ArrayList
  • Stores items in an array that is resizable
  • Has a specific capacity that grows automatically
  • LinkedList
  • Data can be added at the beginning or end of the
    list
  • Good for working with stacks, queues, or
    double-ended queues.
  • Must go through each index (starting at beginning
    or end, depending which is closed to the desired
    index) to retrieve a specific index
  • Vector
  • Like a ArrayList but synchronized to work with
    Threading

8
Maps
  • Allows you to create functionality using data
    stored as keys that are mapped to a value
  • Contains keys that map to one value, in any
    order, but whose values can map to multiple keys
  • Does not extend Collection, but can be viewed as
    a Collection

9
Sorted Map
  • Contains keys that map to one value in a
    particular order, but whose values can map to
    multiple keys.
  • Must implement the Comparable interface or use
    Comparator for sorting purposes

10
Comparable
  • This interface imposes a total ordering on the
    objects of each class that implements it.
  • This ordering is referred to as the class's
    natural ordering, and the class's compareTo
    method is referred to as its natural comparison
    method.
  • Lists (and arrays) of objects that implement this
    interface can be sorted automatically by
    Collections.sort (and Arrays.sort).
  • Objects that implement this interface can be used
    as keys in a sorted map or as elements in a
    sorted set, without the need to specify a
    comparator.

11
Comparator
  • A comparison function, which imposes a total
    ordering on some collection of objects.
  • Comparators can be passed to a sort method (such
    as Collections.sort or Arrays.sort) to allow
    precise control over the sort order.
  • Comparators can also be used to control the order
    of certain data structures (such as sorted sets
    or sorted maps), or to provide an ordering for
    collections of objects that don't have a natural
    ordering.

12
Applets Simple game development
  • In this final lecture note section, we will work
    a bit with applets, animation, sound and images
    to create a very basic applet game.

13
Applets The basic structure
  • // import necessary packages
  • import java.applet.
  • import java.awt.
  • // Inherit the applet class from the class Applet
  • public class FirstApplet extends Applet
  • // Now you should implement the following
    methods
  • // init - method is called the first time you
    enter the HTML site with the applet
  • public void init() ...
  • // start - method is called every time you
    enter the HTML - site with the applet
  • public void start() ...
  • // stop - method is called if you leave the
    site with the applet
  • public void stop() ...

14
Simple animation in an applet
  • In this example, we will move a red ball across
    the applet.
  • Note The animation looks horrible --- but we
    will learn how to fix that!
  • See BallAnimate.java and BallAnimate.html

15
Smoothing the Animation
  • Ball was flickering because repaint() completely
    clears and redraws the applet screen.
  • We see a blank screen (for just a milisecond)
  • We can use double buffering to stop this

16
Quick note about repaint()
  • repaint does not call paint()
  • It calls a method named update(), which clears
    the screen completely and then calls paint()
  • This repaints the entire background and then our
    circle

17
Double Buffering
  • Generates a new off-screen image
  • Draw everything on the offscreen image
  • Copy this image over to the existing image
  • Basically, the picture is already painted before
    it is drawn to the screen
  • Removes flicker, but produces a large amount of
    data (each picture is essentially drawn twice)

18
Double Buffering
  • See BallAnimateSmooth.java and BallAnimateSmooth.h
    tml
  • Note This update method can be used
    (basically as-is) in any applet that has animation

19
Changing movement (bouncing the ball)
  • Now we will see how to update our code to make
    the ball bounce between the left and right side
  • We will need to stop the ball from leaving the
    applet area by reversing direction when we reach
    a side

20
Bouncing Ball
  • See BallAnimateSmoothBounce.java and
    BallAnimateSmoothBounce.html
  • We have added a speed variable and updated the
    code to make a bouncing pattern

21
Simple Key Events
  • We can handle key events using methods if of
    Applet
  • This example is going to use the old keyDown
    method
  • The newer (more correct) way is to use
    processKeyEvent

22
Adding sound
  • We can easily add a bounce sound to the game
  • See BallAnimateSmoothKeysAudio.java and
    BallAnimateSmoothKeysAudio.html

23
A pong-like game
  • This next example turns our code into a very
    simple game
  • See BallGame.java and BallGame.html
Write a Comment
User Comments (0)
About PowerShow.com