# Java 2D Tutorial III--Graphics 2D

Updated on July 24, 2012

## Intro

This is Part Three of a Three Part Tutorial

In the first Tutorial I, I covered creating a package, creating an abstract class, creating a class that extends the abstract class. In Tutorial II, I covered JFrame, JPanel, coordinate system, custom colors, gradient paint, and how to test your code.

In this Tutorial, I will cover Graphics 2D -- you will need the code posted on my other tutorials because all three tutorials lead up to the Mystery drawing that will be finally be revealed.

At the end of this Tutorial, the entire code for the Mystery class will be posted both in code tags and in text form for those that prefer looking at code that way.

## Graphics 2D

Below are some of the basic Methods used to create Shapes in Java 2D. Other Methods are covered throughout the tutorial.

Line: The line is used to draw a line from point a to point b. It takes four arguments. The start x position, the start y position, the end x position and the end y position.

• Line2D nameItWhatYouWant = new Line2D.Float(x, y, x, y);
• Example: Line2D line = new Line2D.Float(x + 20, y + 30, x + 100, y + 30);

Ellipse: The Ellipse is used to make circles and ovals. It takes four arguments. The start x position, the start y position, the width of the Ellipse, and the height of the Ellipse.

• Ellipse2D nameItWhatYouWant = new Ellipse2D.Float(x, y, width, height);

To draw a circle make sure that the width and the height are the same.

• Ellipse2D circle = new Ellipse2D.Float(x + 100, y + 100, 50, 50);

To draw an oval make sure that the width and the height are different.

• Ellipse2D oval = new Ellipse2D.Float(x + 100, y + 100, 80, 50);

Rectangle: The Rectangle is used to make rectangles and squares. It takes four arguments. The start of the x position, the start of the y position, the width of the Rectangle, and the height of the Rectangle.

• Rectangle2D nameItWhatYouWant = new Rectangle2D.Float(x, y, width, height);

To draw a square make sure that the width and the height are the same.

• Rectangle2D square = new Rectangle2D.Float(x + 200, y +100, 80, 80);

To draw a rectangle make sure that the width and the height are different.

• Rectangle2D rectangle = new Rectangle2D.Float(x + 200, y + 100, 50, 100);

## Mystery Class

Open up Mystery class. In Eclipse, the tab should read Mystery.java. Your class should look like the code posted below. If not, make any corrections so that you will be able to test your code as we go along.

## Mystery Class

```package com.java2D.mysterydrawing;

import java.awt.Graphics2D;

public class Mystery extends Model{

@Override
public void drawShape(Graphics2D g) {
// TODO Auto-generated method stub

}
/**
* @param args
*/
public static void main(String[] args) {

Mystery a = new Mystery();

Frame v = new Frame(a);

v.setVisible(true);

}

}```

## Graphics2D--Should you use Absolute Coords?

When creating graphics in Graphics 2D you can use absolute coordinates. For example, you can draw and fill a circle.

For the below examples we want the circle to be located near the upper right hand corner of the screen.

// This sets the color for the Ellipse to white

g.setColor(Color.white);

// This creates the Ellipse arguments (x, y, width, height)

Ellipse2D circle = new Ellipse2D.Float(700, 50, 100, 100);

//This fills the circle with the color white

g.fill(circle);

Test the code, click Run > Run As > 1 Java Application.

*Notice the location of the circle.

However, what if you decided to change the size of the JFrame?

In the picture to the right, I changed my JFrame as well as the Gradient Paint background to (1400, 700).

*Notice that the circle is no longer located in the upper right hand corner.

For this example it wouldn't be to bad to change your code, but imagine creating a lot of graphics and then you decided you wanted your screen size larger...you would literally have line by line of your code to change the location of each image.

public void drawShape(Graphics2D g) {

//Set the Width and the Height equivalent to your JFrame.

int width = 1400;

int height = 700;

Note that in our original screen size, the width was 900, and the circle x value was 700, (width - 200). Also note in the code below, I changed y value to y + 50. Now change the code.

// This sets the color for the Ellipse to white

g.setColor(Color.white);

// This creates the Ellipse arguments (x, y, width, height)

Ellipse2D circle = new Ellipse2D.Float(width - 200 , y + 50, 100, 100);

//This fills the circle with the color white

g.fill(circle);

}

