O'Reilly logo

Java 2D Graphics by Jonathan Knudsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

8.1. If You're Not Too Picky...

Many applications don't need high quality color reproduction. Standard user interface components like buttons and scrolling lists don't demand accurate color reproduction — it doesn't matter if buttons are a slightly different color on different machines. Likewise, for charts and figures, it may not matter if the colors vary from machine to machine. But if you're writing desktop publishing software, or image manipulation software, you will need to be more careful. I'll talk about 2D's support for correct color handling later in this chapter.

If your application doesn't demand that colors look exactly the same everywhere, you don't have to read most of this chapter. It's easy to work with color without worrying about color spaces or profiles.

8.1.1. Creating Colors

The java.awt.Color class represents a color in a default RGB color space. To create a color, just specify its red, green, and blue components:

public Color(int r, int g, int b)

This constructor creates a new Color using the specified values for red, green and blue. The values should range from to 255, inclusive.

public Color(float r, float g, float b)

This constructor creates a new Color using the specified values for red, green and blue. The values should range from 0.0 to 1.0, inclusive.

public Color(int rgb)

This constructor accepts the red, green, and blue values in a packed integer. This means that the red, green, and blue values, which are 8 bits each, are stored inside a Java ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required