ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

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);
	
	}

	

}
Screenshot: Java Circle using absolute coords
Screenshot: Java Circle using absolute coords

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.


Screenshot: Java Circle using absolute coords with JFrame changed.
Screenshot: Java Circle using absolute coords with JFrame changed.

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.

Instead, set up variables equivalent to your JFrame size:

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.

Screenshot: Java Circle with changes in code with the JFrame (1400, 700)
Screenshot: Java Circle with changes in code with the JFrame (1400, 700)
Screenshot: Java Circle with changes in code with JFrame(900, 700)
Screenshot: Java Circle with changes in code with JFrame(900, 700)

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;

}


Now Test Your Code:

Run > Run as > 1 Java Application


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

Mystery Drawing so far.
Mystery Drawing so far.

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);


Now Test Your Code:

Run > Run as > 1 Java Application


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



Screenshot: Mystery drawing at this point.
Screenshot: Mystery drawing 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);

}

}

Now Test Your Code:

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);


}


}


Mystery Drawing Revealed

Screenshot:  Mystery Drawing Revealed
Screenshot: Mystery Drawing Revealed

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!!!!!!!

Comments

    0 of 8192 characters used
    Post Comment

    • Taimur Tariq profile image

      Taimur Tariq 

      5 years ago from Karachi, Pakistan

      thanxxxxxxxxxxxx !!!!!! this is awesome, beautifulllll !!!!! it helped me a lot.. i am new to programming. i was facing tough time regarding my project. This help me a lot... Thank you once again...

    • profile image

      IntegrityYes 

      5 years ago

      You are very welcome,Patty.

    • Patty Kenyon profile imageAUTHOR

      Patty Kenyon 

      5 years ago from Ledyard, Connecticut

      Thanks Integrity!!!! I love that you always go through different Hubs whether or not you are interested!!!! You are truly an encouraging and supportive person!!! THANKS!!!!! I do appreciate it!!!

    • profile image

      IntegrityYes 

      5 years ago

      WHOA! Patty,that is way beyond awesome! I voted up.

    • Patty Kenyon profile imageAUTHOR

      Patty Kenyon 

      6 years ago from Ledyard, Connecticut

      Swarup-Ghosh, Thank-you so much!!! I appreciate it!!! In the near future, I want to write a tutorial on very basic animation. Again, thank-you!!!

    • Swarup-Ghosh profile image

      Swarup-Ghosh 

      6 years ago from Bangalore, India

      Awesome tutorial !! very useful and interesting too...voted up..carry on Patty.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)