With the changes in code, now the circle is placed back in the upper right hand corner.

Now I changed the JFrame and the Gradient Paint used as the back ground, back to the original size of (900, 700). Now all I have to do is change the int width = 900

New code:

@Override

public void drawShape(Graphics2D g) {

//Set the Width and the Height equivalent to your JFrame.

int width = 900;

int height = 700;

// This sets the color for the Ellipse to white

g.setColor(Color.white);

// This creates the Ellipse arguments (x, y, width, height)

Ellipse2D circle = new Ellipse2D.Float(width - 200 , y + 50, 100, 100);

//This fills the circle with the color white

g.fill(circle);

}

## While Loop

Next adding more variables this will be used for the While loop:

public void drawShape(Graphics2D g) {

//Set the Width and the Height equivalent to your JFrame.

int width = 900;

int height = 700;

int number = 0;

int x1 = 20;

int x2 = 0;

int y1 = 590;

int y2 = 620;

int w = 30;

int w2 = 40;

int h = 110;

int h2 = 20;

Using a while loop instead of drawing the same items 17 times

Type this code after g.fill(circle);

while(number < 17){

//This sets the color to black

g.setColor(Color.black);

This creates the Rectangle arguments(x1, y1, w, h) from the new variables from above

Rectangle2D rectangle1 = new Rectangle2D.Float(x1, y1, w, h);

//This fills the rectangle with the color black

g.fill(rectangle1);

This creates the Rectangle arguments(x2, y2, w2, h2);

Rectangle2D rectangle2 = new Rectangle2D.Float(x2, y2, w2, h2);

// fill the rectangle with the color black

g.fill(rectangle2);

GeneralPath is a great way to make shapes that aren't standard and allows the shape to easily be filled with a color of your choice. It is done by connecting a series of points. If you were to you the Line command, you would have to fill in the color nearly pixel by pixel.

//This will create a triangle using GeneralPath

GeneralPath triangle1 = new GeneralPath();

The moveTo--Think of it as picking up a pencil and moving your pencil to a different spot on the paper.

The lineTo--draws a line from the moveTo point to the point that you designate.

Note we are using variable x1

triangle1.moveTo(x1 , y + 590);

triangle1.lineTo(x1 + 15, y + 560);

triangle1.lineTo(x1 + 30, y + 590);

triangle1.lineTo(x1 , y + 590);

//this fills the triangle with the color black

g.fill(triangle1);

//increments the number of the drawing

number += 1;

//increments x1 and x2 coordinates so there is a space in between drawing

x1 += 50;

x2 += 60;

}

### Run > Run as > 1 Java Application

The picture below represents what the Mystery drawing look like at this point.

## Code Up To This Point

```package com.java2D.mysterydrawing;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

public class Mystery extends Model{

@Override
public void drawShape(Graphics2D g) {
//Set the Width and the Height equivalent to your JFrame.
int width = 900;
int height = 700;

int number = 0;
int x1 = 20;
int x2 = 0;
int y1 = 590;
int y2 = 620;
int w = 30;
int w2 = 40;
int h = 110;
int h2 = 20;

// This sets the color for the Ellipse to white
g.setColor(Color.white);
// This creates the Ellipse arguments (x, y, width, height)
Ellipse2D circle = new Ellipse2D.Float(width - 200 , y + 50, 100, 100);
//This fills the circle with the color white
g.fill(circle);

//Using a while loop instead of drawing the same item 17 times
while(number <  17){

//This sets the color to black
g.setColor(Color.black);

//This creates the Rectangle arguments(x1, y1, w, h)
Rectangle2D rectangle1 = new Rectangle2D.Float(x1, y1, w, h);

//This fills the rectangle with the color black
g.fill(rectangle1);

//This creates the Rectangle arguments(x2, y2, w2, h2)

Rectangle2D rectangle2 = new Rectangle2D.Float(x2, y2, w2, h2);
g.fill(rectangle2);

//This will create a triangle using GeneralPath using x1
GeneralPath triangle1 = new GeneralPath();
triangle1.moveTo(x1 , y + 590);
triangle1.lineTo(x1 + 15, y + 560);
triangle1.lineTo(x1 + 30, y + 590);
triangle1.lineTo(x1 , y + 590);

g.fill(triangle1);

//increments the number of the drawing
number += 1;
//increments x1 and x2 coordinates so there is a space in between drawing
x1 += 50;
x2 += 60;

}

}
/**
* @param args
*/
public static void main(String[] args) {

Mystery a = new Mystery();

Frame v = new Frame(a);

v.setVisible(true);

}

```

Type the below code in after:

x1 += 50;

x2 += 60;

}

