Next: References. Up: Using objects. Previous: Some basic graphics classes.


Putting it together

Here's a somewhat longer program that draws a gradual color switch, with a color scheme resembling a sunrise (dark blue to yellow).

It accomplishes this by drawing a series of horizontal lines of colors of various shades between the extremes.
import csbsju.cs160.*;

public class Sunrise {
    public static void run() {
        DrawableFrame window = new DrawableFrame();
        window.show();

        Graphics g = window.getGraphics();
        int y = 0;
        while(y < window.getHeight()) {
            // mix color for this row of window
            double yellow = 1.0 * y / window.getHeight();
            double blue = 0.4 - 0.4 * y / window.getHeight();
            Color color = new Color(yellow, yellow, blue);

            // draw this row
            g.setColor(color);
            g.drawLine(0, y, window.getWidth(), y);

            // move to next row
            y++;
        }
    }
}


Next: References. Up: Using objects. Previous: Some basic graphics classes.