The Coder's Handbook   

Shapes, Color, and Text

SHAPES

For more detailed information, look at the Graphics and Color classes in the Slick 2D Javadoc

Drawing Basic Shapes


// Draws the outline of a rectangle or oval.  Uses x, y, w, h.

g.drawRect(50, 50, 50, 50);

g.drawOval(100, 200, 50, 50);

// Draws a line between two points

g.drawLine(400,  25,  500,  75);

// Draws a filled rectangle.  Uses x, y, w, h.

g.fillRect(200,  200,  500,  500);


// You can use variables to create animations for these images

g.fillOval(xPos, 100, 200, 200);

COLOR

Changing Colors


// Sets color using a preset color.

g.setColor(Color.cyan);


// Sets color using a custom RGB value.  

g.setColor(new Color(125, 50, 70));


// You can add a fourth parameter as "alpha value" or transparency (0-255)

g.setColor(new Color(255, 0, 0, 50));


// You can store these colors as variables and use color methods...

Color mediumRed = new Color(235, 45, 50);

Color darkRed = mediumRed.darker();

int blueAmount = mediumRed.getBlue();

Floats Work Differently

TEXT

Drawing Strings


// Draws text at x, y

g.drawString("Hello World!", 50, 70);

// Note that this only takes a String as a parameter

g.drawString(""+getCurHealth(), xPos, yPos));

A visual approximation of the
draw string method.

RESOURCES

The New Boston - Shapes, Text, and Titles