Create a new General Path.

Notice in the code below the color change and the draw method.

First filling GeneralPath one with black, changing the color to Dark Gray, then using the draw method.

• The Fill Method fills the shape with color
• The Draw Method draws the outline of the shape.

It is important which method is used first. In this case I am filling GeneralPath one with black and changing the color to Dark Gray then using the draw method to outline the square. If I had used the Draw method first and then the fill method, the Dark Gray outline would not be seen.

GeneralPath one = new GeneralPath();

one.moveTo(x + 300, y + 400);

one.lineTo(x + 280, y + 300);

one.lineTo(x + 350, y + 280);

one.lineTo(x + 350, y + 400);

one.lineTo(x + 300, y + 400);

g.fill(one);

g.setPaint(Color.DARK_GRAY);

g.draw(one);

Looking at the code below:

In this case I am filling the square with yellow and then using the draw method to outline the square. If I had used the Draw method first and then the fill method, the red outline would not be seen.

Rectangle2D rectangle3 = new Rectangle2D.Float(x + 595, y + 315, 15, 15 );

g.setPaint(Color.YELLOW);

g.fill(rectangle3);

g.setPaint(Color.RED);

g.draw(rectangle3);

Rectangle2D rectangle4 = new Rectangle2D.Float(x + 350, y +240, 150, 170);

g.setPaint(Color.BLACK);

g.fill(rectangle4);

g.setPaint(Color.DARK_GRAY);

GeneralPath two = new GeneralPath();

two.moveTo(x +500, y + 280);

two.lineTo(x + 570, y + 300);

two.lineTo(x + 550, y + 400);

two.lineTo(x +500, y + 400);

g.setPaint(Color.BLACK);

g.fill(two);

g.setPaint(Color.DARK_GRAY);

g.draw(two);

Notice here that the outline for rectangle4 is drawn. If it was drawn before this point the other shapes would have covered the outline.

g.draw(rectangle4);

Rectangle2D rectangle6 = new Rectangle2D.Float(x + 540, y + 315, 15, 15 );

g.setPaint(Color.YELLOW);

g.fill(rectangle6);

g.setPaint(Color.RED);

g.draw(rectangle6);

### Run > Run as > 1 Java Application

The picture below represents what the Mystery drawing looks like at this point.

## Code To This Point

