Using ArrayList in Java for Dynamic Array Management in a Drawing Tool Project, Study Guides, Projects, Research of Communication

An overview of using arraylist in java for managing a dynamic number of asteroids in a drawing tool project. It covers the basics of arraylist, including its resizable nature, methods like add(), get(), remove(), and size(), and the need to cast back to the more detailed type when retrieving objects from the arraylist. The document also discusses the concept of super and this in java, and how they can be used in the context of the asteroid class. Finally, it explains how to create and initialize an arraylist, and how to use it to draw and destroy asteroids.

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 08/05/2009

koofers-user-hfz
koofers-user-hfz 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
LCC 6310
The Computer as an
Expressive Medium
Lecture 9
Overview
Programming questions related to project 2? (due Friday 6pm!)
Programming concepts
super and this
Java SDK classes
Lists
Reading the Java docs
Free time to work on project 2
Project 2
Create your own drawing tool, emphasizing algorithmic
generation/modification/manipulation. Explore the balance of control
between the tool and the person using the tool. The tool should do
something different when moving vs. dragging (moving with the
mouse button down). The code for your tool should use at least one
class.
Revisiting our example
So far we have a rocket that flies around in a field of asteroids and
fires missiles
Now we want our missiles to blow up asteroids
This means we need a variable number of asteroids
How do we do this with an array? (They have a fixed size)
We can use a special Java object called an ArrayList!
We'll also need to figure out when we have a collision
pf3
pf4
pf5

Partial preview of the text

Download Using ArrayList in Java for Dynamic Array Management in a Drawing Tool Project and more Study Guides, Projects, Research Communication in PDF only on Docsity!

LCC 6310

The Computer as an

Expressive Medium

Lecture 9

Overview

Programming questions related to project 2? (due Friday 6pm!)Programming concepts

super and thisJava SDK classes
ListsReading the Java docs

Free time to work on project 2

Project 2

Create your own drawing tool, emphasizing algorithmicgeneration/modification/manipulation. Explore the balance of controlbetween the tool and the person using the tool. The tool should dosomething different when moving vs. dragging (moving with themouse button down). The code for your tool should use at least oneclass.

Revisiting our example

So far we have a rocket that flies around in a field of asteroids andfires missilesNow we want our missiles to blow up asteroids

This means we need a variable number of asteroidsHow do we do this with an array? (They have a fixed size)We can use a special Java object called an ArrayList!We'll also need to figure out when we have a collision

The Java SDK

(Java SDK = Java Software Developer's Kit)Java comes with thousands of classes in the Java Platform APIDocumentation is available on Sun’s website

http://java.sun.com/j2se/1.4.2/docs/api/index.htmlNote that Processing supports Java 1.4.2 features (not yet 1.5)You may want to download the documentation…

Let’s look at ArrayList

ArrayList

ArrayList is a resizeable list

This means we can add and delete things from the list without worryingabout declaring the size up front

The main methods we care about are add(), get(), remove(), and size()

http://java.sun.com/j2se/1.4.2/docs/api/index.html

Steps for using ArrayList

Declare a variable of type ArrayListCreate a new ArrayList and assign it to the variableCall add(), get(), remove() and size() on the ArrayList as you need them

Parents and children

Remember that we declared a child class ArmedRocket whose parentwas Rocket, and remember that classes are types

So ArmedRocket is a type and Rocket is a type

So, here are some legal assignments:

ArmedRocket
r1 = new
ArmedRocket(50, 60, 0);
Rocket
r2 =
new Rocket(50,
Rocket
r3 =
new ArmedRocket(50, 60, 0);

But this one is illegal:

ArmedRocket
r4 = new
Rocket(50, 60, 0);

Same goes for method arguments as well…

Using ArrayList.add()

The argument type of the add method is Object

Object is the parent class of all classesWith an object argument type, you can pass in an object of any class

So, let's declare our asteroids ArrayList…

ArrayList
asteroids;

And then initialize our asteroids…

asteroids
new
ArrayList();
for
(int i
i
NUM_ASTEROIDS;
i++)
asteroids.add(new
Asteroid());

Let's try this in Processing…

Destroying asteroids

When a missile hits an Asteroid, we need to destroy it

This was the whole reason for using ArrayList!Big asteroids turn into two small asteroidsSmall asteroids disappear void destroy(ArrayList asteroids) {
asteroids.remove(this);if (large) {
asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis));asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis));

Hmm… we've added some new things here:

We need to store the size of the Asteroid (is it large or small?)We need to be able to create new smaller Asteroids at a specifiedposition

Asteroid size

Since we only want two sizes (big and small) we can use a boolean:

boolean
large;

We also need to put something in the Asteroid's drawMe() method tomake sure that it will get drawn at the right size

Let's make small Asteroids at half-scale if (!large)
scale(0.5);

Now let's think about how to create the new smaller Asteroids at agiven (x,y) position…

super and this

this is a special variable keyword that always refers to the currentinstance (object)

Useful in methods to refer to yourselfthis.method() – calls a method on yourself (but normally you justdirectly call the method)this() – calls a constructor on yourself (useful for one version of aconstructor to call another)this.someVariable – refers to the class variable (useful if a variable oflocal scope has the same name as some class variable)

super is a special variable that always refers to the superclass portionof an object (the object cast into it’s superclass)

super.method() – calls a method on the superclasssuper() – calls a constructor on the superclass

A new Asteroid constructor

Asteroid(boolean

isLarge,

float

initialX,

float

initialY,

long

previousDrawTime)

{

//

set

the

initial

x

and

y

pos

xPos

=

initialX;

yPos

=

initialY;

//

get

a

random

orientation

for

this

asteroid

rotation

=

random(0,

TWO_PI);

//

get

the

asteroid's

velocity

in

x

and

y

directions

//

based

on

its

orientation

and

size

(bigger

moves

slower)

if

(isLarge)

{

velocityX

=

sin(rotation)*10;

velocityY

=

-cos(rotation)*10;

}

else

{

velocityX

=

sin(rotation)*25;

velocityY

=

-cos(rotation)*25;

} large

=

isLarge;

lastDrawMillis

=

previousDrawTime;

}

But what about the old constructor?

We still want to be able to create an Asteroid in a random location

So we want to keep our original constructor without parameters, but weneed to update it to take into account the asteroid size (they start big)We can just use this to call our new constructor and let it do all the work Asteroid() {
// create an asteroid at a random location on the displaythis (true, random(0, XSIZE), random(0, YSIZE), 0);

Let's assemble all these Asteroid changes together and put them inProcessing…

The size variable…

Storing our size

boolean
large;

Drawing at the correct size

after translating
and
rotating,
we
should
make
sure
to
scale
ourselves too
if
(!large)
scale(0.5);

The constructors…

Asteroid()

{

this

(true,

random(0,

XSIZE),

random(0,

YSIZE),

0);

} Asteroid(boolean

isLarge,

float

initialX,

float

initialY,

long

previousDrawTime)

{

xPos

=

initialX;

yPos

=^

initialY;

rotation

=

random(0,

TWO_PI);

if

(isLarge)

{

velocityX

=

sin(rotation)*10;

velocityY

=

-cos(rotation)*10;

}

else

{

velocityX

=

sin(rotation)*25;

velocityY

=

-cos(rotation)*25;

} large

=

isLarge;

lastDrawMillis

=

previousDrawTime;

}

The destroy method…

void destroy(ArrayList asteroids) {
asteroids.remove(this);if (large) {
asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis));asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis));