Programming in Java on Your Computer
- Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Premium Content - Free Preview
In the next few nodes, we'll review using BlueJ and Object-Oriented programming (OOP), and we'll learn some new stuff too. To get started, open up another project: Shapes. It can be found in examples/shapes in the folder where BlueJ is installed or in the one you downloaded.
The Shapes Project will let you display graphics through its Canvas class. You don't need to know how Canvas works internally to be able to interact with it, which is one of the benefits of OOP.
Before you get started, Click on View > Show Terminal so you can view the code for the actions you do in BlueJ. If the class rectangles have diagonal grey lines on them, click "Compile" to compile them. (You should have already clicked on Options > Record Method Calls.)
- Using the BlueJ interface, create a Circle instance
circle1. You can then inspect it to see the default values of its instance variables.
- Invoke its makeVisible() method. This should cause a window to pop up with a circle in it.
- Invoke other methods on the circle, such as moveRight() and changeColor(). Notice what happens to the circle. You can now inspect the Circle again to see how its variables have changed.
End of Free Content Preview. Please Sign in or Sign up to buy premium content.
catypusOct 12, 9:03 PM
// initialise instance variables
Doesn't work because it cannot find symbol "circle1". How do I make it work?
LearnerooOct 12, 9:08 PM
You need to create
circle1first (See the animation above).
catypusOct 12, 9:24 PM
Hi. When you compile circle, create a new instance of circle and then compile Picture, it deletes your instance of circle. So circle1 is deleted before it compiled Picture.
How do I stop it from deleting my instance?
LearnerooOct 12, 9:31 PM
Only code that it's the actual source code file is saved. As mentioned, double-click on a Class icon to edit its source code.
RinonFeb 7, 3:59 PM
Many issues with compiling? I imported the missing classes but still many other errors in Canvas.java, can't get it to work!