```package com.java2D.mysterydrawing;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

public class Mystery extends Model{

@Override
public void drawShape(Graphics2D g) {
//Set the Width and the Height equivalent to your JFrame.
int width = 900;
int height = 700;

int number = 0;
int x1 = 20;
int x2 = 0;
int y1 = 590;
int y2 = 620;
int w = 30;
int w2 = 40;
int h = 110;
int h2 = 20;

// This sets the color for the Ellipse to white
g.setColor(Color.white);
// This creates the Ellipse arguments (x, y, width, height)
Ellipse2D circle = new Ellipse2D.Float(width - 200 , y + 50, 100, 100);
//This fills the circle with the color white
g.fill(circle);

//Using a while loop instead of drawing the same item 17 times
while(number <  17){

//This sets the color to black
g.setColor(Color.black);

//This creates the Rectangle arguments(x1, y1, w, h)
Rectangle2D rectangle1 = new Rectangle2D.Float(x1, y1, w, h);

//This fills the rectangle with the color black
g.fill(rectangle1);

//This creates the Rectangle arguments(x2, y2, w2, h2)

Rectangle2D rectangle2 = new Rectangle2D.Float(x2, y2, w2, h2);
g.fill(rectangle2);

//This will create a triangle using GeneralPath using x1
GeneralPath triangle1 = new GeneralPath();
triangle1.moveTo(x1 , y + 590);
triangle1.lineTo(x1 + 15, y + 560);
triangle1.lineTo(x1 + 30, y + 590);
triangle1.lineTo(x1 , y + 590);

g.fill(triangle1);

//increments the number of the drawing
number += 1;
//increments x1 and x2 coordinates so there is a space in between drawing
x1 += 50;
x2 += 60;

}

GeneralPath one = new GeneralPath();
one.moveTo(x + 300, y + 400);
one.lineTo(x + 280, y + 300);
one.lineTo(x + 350, y + 280);
one.lineTo(x + 350, y + 400);
one.lineTo(x + 300, y + 400);

g.fill(one);
g.setPaint(Color.DARK_GRAY);
g.draw(one);

Rectangle2D rectangle3 = new Rectangle2D.Float(x + 295, y + 315, 15, 15 );
g.setPaint(Color.YELLOW);
g.fill(rectangle3);
g.setPaint(Color.RED);
g.draw(rectangle3);

Rectangle2D rectangle4 = new Rectangle2D.Float(x + 350, y +240, 150, 170);
g.setPaint(Color.BLACK);
g.fill(rectangle4);
g.setPaint(Color.DARK_GRAY);

GeneralPath two = new GeneralPath();
two.moveTo(x +500, y + 280);
two.lineTo(x + 570, y + 300);
two.lineTo(x + 550, y + 400);
two.lineTo(x +500, y + 400);
g.setPaint(Color.BLACK);
g.fill(two);
g.setPaint(Color.DARK_GRAY);

g.draw(two);
g.draw(rectangle4);

Rectangle2D rectangle6 = new Rectangle2D.Float(x + 540, y + 315, 15, 15 );
g.setPaint(Color.YELLOW);
g.fill(rectangle6);
g.setPaint(Color.RED);
g.draw(rectangle6);

}
/**
* @param args
*/
public static void main(String[] args) {

Mystery a = new Mystery();

Frame v = new Frame(a);

v.setVisible(true);

}```

Type the following code after g.draw(rectangle6);

Rectangle2D rectangle7 = new Rectangle2D.Float(x + 365, y + 280, 20, 20 );

g.setPaint(Color.RED);

g.draw(rectangle7);

Rectangle2D rectangle8 = new Rectangle2D.Float(x + 400, y + 280, 20, 20 );

g.setPaint(Color.YELLOW);

g.fill(rectangle8);

g.setPaint(Color.RED);

g.draw(rectangle8);

Rectangle2D rectangle9 = new Rectangle2D.Float(x + 430, y + 280, 20, 20 );

g.setPaint(Color.YELLOW);

g.fill(rectangle9);

g.setPaint(Color.RED);

g.draw(rectangle9);

Rectangle2D rectangle10 = new Rectangle2D.Float(x + 460, y + 280, 20, 20 );

g.setPaint(Color.RED);

g.draw(rectangle10);

GeneralPath three = new GeneralPath();

three.moveTo(x + 280, y + 270);

three.lineTo(x + 425, y + 170);

three.lineTo(x + 560, y + 270);

three.lineTo(x + 350, y + 270);

g.setPaint(Color.BLACK);

g.fill(three);

g.setPaint(Color.DARK_GRAY);

g.draw(three);

GeneralPath four = new GeneralPath();

four.moveTo(x + 400, y + 410);

four.lineTo(x + 400, y + 360);

four.lineTo(x + 430, y + 310);

four.lineTo(x + 460, y + 360);

four.lineTo(x + 460, y + 410);

g.setPaint(Color.RED);

g.draw(four);

GeneralPath five = new GeneralPath();

five.moveTo(x + 410, y + 410);

five.lineTo(x + 410, y + 370);

five.lineTo(x + 430, y + 325);

five.lineTo(x + 450, y + 370);

five.lineTo(x + 450, y + 410);

