The purpose of this exercise is:
|
By now, you should have become familiar with netbeans. While netbeans supports BlueJ projects, it doesn't do so fully; the first thing that we will do is to change the project to a Netbeans native project. This will also copy all the files so giving you something to go back to if you need to 1.
|
There is a further issue that we should deal with. The Shapes
package has been
somewhat naughty; it's using what is called the default package. This is
considered bad practice. The problem is this; a name like "Circle" is rather
too common and might occur anywhere. Java's package system adds more
information to prevent these name clashes 2. In fact, the Shapes
package has
a name class; there is another class called Canvas
in Java. You can read more
about packages if you wish. We will create a few more of these as time goes
on.
|
If you have done it right, all your classes should now be in the shapes directory. Check with the demonstrator if you are not sure.
|
We are now ready to do something new. In the lectures, you should have heard about control flow constructs; these are ways in which you affect the way that the program runs. You can read the details of them all if you wish. In this exercise, we are going to mostly focus on the for loop. In this case, we're going to do an animation of sorts 3.
First, we want to create a new package; the functionality is going to depend
on that in the shapes
package, but it's not critical for shapes
. So, it
should be in a different package. Let's create a new package.
|
Now, we are going to animate a circle that starts off small and grows over time. For this, we need a bit of magic code.
Put these methods into private void animateStep() { Canvas.getCanvas().wait( 50 ); } private void eraseObject(Object erased) { Canvas.getCanvas().erase(erased); } You should now have this. |
Now, we are going to create a new method or operation much like you have seen
on the shapes Classes. In this case, it will be called animateGrowingCircle
.
Add the following declaration to Grow.java .
public void animateGrowingCircle(){ }We are going to instantiate (make a new object) of the Circle class.
Change your code to look like this.
public void animateGrowingCircle(){ Circle c = new Circle(); c.makeVisible(); } |
Packages in Java are meant to separate out different functionality. But this
has caused us a problem. Java now doesn't know what Circle you mean — it's
looking for Circle.java
in the same package and it can't find it. Likewise,
for the Canvas
class.
|
Okay, we are now nearly ready to try out the for
loop. You have an object of
the Circle
class. You should know how to change it's size. And you should have
read how to use a for
loop.
animateStep();
eraseObject( c ); |
Okay, so we now should have a proper method. Hopefully, netbeans will not be signalling any errors. So, how do we test this. We can't run this Class directly. Instead, we will use a test class; something whose sole function is to test something else 4.
Add the following code into GrowTest.java .
public static void main(String[] args) throws InterruptedException { Grow g = new Grow(); g.animateGrowingCircle(); Thread.sleep(2000); System.exit(0); }Now with GrowTest.java still selected, hit Shift-F6 (right-click Run
"GrowTest.java"). With a bit of luck, it should all work.
|
So, we now have an animated circle growing. Now, we are going to try something somewhat more complex.
Using similar techniques as before, implement the body of this method
public void animateGrowingThenShrinkingSquare(){ // you code in here } In this case, it should animate a square which should grow from 0 to 300 and
then shrink again. You will need two public static void main(String[] args) throws InterruptedException { Grow g = new Grow(); g.animateGrowingCircle(); Thread.sleep(2000); g.animateGrowingThenShrinkingSquare(); Thread.sleep(2000); System.exit(0); } |
This is fine, but the code is a bit limited. What happens if we want to make the Circle or Square grow quicker? What happens if we want to change the maximum size? We can't change this because we have hard coded these values. Let's change the animation step first. We will do this by introducing a new variable.
private void animateStep() { Canvas.getCanvas().wait(getAnimationStep()); }
public static void main(String[] args) throws InterruptedException { Grow g = new Grow(); g.setAnimateStep( 200 ); g.animateGrowingCircle(); Thread.sleep(2000); g.setAnimateStep( 0 ); g.animateGrowingThenShrinkingSquare(); Thread.sleep(2000); System.exit(0); }
|
Get Grow.java and GrowTest.java signed off. Together these make up the
coursework for this part of the exercise.
|
1. Going back to old code that was working is something that tends to happen a lot with programming. In the short term, keeping regular "check-points" of your code — copies made when it's all working — is a good idea. A much better way of doing this is to use Version Control systems; I use these all the time (these web pages are versioned), but they are a little complex to get started with. The advanced OO module shows how to use these.
2. I used to work at Manchester University. I still have an email address there. Unfortunately, while my name is relatively rare there is another Phillip Lord there; he is a building manager at the University. I still regularly get email about cleaning fluids. The problem isn't so bad know, because he is Phillip Lord at Manchester and I am Phillip Lord at Newcastle. The problem would be much more common if we just used firstname.lastname for the entire world.
3. I suspect that Pixar will have little to worry about.
4. There are much better ways of doing this, using a Test Harness, often also know as Unit testing. Rather like version control, it's very good, but a little complicated, so we will not use it here. It will be covered in the advanced OO module also.