CS396N - Web Programming Spring 2002 Java 2D

Step 4

Example 1

Draw Rectangle and Filled Circle

import java.applet.*;
import java.awt.*;      // For Graphics, etc.
import java.awt.geom.*; // For Ellipse2D, etc.

public class ShapeExample2 extends Applet {
private Ellipse2D.Double circle =
new Ellipse2D.Double(10, 10, 350, 350);
private Rectangle2D.Double square =
new Rectangle2D.Double(10, 10, 350, 350);
public void init()
{
setSize (400,400); // sets image size
}
public void paint(Graphics g) {

Graphics2D g2d = (Graphics2D)g;
g2d.fill(circle);
g2d.draw(square);
}

protected Ellipse2D.Double getCircle() {
return(circle);
}

}

Example 2

import java.applet.*;
import java.awt.*;      // For Graphics, etc.
import java.awt.geom.*; // For Ellipse2D, etc.

public class GradientPaintExample2 extends Applet {
private Ellipse2D.Double circle =
new Ellipse2D.Double(10, 10, 350, 350);
new GradientPaint(0, 0, Color.red, 175, 175, Color.yellow,
true); // true means to repeat pattern

public void init()
{
setSize (400,400);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
}

g2d.fill(getCircle());
g2d.setPaint(Color.black);
g2d.draw(getCircle());
}

protected Ellipse2D.Double getCircle() {
return(circle);
}

}

Stroke Styles

Stroke Thickness

import java.awt.*;
import java.applet.*;
import java.awt.geom.*;

public class StrokeThicknessExample2 extends Applet {

private Ellipse2D.Double circle =
new Ellipse2D.Double(10, 10, 350, 350);
new GradientPaint(0, 0, Color.red, 175, 175, Color.yellow,
true); // true means to repeat pattern

public void init()
{
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
env.getAvailableFontFamilyNames();
setFont(new Font("Counterscraps", Font.PLAIN, 75));
setSize(400,400);
}

public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
drawBigString(g2d);
drawThickCircleOutline(g2d);
}

g2d.fill(getCircle());
g2d.setPaint(Color.black);
g2d.draw(getCircle());
}

protected void drawBigString(Graphics2D g2d) {
g2d.setPaint(Color.black);
g2d.drawString("Java 2D", 25, 215);
}

protected void drawThickCircleOutline(Graphics2D g2d) {
g2d.setPaint(Color.blue);
g2d.setStroke(new BasicStroke(8)); // 8-pixel wide pen
g2d.draw(getCircle());
}

protected Ellipse2D.Double getCircle() {
return(circle);
}

}

Dashed Stroke

import java.awt.*;
import java.applet.*;
import java.awt.geom.*;

public class DashedStrokeExample2 extends Applet {

private Ellipse2D.Double circle =
new Ellipse2D.Double(10, 10, 350, 350);
new GradientPaint(0, 0, Color.red, 175, 175, Color.yellow,
true); // true means to repeat pattern

public void init()
{
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
env.getAvailableFontFamilyNames();
setFont(new Font("Counterscraps", Font.PLAIN, 75));
setSize(400,400);
}

public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
drawBigString(g2d);
drawDashedCircleOutline(g2d);
}

g2d.fill(getCircle());
g2d.setPaint(Color.black);
g2d.draw(getCircle());
}

protected void drawBigString(Graphics2D g2d) {
g2d.setPaint(Color.black);
g2d.drawString("Java 2D", 25, 215);
}

protected void drawDashedCircleOutline(Graphics2D g2d) {
g2d.setPaint(Color.blue);
// 30-pixel line, 10-pixel gap, 10-pixel line, 10-pixel gap
float[] dashPattern = { 30, 10, 10, 10 };
g2d.setStroke(new BasicStroke(8, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10,
dashPattern, 0));
g2d.draw(getCircle());
}

protected Ellipse2D.Double getCircle() {
return(circle);
}

}

Coordinate Transforms

Rotation

import java.awt.*;
import java.applet.*;
import java.awt.geom.*;

public class RotationExample2 extends Applet {
private Color[] colors = { Color.white, Color.black };

private Ellipse2D.Double circle =
new Ellipse2D.Double(10, 10, 350, 350);
new GradientPaint(0, 0, Color.red, 175, 175, Color.yellow,
true); // true means to repeat pattern

public void init()
{

setSize(400,400);
}

public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
drawThickCircleOutline(g2d);
g2d.setFont(new Font("Serif",Font.ITALIC,90));
// Move the origin to the center of the circle.
g2d.translate(185.0, 185.0);
for (int i=0; i<16; i++) {
// Rotate the coordinate system around current
// origin, which is at the center of the circle.
g2d.rotate(Math.PI/8.0);
g2d.setPaint(colors[i%2]);
g2d.drawString("Java", 0, 0);
}
}

g2d.fill(getCircle());
g2d.setPaint(Color.black);
g2d.draw(getCircle());
}

protected void drawThickCircleOutline(Graphics2D g2d) {
g2d.setPaint(Color.blue);
g2d.setStroke(new BasicStroke(8)); // 8-pixel wide pen
g2d.draw(getCircle());
}

protected Ellipse2D.Double getCircle() {
return(circle);
}

}