Shapes
Circle(int radius, String color)CircleOutline(int radius, String color)Rect(int width, int height, String color)RectOutline(int width, int height, String color)Triangle(int width, int height, String color)TriangleOutline(int width, int height, String color)Text(String content, int size, String color)
A size of 0 makes an empty picture, such as new Rect(0, 0, "white"): it takes up no room, so it is a handy starting point for building a picture up piece by piece. A negative size is an error.
Colors
"red", "green", "blue", "yellow", "orange", "pink", "magenta", "cyan", "purple", "brown", "black", "white", "gray", "grey", "lightgray", "darkgray"
"transparent" paints nothing, but takes up its size like any other picture.
Combinations
Beside(Pict left, Pict right)Above(Pict top, Pict bottom)Below(Pict bottom, Pict top)Overlay(Pict front, Pict back)OverlayAt(Pict front, int x, int y, Pict back)OverlayAtAlign(Pict front, int x, int y, String align, Pict back)—alignis"center","bottom-left","bottom-right","top-left"or"top-right"AddLine(Pict base, int x1, int y1, int x2, int y2, String color)AddLine(Pict base, int x1, int y1, int x2, int y2, int thickness, String color)
Transformations
Rotate(Pict pict, int degrees)
Pict Methods
int width()int height()Pict beside(Pict other)– this picture on the left,otheron the rightPict above(Pict other)– this picture on top,otherunder itPict below(Pict other)– this picture on the bottom,otheron top of itPict overlay(Pict other)– this picture in front,otherbehind itPict overlayAt(int x, int y, Pict other)Pict overlayAtAlign(int x, int y, String align, Pict other)Pict addLine(int x1, int y1, int x2, int y2, String color)Pict addLine(int x1, int y1, int x2, int y2, int thickness, String color)Raster render()Pict rotate(int degrees)
Fractional Sizes and Angles
Every size, position, thickness, and angle above also accepts a double, rounded to the nearest whole pixel or degree. A picture stores and prints whole numbers either way. A measurement of NaN or infinity is an error.
Circle(double radius, String color)CircleOutline(double radius, String color)Rect(double width, double height, String color)RectOutline(double width, double height, String color)Triangle(double width, double height, String color)TriangleOutline(double width, double height, String color)Text(String content, double size, String color)OverlayAt(Pict front, double x, double y, Pict back)OverlayAtAlign(Pict front, double x, double y, String align, Pict back)AddLine(Pict base, double x1, double y1, double x2, double y2, String color)AddLine(Pict base, double x1, double y1, double x2, double y2, double thickness, String color)Rotate(Pict pict, double degrees)Pict overlayAt(double x, double y, Pict other)Pict overlayAtAlign(double x, double y, String align, Pict other)Pict addLine(double x1, double y1, double x2, double y2, String color)Pict addLine(double x1, double y1, double x2, double y2, double thickness, String color)Pict rotate(double degrees)
Rasters
A Raster is a rectangle of pixels, each a Color from java.awt (start the file with import java.awt.Color;). x counts across from the left and y counts down from the top, both starting at 0.
Raster(Color[][] pixels)– one inner array per row, top row first; every row must have the same length, and no pixel may benull. The raster copies the array, so changingpixelsafterward does not change it.Raster(int width, int height)– every pixel transparentint width()– the length of each rowint height()– the number of rowsColor colorAt(int x, int y)– the pixel in rowy, columnxvoid setColorAt(int x, int y, Color color)FromRaster(Raster raster)– aPictthat shows the raster’s pixels
Two rasters are equals when they have the same size and the same color at every pixel, however they were made. Pict.render() turns any picture into a raster.
Java Colors
Color.RED,Color.GREEN,Color.BLUE,Color.WHITE,Color.BLACK, and the other names injava.awt.Colornew Color(int r, int g, int b)– red, green, and blue amounts from0to255new Color(int r, int g, int b, int alpha)–alpha0is fully transparent,255fully solid
A Color prints as java.awt.Color[r=…,g=…,b=…], without its alpha, so a transparent pixel prints the same as black.
Showing and Saving
ImageIO_.show(Pict p)ImageIO_.show(Raster r)ImageIO_.save(Pict p, String filename)ImageIO_.save(Raster r, String filename)ImageIO_.save(Pict[] frames, String filename)— an animated GIF, 100 ms per frame, loopingImageIO_.save(Pict[] frames, int delayMs, String filename)ImageIO_.save(Pict[] frames, int delayMs, boolean loop, String filename)ImageIO_.load(String filename)– aRaster; a file you saved wins over one of the book’s images with the same nameImageIO_.render(Pict p)
Showing an empty picture puts up a small gray dotted circle, so you can see that something was shown. Saving one is an error, because PNG and GIF files need at least one pixel.
The book’s images: images/geisel.png, images/salk.png, images/cove.png, images/blacks-beach.png, images/torrey-cliffs.png, images/pier.png, images/pier-under.png
Credits
The Pict library is based on the image library from How to Design Programs and Racket, and on work by Ben Lerner at Northeastern University. It is re-implemented here to make it run well in the book and suit the needs of this course.