A few examples


Here are some examples illustrating a few additional features in Java

Custom painting
Alpha
Offscreen images
Writing JPEG images
Importing images

Custom painting

A Graphics2D's setPaint method accepts an instance of Paint. A Color is an example of a Paint but we can also do more general things.

Example: Painting.java


Alpha

Here's an example where the alpha attribute of a Color is set. The entire code is not here but the relevant class is. In particular, look at how the colors are set up in the constructor.

SpherePanel.java


Offscreen images

In this example, I draw the picture on an offscreen image and examine its pixels: AlphaView.java


Writing JPEG Images

Here are two useful classes: One gives a JFrame containing a button that, when pressed, will output a .jpg image file of the content that has been added. You can just add content to it like you would an ordinary JFrame. Some parameters to look at are the scale and quality of the output. The second file contains the code to create the .jpg file.

JPEGDrawFrame.java

JPEGWriter.java, written by Bill Casselman


Importing images

This shows you how to use Swing classes to import images into Java programs and manipulate them.

ReadImage.java

Painting.java