CS 110 Elements of Computing

concepcionsockΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 8 χρόνια και 11 μήνες)

385 εμφανίσεις

CS 280

Data Structures

Professor John Peterson

Quiz 4 Recap

Consider the following array: {2, 6, 7, 3, 4, 1, 5, 9}.
Draw this in tree form and then show the array after
each exchange in stage 1 of heapsort. For stage 2,
show the array NOT in tree form but as an array at
the end of each iteration of the loop after the swap
and the call to heapify

Suppose we sort an array of 1,000,000 items. What is
the maximum number of calls to heapify generated by
heapify(0, s, 1000000)? You can use round numbers

off by 1 is good enough.

New Timetable

Projects due Monday

Homework due Friday

This Week

We have a big “todo” this week

understanding how to create animations
in Java.


we’re not using the ACM library!

No homework this week so you can
concentrate on coding.

I will go through the theory here and
then let you work on the project in

How to do Animation

Need a JFrame at the top level (this is
something you can select in NetBeans
when you create a file)

The JFrame has two views: code and
design. You can switch between these.
Code shows the actual code with
protected areas, design shows what
happens when the code runs.

How to do Animation

Top level variables have to go in the

There’s a special place for initialization
if you want to do something at startup

You need to set up a timer to wake the
program up to display the next frame of

The timer calls a “step()” method to
change the animation state

How to do Animation

Objects which change during the
animation need a custom paint method.

You need to subclass the Java objects
to add this

You need to use custom creation code in
Netbeans to make this work

Objects like buttons can talk to the
program through events

Drag and drop to create objects

How to do Animation

Widgets to know:

JPanel: a place to draw pictures

JButton: a push button that sends
“ActionPerformed” when you push it

JLabel: a place to put text

Graphics: this is a class used to draw

it’s part of the repaint

How to do Animation


Use setColor to choose a paint color

Use fillRectangle to create rectangles
(that’s all we need in this class)

(0,0) is at the top left, coordinates are
in pixels


setText is needed to change the text

Set the font at design time

Your Program

import all sorts of library stuff

Main class (extends JFrame)

Program level vars, including timer

Classes to add graphics
to GUI objects


Place initializers here (timer too)

Step() method to change

Event handlers and helpers

Into the Wiki

Start your Netbeans now

Todays stuff doesn’t need the previous