g.draw(five);

Ellipse2D circle2 = new Ellipse2D.Float(x + 420, y + 350, 20, 20);

g.setColor(Color.DARK_GRAY);

g.fill(circle2);

g.setPaint(Color.BLACK);

g.draw(circle2);

}

This is the main:

/**

* @param args

*/

public static void main(String[] args) {

This is the Mystery class and naming it a.

Mystery a = new Mystery();

This is calling the JFrame, naming it v, and passing the argument a which allows the drawing to be painted onto the JPanel.

Frame v = new Frame(a);

This is setting the JFrame visibility to true so that the frame is visible in the window.

v.setVisible(true);

}

}

### Run > Run as > 1 Java Application

The Mystery drawing is Revealed Below. If there are any problems, check for typos and re-compare the code pasted here and your code.

## The Entire Mystery Class

```package com.java2D.mysterydrawing;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

public class Mystery extends Model{

@Override
public void drawShape(Graphics2D g) {
//Set the Width and the Height equivalent to your JFrame.
int width = 900;
int height = 700;

int number = 0;
int x1 = 20;
int x2 = 0;
int y1 = 590;
int y2 = 620;
int w = 30;
int w2 = 40;
int h = 110;
int h2 = 20;

// This sets the color for the Ellipse to white
g.setColor(Color.white);
// This creates the Ellipse arguments (x, y, width, height)
Ellipse2D circle = new Ellipse2D.Float(width - 200 , y + 50, 100, 100);
//This fills the circle with the color white
g.fill(circle);

//Using a while loop instead of drawing the same item 17 times
while(number <  17){

//This sets the color to black
g.setColor(Color.black);

//This creates the Rectangle arguments(x1, y1, w, h)
Rectangle2D rectangle1 = new Rectangle2D.Float(x1, y1, w, h);

//This fills the rectangle with the color black
g.fill(rectangle1);

//This creates the Rectangle arguments(x2, y2, w2, h2)

Rectangle2D rectangle2 = new Rectangle2D.Float(x2, y2, w2, h2);
g.fill(rectangle2);

//This will create a triangle using GeneralPath using x1
GeneralPath triangle1 = new GeneralPath();
triangle1.moveTo(x1 , y + 590);
triangle1.lineTo(x1 + 15, y + 560);
triangle1.lineTo(x1 + 30, y + 590);
triangle1.lineTo(x1 , y + 590);

g.fill(triangle1);

//increments the number of the drawing
number += 1;
//increments x1 and x2 coordinates so there is a space in between drawing
x1 += 50;
x2 += 60;

}

GeneralPath one = new GeneralPath();
one.moveTo(x + 300, y + 400);
one.lineTo(x + 280, y + 300);
one.lineTo(x + 350, y + 280);
one.lineTo(x + 350, y + 400);
one.lineTo(x + 300, y + 400);

g.fill(one);
g.setPaint(Color.DARK_GRAY);
g.draw(one);

Rectangle2D rectangle3 = new Rectangle2D.Float(x + 295, y + 315, 15, 15 );
g.setPaint(Color.YELLOW);
g.fill(rectangle3);
g.setPaint(Color.RED);
g.draw(rectangle3);

Rectangle2D rectangle4 = new Rectangle2D.Float(x + 350, y + 250, 150, 160);
g.setPaint(Color.BLACK);
g.fill(rectangle4);
g.setPaint(Color.DARK_GRAY);

GeneralPath two = new GeneralPath();
two.moveTo(x +500, y + 280);
two.lineTo(x + 570, y + 300);
two.lineTo(x + 550, y + 400);
two.lineTo(x +500, y + 400);
g.setPaint(Color.BLACK);
g.fill(two);
g.setPaint(Color.DARK_GRAY);

g.draw(two);
g.draw(rectangle4);

Rectangle2D rectangle6 = new Rectangle2D.Float(x + 540, y + 315, 15, 15 );
g.setPaint(Color.YELLOW);
g.fill(rectangle6);
g.setPaint(Color.RED);
g.draw(rectangle6);

Rectangle2D rectangle7 = new Rectangle2D.Float(x + 365, y + 280, 20, 20 );
g.setPaint(Color.RED);
g.draw(rectangle7);

Rectangle2D rectangle8 = new Rectangle2D.Float(x + 400, y + 280, 20, 20 );
g.setPaint(Color.YELLOW);
g.fill(rectangle8);
g.setPaint(Color.RED);
g.draw(rectangle8);

Rectangle2D rectangle9 = new Rectangle2D.Float(x + 430, y + 280, 20, 20 );
g.setPaint(Color.YELLOW);
g.fill(rectangle9);
g.setPaint(Color.RED);
g.draw(rectangle9);

Rectangle2D rectangle10 = new Rectangle2D.Float(x + 460, y + 280, 20, 20 );
g.setPaint(Color.RED);
g.draw(rectangle10);

GeneralPath three = new GeneralPath();
three.moveTo(x + 280, y + 270);
three.lineTo(x + 425, y + 170);
three.lineTo(x + 560, y + 270);
three.lineTo(x + 350, y + 270);
g.setPaint(Color.BLACK);
g.fill(three);
g.setPaint(Color.DARK_GRAY);
g.draw(three);

GeneralPath four = new GeneralPath();
four.moveTo(x + 400, y + 410);
four.lineTo(x + 400, y + 360);
four.lineTo(x + 430, y + 310);
four.lineTo(x + 460, y + 360);
four.lineTo(x + 460, y + 410);
g.setPaint(Color.RED);
g.draw(four);

GeneralPath five = new GeneralPath();
five.moveTo(x + 410, y + 410);
five.lineTo(x + 410, y + 370);
five.lineTo(x + 430, y + 325);
five.lineTo(x + 450, y + 370);
five.lineTo(x + 450, y + 410);

g.draw(five);

Ellipse2D circle2 = new Ellipse2D.Float(x + 420, y + 350, 20, 20);
g.setColor(Color.DARK_GRAY);
g.fill(circle2);
g.setPaint(Color.BLACK);
g.draw(circle2);

}
/**
* @param args
*/
public static void main(String[] args) {

Mystery a = new Mystery();

Frame v = new Frame(a);

v.setVisible(true);

}

}
```

