Assignment 3: WorldImages using javalib library
The javalib library provides the support for the design of interactive games and creating images composed by combining geometric shapes as well as image files.
To use the library, download the javalib.jar and add it to your project the same way you have added the tester library.
On the top of the .java file where the library is used, add the following import statements:
import javalib.colors.*; |
inport javalib.worldimages.*; |
import javalib.worldcanvas.; |
1 Basic geometric shapes
The website for the javalib library at World Images includes an example that shows how to draw all available geometric shapes. Here is a simple example:
// to present a sample of how to define and draw geometric images |
class ExamplesDrawing { |
// a red circle of radius 10 at location (200, 60) |
WorldImage circle = new CircleImage(new Posn(200, 60), 10, |
new Red()); |
|
// a blue disk of radius 10 at location (200, 140) |
WorldImage disk = new DiskImage(new Posn(200, 140), 10, |
new Blue()); |
|
// a yellow rectangle of size 60 x 20 at location (200, 130) |
WorldImage rectangle = new RectangleImage(new Posn(200, 130), |
60, 20, new Yellow()); |
|
// a green line from point (200, 220) to point (280, 230) |
WorldImage line = |
new LineImage(new Posn(200, 220), new Posn(280, 230), |
new Green()); |
|
// all of the above images combined |
// with the first one on the bottom... |
WorldImage combined = |
this.circle.overlayImages( |
this.disk, |
this.rectangle, |
this.line); |
|
// display the image on the canvas (no real tests are possible) |
boolean testDrawImage(Tester t) { |
WorldCanvas c = new WorldCanvas(400, 300); |
return t.checkExpect( |
c.show() && |
c.drawImage(this.combined), true); |
} |
} |
For each shape you need to specify wher will it be drawn on the canvas. You can combine an arbitrary number of images withby invoking the overlayImages method on the first image - the one that will be drawn on the bottom.