// RGB values are in the [0..255] range.
import java.awt.*;
import java.applet.*;
public class Java0620 extends Applet
{
public void paint(Graphics g)
{
g.setColor(new Color(255,0,255));
g.fillRect(0,0,800,200);
g.setColor(new Color(0,255,255));
g.fillRect(0,200,800,200);
g.setColor(new Color(100,100,100));
g.fillRect(0,400,800,200);
}
}


// Java0621.java
// This program shows all the shades of Red, Green and Blue using the <setColor> method.
import java.awt.*;
import java.applet.*;
public class Java0621 extends Applet
{
public void paint(Graphics g)
{
for (int red = 0; red <= 255; red++)
{
g.setColor(new Color(red,0,0));
g.drawLine(red,0,red,600);
}
for (int green = 0; green <= 255; green++)
{
g.setColor(new Color(0,green,0));
g.drawLine(green+255,0,green+255,600);
}
for (int blue = 0; blue <= 255;blue++)
{
g.setColor(new Color(0,0,blue));
g.drawLine(blue+510,0,blue+510,600);
}
}
}


// Java0622.java
// This program draws three squares with user-defined <Color> objects.
import java.awt.*;
import java.applet.*;
public class Java0622 extends Applet
{
public void paint(Graphics g)
{
Color myRed = new Color(255,0,64);
Color myGreen = new Color(16,255,16);
Color myBlue = new Color(64,64,255);
g.setColor(myRed);
g.fillRect(20,100,100,100);
g.setColor(myGreen);
g.fillRect(140,100,100,100);
g.setColor(myBlue);
g.fillRect(260,100,100,100);
}
}

Anonymous Objects
Situations exist where an object identifier is not
necessary when creating a new object.
The majority of objects have an object identifier like
these two examples:
Bank tom = new Bank(2500);
Color myRed = new Color(255,0,64);
There are also objects that are used as parameters in a
method call like:
g.setColor(new Color(100,100,100));
A new Color object is created, but not identified.
Such objects are called
anonymous
objects.

1.
Load Paint
2.
Click [Colors] - [Edit Colors] - [Define Custom Colors]
3.
Click the triangle at the right side of the window
4.
Move the crosshairs and triangle until you get the color
you want in the
Color/Solid
box.
5.
Copy the
Red
,
Green
, and
Blue
numbers into your java
program.
6.
Ignore the Hue, Sat, and Lum!

// Java0622.java
// Try This!
Add your customized color!
public class Java0622 extends Applet
{
public void paint(Graphics g)
{
Color myRed = new Color(255,0,64);
Color myGreen = new Color(16,255,16);
Color myBlue = new Color(64,64,255);
Color myBrown = new Color(150,100,15);
g.setColor(myRed);
g.fillRect(20,100,100,100);
g.setColor(myGreen);
g.fillRect(140,100,100,100);
g.setColor(myBlue);
g.fillRect(260,100,100,100);
g.setColor(myBrown);
g.fillRect(380,100,100,100);
}
}

Special Note for People
Who Create Web Pages
This same technique can be used to pick colors for a web page.
If you ever do a
View - Page Source
on a web page, you may see
something like:
The second line shows the same RGB (red, green, blue) values
are used in the same way that we did in Java.
The third line also uses RGB (red, green, blue) values, but these
are base 16 (Hexadecimal) numbers.
Either method works.
Chapter 16 will discuss
Number Systems
.

Drawing Polygons
Java has some special features to draw polygons.
The Graphics class provides two methods,
drawPolygon
and
fillPolygon
.
Before you draw any polygon, you must first create an
object of the
Polygon
class.
The
Polygon
class has an
addPoint
method.


You've reached the end of your free preview.
Want to read all 65 pages?
- Fall '14
- RICH,ELAINEA
- Object-Oriented Programming, Subroutine, Sue, Tom, System.out.println