## Text Version of Mystery class

package com.java2D.mysterydrawing;

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.geom.Ellipse2D;

import java.awt.geom.GeneralPath;

import java.awt.geom.Line2D;

import java.awt.geom.Rectangle2D;

public class Mystery extends Model{

@Override

public void drawShape(Graphics2D g) {

//Set the Width and the Height equivalent to your JFrame.

int width = 900;

int height = 700;

int number = 0;

int x1 = 20;

int x2 = 0;

int y1 = 590;

int y2 = 620;

int w = 30;

int w2 = 40;

int h = 110;

int h2 = 20;

// This sets the color for the Ellipse to white

g.setColor(Color.white);

// This creates the Ellipse arguments (x, y, width, height)

Ellipse2D circle = new Ellipse2D.Float(width - 200 , y + 50, 100, 100);

//This fills the circle with the color white

g.fill(circle);

//Using a while loop instead of drawing the same item 17 times

while(number < 17){

//This sets the color to black

g.setColor(Color.black);

//This creates the Rectangle arguments(x1, y1, w, h)

Rectangle2D rectangle1 = new Rectangle2D.Float(x1, y1, w, h);

//This fills the rectangle with the color black

g.fill(rectangle1);

//This creates the Rectangle arguments(x2, y2, w2, h2)

Rectangle2D rectangle2 = new Rectangle2D.Float(x2, y2, w2, h2);

g.fill(rectangle2);

//This will create a triangle using GeneralPath using x1

GeneralPath triangle1 = new GeneralPath();

triangle1.moveTo(x1 , y + 590);

triangle1.lineTo(x1 + 15, y + 560);

triangle1.lineTo(x1 + 30, y + 590);

triangle1.lineTo(x1 , y + 590)

g.fill(triangle1);

//increments the number of the drawing

number += 1;

//increments x1 and x2 coordinates so there is a space in between drawing

x1 += 50;

x2 += 60;

}

GeneralPath one = new GeneralPath();

one.moveTo(x + 300, y + 400);

one.lineTo(x + 280, y + 300);

one.lineTo(x + 350, y + 280);

one.lineTo(x + 350, y + 400);

one.lineTo(x + 300, y + 400);

g.fill(one);

g.setPaint(Color.DARK_GRAY);

g.draw(one);

Rectangle2D rectangle3 = new Rectangle2D.Float(x + 295, y + 315, 15, 15 );

g.setPaint(Color.YELLOW);

g.fill(rectangle3);

g.setPaint(Color.RED);

g.draw(rectangle3);

Rectangle2D rectangle4 = new Rectangle2D.Float(x + 350, y + 250, 150, 160);

g.setPaint(Color.BLACK);

g.fill(rectangle4);

g.setPaint(Color.DARK_GRAY);

GeneralPath two = new GeneralPath();

two.moveTo(x +500, y + 280);

two.lineTo(x + 570, y + 300);

two.lineTo(x + 550, y + 400);

two.lineTo(x +500, y + 400);

g.setPaint(Color.BLACK);

g.fill(two);

g.setPaint(Color.DARK_GRAY);

g.draw(two);

g.draw(rectangle4);

Rectangle2D rectangle6 = new Rectangle2D.Float(x + 540, y + 315, 15, 15 );

g.setPaint(Color.YELLOW);

g.fill(rectangle6);

g.setPaint(Color.RED);

g.draw(rectangle6);

Rectangle2D rectangle7 = new Rectangle2D.Float(x + 365, y + 280, 20, 20 );

g.setPaint(Color.RED);

g.draw(rectangle7);

Rectangle2D rectangle8 = new Rectangle2D.Float(x + 400, y + 280, 20, 20 );

g.setPaint(Color.YELLOW);

g.fill(rectangle8);

g.setPaint(Color.RED);

g.draw(rectangle8);

Rectangle2D rectangle9 = new Rectangle2D.Float(x + 430, y + 280, 20, 20 );

g.setPaint(Color.YELLOW);

g.fill(rectangle9);

g.setPaint(Color.RED);

g.draw(rectangle9);

Rectangle2D rectangle10 = new Rectangle2D.Float(x + 460, y + 280, 20, 20 );

g.setPaint(Color.RED);

g.draw(rectangle10);

GeneralPath three = new GeneralPath();

three.moveTo(x + 280, y + 270);

three.lineTo(x + 425, y + 170);

three.lineTo(x + 560, y + 270);

three.lineTo(x + 350, y + 270);

g.setPaint(Color.BLACK);

g.fill(three);

g.setPaint(Color.DARK_GRAY);

g.draw(three);

GeneralPath four = new GeneralPath();

four.moveTo(x + 400, y + 410);

four.lineTo(x + 400, y + 360);

four.lineTo(x + 430, y + 310);

four.lineTo(x + 460, y + 360);

four.lineTo(x + 460, y + 410);

g.setPaint(Color.RED);

g.draw(four);

GeneralPath five = new GeneralPath();

five.moveTo(x + 410, y + 410);

five.lineTo(x + 410, y + 370);

five.lineTo(x + 430, y + 325);

five.lineTo(x + 450, y + 370);

five.lineTo(x + 450, y + 410);

g.draw(five);

Ellipse2D circle2 = new Ellipse2D.Float(x + 420, y + 350, 20, 20);

g.setColor(Color.DARK_GRAY);

g.fill(circle2);

g.setPaint(Color.BLACK);

g.draw(circle2);

}

/**

* @param args

*/

public static void main(String[] args) {

Mystery a = new Mystery();

Frame v = new Frame(a);

v.setVisible(true);

}

}

## Final Thoughts

There is so much more to Java 2D. These Tutorials covered the basics to get you started with a fun language.

I hope you enjoyed this Tutorial series. If you would like to see further Tutorials on certain drawings in Java 2D, be sure to contact me and I will see what I can do.

If you enjoyed these tutorials, please comment, and rate!!!

THANKS-AGAIN for reading these Tutorials!!! GOOD LUCK!!!!!!!

18

4

3

working