Created
December 7, 2012 04:06
-
-
Save anonymous/4230687 to your computer and use it in GitHub Desktop.
Graphics2D Example with Appengine-awt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.witwall.test; | |
import java.io.File; | |
import java.io.IOException; | |
import com.google.code.appengine.awt.Color; | |
import com.google.code.appengine.awt.Font; | |
import com.google.code.appengine.awt.FontMetrics; | |
import com.google.code.appengine.awt.Graphics2D; | |
import com.google.code.appengine.awt.geom.Area; | |
import com.google.code.appengine.awt.geom.Ellipse2D; | |
import com.google.code.appengine.awt.image.BufferedImage; | |
import com.google.code.appengine.imageio.ImageIO; | |
public class Graphics2DEx { | |
/** | |
* @param args | |
* @throws IOException | |
*/ | |
public static void main(String[] args) throws IOException { | |
int w = 150; | |
int h = 150; | |
BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB); | |
Graphics2D g2 =image.createGraphics(); | |
double ew = w/2; | |
double eh = h/2; | |
Ellipse2D.Double circle, oval, leaf, stem; | |
Area circ, ov, leaf1, leaf2, st1, st2; | |
circle = new Ellipse2D.Double(); | |
oval = new Ellipse2D.Double(); | |
leaf = new Ellipse2D.Double(); | |
stem = new Ellipse2D.Double(); | |
circ = new Area(circle); | |
ov = new Area(oval); | |
leaf1 = new Area(leaf); | |
leaf2 = new Area(leaf); | |
st1 = new Area(stem); | |
st2 = new Area(stem); | |
g2.setColor(Color.green); | |
// Creates the first leaf by filling the intersection of two Area objects created from an ellipse. | |
leaf.setFrame(ew-16, eh-29, 15.0, 15.0); | |
leaf1 = new Area(leaf); | |
leaf.setFrame(ew-14, eh-47, 30.0, 30.0); | |
leaf2 = new Area(leaf); | |
leaf1.intersect(leaf2);//will stop here | |
g2.fill(leaf1); | |
// Creates the second leaf. | |
leaf.setFrame(ew+1, eh-29, 15.0, 15.0); | |
leaf1 = new Area(leaf); | |
leaf2.intersect(leaf1); | |
g2.fill(leaf2); | |
g2.setColor(Color.black); | |
// Creates the stem by filling the Area resulting from the subtraction of two Area objects created from an ellipse. | |
stem.setFrame(ew, eh-42, 40.0, 40.0); | |
st1 = new Area(stem); | |
stem.setFrame(ew+3, eh-47, 50.0, 50.0); | |
st2 = new Area(stem); | |
st1.subtract(st2); | |
g2.fill(st1); | |
g2.setColor(Color.yellow); | |
// Creates the pear itself by filling the Area resulting from the union of two Area objects created by two different ellipses. | |
circle.setFrame(ew-25, eh, 50.0, 50.0); | |
oval.setFrame(ew-19, eh-20, 40.0, 70.0); | |
circ = new Area(circle); | |
ov = new Area(oval); | |
circ.add(ov); | |
g2.fill(circ); | |
g2.setColor(Color.black); | |
Font thisFont = new Font("Arial", Font.PLAIN, 18); | |
g2.setFont(thisFont); | |
String pear = "Pear"; | |
FontMetrics metrics = g2.getFontMetrics(); | |
int width = metrics.stringWidth(pear); | |
g2.drawString(pear, (w - width) / 2, 20); | |
g2.dispose(); | |
ImageIO.write(image, "png", new File("graphics2DEx_gae.png")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment