These exercises teach one of the most important aspects of OOP, which is the idea of getting other objects to do some work for you. Rather than altering the source code (as we did with BankAccount
) we are going to see how to use existing classes as part of a new class.
First download the source code for . Open the class with the BlueJ editor and select the Documentation view option (instead of Source Code) to read its documentation.
Create a 400x400 SimpleCanvas
object, and notice that it causes a "drawing window" to appear on the screen. The methods of the SimpleCanvas
provide you with an easy way to produce graphical output.
First familiarize yourself with the co-ordinate system:
The command
drawLine(x1,y1,x2,y2)
draws a line from the point with co-ordinates (x1,y1) to the point with co-ordinates (x2,y2).
Using BlueJ, draw lines on the SimpleCanvas
as follows:
drawLine(0,0,399,399);
drawLine(100,100,200,100);
drawLine(0,400,400,0);
What happens if you use coordinate positions off the SimpleCanvas
?
Try doing some "silly" things like
drawLine(100,100,20000,0);
drawLine(-50,-100,600,1000);
and see what happens. Does the SimpleCanvas
object behave sensibly?
Draw some String
s on the SimpleCanvas
at various locations - try drawing one at position (-20,20)
and see what happens.
Learn how to create a new class in BlueJ by starting your own EtchASketch
class. When you use New Class
BlueJ gives you some "sample" instance variables, constructors and methods that do not do anything much. Just delete these so that you start with a blank slate
public class EtchASketch {
}
Copy the instance variables, constructor and four basic methods from the hardcopy lecture notes "Using Other Classes" (Slides 8, 11, 17 and 21 are relevant).
Add a method public void drawRectangle(int width, int height)
to the EtchASketch
class that draws a rectangle of the given width and height with the current drawing point as its top-left corner.
Note: Do NOT alter the SimpleCanvas
class - these additional methods should go into your own EtchASketch
class.
This section contains further optional work for interested students who do not need to do this work, but obviously it will be beneficial. While many of the exam questions come directly from lab exercises, none of them will come from the hot sections (unless the material subsequently appears in the regular sections of later lab sessions).
Using BlueJ's "Use Library Class" facility (on the Tools menu of the main BlueJ window) to create some objects of the class java.awt.Color
, and then change the drawing colour of the SimpleCanvas
, and draw some things in different colours. You will need to choose which constructor to use; the easiest one takes three values of type int which represent the amount of red, blue and green in the colour respectively. These take values between 0 and 255 so to get red, you would use new java.awt.Color(255,0,0)
and to get purple you would use new java.awt.Color(255,0,255)
. Once you have created the colour you want to use, you can then use the setForegroundColour()
method of the SimpleCanvas in order to change the drawing colour.
Work out what commands would be needed to draw this famous optical illusion (which line is longer)
Create a new class, similar to the EtchASketch
class from the lecture "Using Other Classes" and put these commands into a method, so that the client can draw this illusion with one method call.
Look up the class java.util.Random
in the Java API, in particular the method nextInt
.
Create a method that chooses two random points on the SimpleCanvas
and draws a rectangle with those two points at opposite corners.
Create a method that draws lots of random squares (by repeatedly calling the original random square method) - you may look ahead at how to use a loop, or just use cut-n-paste to repeat the line 20 times or so.
Write a new random square method that draws a square with a random position, size and orientation. That is, the square is not necessarily aligned with the vertical and horizontal axes, but is at a random angle to the horizontal axis. You may need to use the library functions java.lang.Math.sin()
and java.lang.Math.cos()
.