Interactive Picture


Premium Content - Free Preview

The Picture Class you created may look nice, but it doesn't do much besides create a static picture. In Object-Oriented Programming, you can have Objects interact with each other to do more exciting things than just sit there. For example, it would be cool if you could change the colors or move multiple Objects with one action. Currently, your code probably looks like the code below, where each Object is created and then forgotten:

 public class Picture
{

  public Picture()
  {
     Circle circle1 = new Circle(30, 30, 60, "blue");
     Circle circle2  = new Circle(30, 60, 60, "blue");
     Triangle triangle1 = new Triangle();
     //continued code...
   }

}

The Circles are created, but they won't be available to other methods in your program. In fact, if you inspect your Picture Object on the bench, you'll see it has nothing to show. This is the first thing that should be fixed.


Instance Variable Task

  1. Declare all the shapes that you will use as instance variables at the beginning of your Class code. You might as well give them descriptive names this time. For example: Circle eye1;
  2. Since you just declared the types, your constructor shouldn't repeat that. Remove the type declarations from the constructor so it just assigns each variable to a new Object. For example: eye1 = new Circle(30, 30, 60, "blue");

The Picture cannot do anything more yet, but at least it now has some accessible data. If you inspect a Picture Object again, you'll now be able to see (and inspect) each instance variable you created:

Picture-inspection-window


Simple Method

Now that Picture's shapes are accessible, you can create a method that changes the color of all its Circles:

  1. Create a method changeCirclesColor that takes in a String color as a parameter.
  2. Invoke the changeColor method on each one of Picture's Circles and pass in color as a parameter.
View Code

You should now be able to create a Picture and change the colors of all its circles at once.


End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Comments

  • Circle[] circles = new Circle[3];
    circles[0] = eye1;
    circles[1] = eye2;
    Result in ... ']' expected ... error in circles[0] definition ?

  • @Honzis, you should move your code to the constructor.

  • Really cool stuff. At first, I wasn't crazy about using BlueJ, but everything is starting to piece together very nicely. Thanks!

  • public void moveCirclesRight()
    { 
        for (int i=0; i < circles.length; i++)
        {
            circles[i].moveRight();
        }
    }
    

    java.lang.NullPointerException
    at circles[i].moveRight();

  • How do I get a check on this? You've turned me into a slave for completion checks...

    boolean lessonComplete;
    public void gimmeCheck(){
    lessonComplete = true;
    }
    interactivePicture.gimmeCheck();

  • Currently there's no online question/check for this page since you do the coding on your computer.

  • How do I get the checkbox for this page? I'm finished the unit... but I can't find any quiz on this page to finish. I just can't stand not having my checkbox checked!!!

  • Just added a simple question so you can mark your progress.

Contact Us
Sign in or email us at [email protected]