/* --- CSU213 Spring 2006 Lecture Notes --------- Copyright 2006 Viera K. Proulx Lecture 10: Libraries: Let's Make a Deal Goals: - Learning to use Java class libraries - Drawing simple images using the library methods Introduction: When writing programs we want to make sure that we can use programs others have written already. With Java class based design, this should be easy. Every class defines clearly what are the fields ot contains, the types of the fields, and, most importantly, what are the signatures and purpose statements for all methods. So, for example, if I know that there is a class Person hat represents a person's name and date of birth, I can design a book class with an author field of the type Person - using all the methods already defined there, such as bornInMonth(int month) or sameName(String name). For every programming language people build libraries of programs that others can use. The API for the libraries (Application Programmer's Interface) describes the classes available in a given library, and for each class the fields the programmer needs to know about, as well as all methods that the programmer can invoke. The documentation includes the purpose statements, and for more complex methods it may come with detailed examples of use. One assumes that the libraries are well designed and properly tested -- but this judgment should only be made when we know we can trust the programmer of such library. The first library we will consists of classes that make it easy for us to draw simple pictures. The classes represent the canvas to draw on, the colors we can use, and a class that represents a point location on the canvas. Later we will include additional class to represent a world for animation controlled by the system clock and user interactions with the keyboard. These classes are a part of the 'draw' library. To tell Java we require the use of a given library, we include at the beginning (before our first class definition) the line: import draw.*; This makes all the classes in the 'draw' library available to us. If we wanted only some of these classes, we could import them individually: import draw.Canvas; import draw.Color; import draw.Red; We first want to know what are the fields and methods in each of these classes. Classes that represent color have no fields and the constructor takes no arguments. The main class in the Canvas class. It defines the following methods: // create a visible canvas of the given size boolean start(int width, int height); // hide the canvas boolean stop(); // draw a circle at the given position, radius and color // on this canvas boolean drawCircle(Posn p, int r, Color c); // draw a solid disk at the given position, radius and color // on this canvas boolean drawDisk(Posn p, int r, Color c); // draw a rectangle at the given position, width, height, // and color on this canvas boolean drawRect(Posn p, int width, int height, Color c); // draw a line at the between the two given positions, and // of the given color on this canvas boolean drawLine(Posn p0, Posn p1, Color c); // draw a string at the given position on this canvas boolean drawString(Posn p, String s); // clear a circle at the given position, radius and color // on this canvas boolean clearCircle(Posn p, int r, Color c); // clear a solid disk at the given position, radius and color // on this canvas boolean clearDisk(Posn p, int r, Color c); // clear a rectangle at the given position, width, height, // and color on this canvas boolean clearRect(Posn p, int width, int height, Color c); // clear a line at the between the two given positions, and // of the given color on this canvas boolean clearLine(Posn p0, Posn p1, Color c); The Posn class has two fields, 'int x' and 'int y' and no methods. With this information we can start drawing - as can be seen in the following example: */ import draw.*; class TreeDrawing { TreeDrawing() {} // the canvas for showing the drawings Canvas c = new Canvas(); // The method to show the initial world // this.c.start has to be run once before anything can be drawn on canvas. // It should not be run again until the canvas is closed using the stop() // method. boolean showTree(){ return this.c.start(200, 300) && // draw the tree leaves this.c.drawDisk(new Posn(100, 150), 80, new Green()) && // draw the tree trunk this.c.drawRect(new Posn(90, 230), 20, 120, new Black()) && // draw the sun this.c.drawDisk(new Posn(30, 30), 20, new Yellow()); } } /* To explore the behavior, run the following in the interactions window: > TreeDrawing td = new TreeDrawing(); > td.showTree() > td.c.stop() > td.showTree() > td.c.drawString(new Posn(80, 150), "Hello"); */