Graphic Shape Library
Cay S. Horstmann
I created the Simple Java Graphics library for the CS46A Udacity course. It is similar to the standard Java graphics library, but it works better with BlueJ. Here, you will find an overview of the library, the API documentation, and a cookbook for translating your simple graphics programs to standard graphics.
Browse 194,336 incredible Abstract Shapes vectors, icons, clipart graphics, and backgrounds for royalty-free download from the creative contributors at Vecteezy! Download free PowerPoint charts, diagrams and templates from Powered Template individual designs, a huge number of alternatives for any taste and presentation purpose.
Using the Graphics Library
Download and unzip this file.
Simply include the classes that you need (or, if you prefer, all classes) in your project. You always need Shape
, Canvas
, and Color
. Then add Rectangle
, Ellipse
, Text
, Line
, and Picture
as needed.
Construct a shape and then either draw
it or fill
it. (Filling fills the entire shape with its color.)
You can change a shape by translating (i.e., moving) or growing it.
All changes to a shape are instantaneous. For example, if you view a Rectangle
object in BlueJ and change the color, position, or size (with setColor
, translate
, or grow
), the shape gets updated right away.
Constructing Shapes
To construct a rectangle, give the x- and y- positions of the top left corner, the width, and the height. For an ellipse, give those values for the bounding box of the ellipse.
For a line, specify the end points. For text, specify the top left corner and the string to be drawn.
To construct a picture, provide the name of the image file, and then move it to the desired location with the translate
method.
You can get the the width and height of any shape with the getWidth
, and getHeight
methods. The getX
and getY
methods return the coordinates of the top left corner. For convenience, getMaxX
and getMaxY
return the coordinates of the bottom right corner.
Canvas Methods
All shapes are drawn on an object of type Canvas
. You don't need to worry about it, but the Canvas
class has a couple of convenience methods.
Canvas.pause()
pops up a dialog so that the drawing is stopped until the user dismisses the dialog. This can be useful when you want to see a picture before you change its pixels.
Canvas.snapshot()
takes a snapshot of the canvas, fades it, and sets the faded image as the canvas background. Any newer drawings appear at full intensity. By taking repeated snapshots, you can show movement.
Pictures and Pixels
The Picture
class can be used to add images to scenes, or to write programs that manipulate pixels.
Graphic Import Shape Library
If you want to do the latter, you can construct an empty picture as
You access the pixels of a picture with the methods getColorAt
and setColorAt
. You can use a single integer index to access the pixels.
The index goes from 0 to pic.pixels() - 1
, where pic.pixels()
is the total number of pixels in the image. It is the same as pic.getWidth() * pic.getHeight()
.
This one-dimensional index is useful if you simply want to iterate over all pixels and transform them in a uniform way, for example to make all pixels in an image redder.
Alternatively, you can access pixels by their x
and y
coordinates, for example
Here, x
goes from 0 to pic.getWidth() - 1
and y
goes from 0 to pic.getHeight() - 1
.
Finally, you can get a two-dimensional array of gray levels between 0 and 255 with the call
Why not an array of colors? This method is used for some practice assignments with two-dimensional arrays, and an array of int
is easier to work with than an array of Color
values.
Note that the row index is the y-coordinate and the column index is the x-coordinate; that is, grays[y][x]
is the gray level of pix.getColor(x, y)
.
API Documentation
Follow this link.
Changing to Standard Java
If you can't use the simple graphics library, make these changes in your program.
- Use
Rectangle2D.Double
,Ellipse2D.Double
,Line2D.Double
from thejava.awt.geom
package instead ofRectangle
,Ellipse
,Line
- Place your drawing code inside a class
- In the drawing instructions, put the code that you would have placed in
main
, and change all callsshape.draw()
tog2.draw(shape)
andshape.fill()
tog2.fill(shape)
. However, forText
objects, there is no analogous object. Instead, useg2.drawString(x, y, messageString)
. - Use
java.awt.Color
instead of theColor
class. Replaceshape.setColor(c);
withg2.setColor(c);
just before drawing or filling the shape. - Add a class
Now you probably know why I supply the simple library instead :-)
Finally, I finished U8glib V2, now called U8g2.
What is u8g2?
- Graphics library for many different monochrome OLEDs and LCDs
- Supported display controller: SSD1305, SSD1306, SSD1309, SSD1322, SSD1325, SSD1327, SSD1606, SH1106, T6963, RA8835, LC7981, PCD8544, PCF8812, UC1604, UC1608, UC1610, UC1611, UC1701, ST7565, ST7567, NT7534, IST3020, ST7920, LD7032, KS0108
- Powerful text and graphics commands
- More than 700 fonts available
Installation
- … via Arduino IDE library manager (search for “u8g2”).
Graphic Shape Library For Preschoolers
What are the most important advantages compared to U8glib V1?
- Full “RAM” memory buffer without picture loop (u8glib picture loop still supported).
- Arduino SPI and TWI Libraries instead of custom code. This will make u8g2 much more portable.
- Successfully tested with AVR, SAM and ESP8266 boards.
- Support for Unicode and UTF-8: Unicode chars appear on the display as written in the Arduino IDE.
- Faster compilation.
- High speed text only API (U8x8).
- Hardware 180 degree rotation for some displays available.
- Compressed font format.
- Button debounce event handler included.
- 90% compatible with U8glib.
Limitations
- Still support for some display controller is missing compared to U8glib V1
Links
Oliver
Edit: Updated list of supported controllers and font count.
Edit: Updated list of supported controllers (v2.9.1)
Edit: Updated list of supported controllers (v2.12.6)