Fill It Game Without Java

Fill It Game Without Java Average ratng: 4,7/5 6935 votes

Visit The Arcade Machine Internet Entertainment Center and play FILL-IT, the online Java game.

In this part of the Java 2D tutorial, we create some basic and more advanced shapes. We fill shapes with solid colours, gradients, and textures.

Basic shapes

First we draw some basic Java 2D shapes.

Flash Games

BasicShapes.java

In this example, we draw six basic shapes on the panel:a square, a rectangle, a rounded rectangle, an ellipse, an arc, and a circle.

The shapes will be drawn in a gray background.

Fill It Game Without Java

The fillRect() method is used to draw both a rectangle and a square. The first two parameters are x, y coordinates of a shape to be drawn. The last two parameters are the width and the height of the shape.

Here we create a rounded rectangle. The last two parameters are the horizontal and vertical diameters of the arc at the four corners.

The fill() method draws the interior of the given shape—an ellipse.

The fillArc() method fills a circular or elliptical arc covering the specified rectangle. An arc is a portion of the circumference of a circle.

A circle is drawn using the fillOval() method.

General path

More complex shapes can be constructed with GeneralPath. It represents a geometric path constructed from straight lines, and quadratic and cubic Bézier curves.

The following example creates a star shape with this class.

We create a star from a series of points.

Online

These are the coordinates of the star.

Here we instantiate the GeneralPath class.

We move to the initial coordinate of the GeneralPath.

Here we connect all the coordinates of the star.

We close the path and fill the interior of the star.

Areas

Another way to create complex shapes is to compose areas. Area stores and manipulates a resolution-independent description of an enclosed area of 2-dimensional space. It can be manipulated byaddition, subtraction, intersection, and exclusiveOr operations.

AreasEx.java

The example creates three different shapes by manipulating areas.

This code constructs a shape by subtracting an ellipse from a rectangle.

These lines construct a shape by adding a rectangle to an ellipse.

Colours

The Color class is used to work with colours in Java 2D. To fill rectangles with the current colour, we use the fillRect() method.

In the example we draw nine coloured rectangles.

There is no need to create a copy of the Graphics2D class orto reset the value when we change the colour property of the graphics context.

A new colour is created with the Color class. The parameters of the constructorare the red, green, and blue parts of the new colour. The setColor() method sets the graphics context's current colour to the specified colour value. All subsequent graphics operations use this colour value.

To fill the rectangle with a colour, we use the fillRect() method.

Gradients

In computer graphics, gradient is a smooth blending of shades from light to dark or from one colour to another. In 2D drawing programs and paint programs, gradients are used to create colorful backgrounds and special effects as well as to simulate lights and shadows. (answers.com)

GradientsEx.java

Our code example presents five rectangles with gradients.

To work with gradients, we use the GradientPaint class. By manipulating the colour values and the starting and ending points, we can get different results.

The gradient is activated by calling the setPaint() method.

Textures

A texture is a bitmap image applied to a shape.To work with textures in Java 2D, we use the TexturePaint class.A texture is applied with the setPaint() method.

In the code example, we fill three rectangles with three different textures.

Using the ImageIO class, we read an image into a buffered image.

We create a TexturePaint class out of the buffered image.

We fill a rectangle with a texture.

In this part of the Java 2D tutorial, we have covered some basic and more advanced shapes of the Java 2D library.