Java Tutorial/2D Graphics/Area
Содержание
Area.add
import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AreaAdding extends JPanel {
DrawingCanvas canvas= new DrawingCanvas();
public AreaAdding() {
add(canvas);
canvas.area1 = new Area(canvas.gp);
canvas.area1.add(canvas.area2);
canvas.repaint();
}
public static void main(String arg[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add("Center", new AreaAdding());
frame.pack();
frame.setSize(new Dimension(400, 300));
frame.setVisible(true);
}
class DrawingCanvas extends Canvas {
GeneralPath gp;
Ellipse2D ellipse;
Area area1, area2;
public DrawingCanvas() {
setBackground(Color.white);
setSize(350, 250);
int w = getWidth();
int h = getHeight();
gp = new GeneralPath();
gp.moveTo(w / 8, h / 2);
gp.lineTo(w / 2, h / 4);
gp.lineTo(7 * w / 8, h / 2);
gp.lineTo(w / 2, 3 * h / 4);
gp.closePath();
area1 = new Area(gp);
ellipse = new Ellipse2D.Double(w / 4, h / 4, w / 2, h / 2);
area2 = new Area(ellipse);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setStroke(new BasicStroke(2.0f));
g2D.draw(area1);
g2D.draw(area2);
}
}
}
Area.exclusiveOr
import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AreaExclusiveOr extends JPanel {
DrawingCanvas canvas;
public AreaExclusiveOr() {
canvas = new DrawingCanvas();
add(canvas);
canvas.area1 = new Area(canvas.gp);
canvas.area1.exclusiveOr(canvas.area2);
canvas.repaint();
}
public static void main(String arg[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add("Center", new AreaExclusiveOr());
frame.pack();
frame.setSize(new Dimension(400, 300));
frame.setVisible(true);
}
class DrawingCanvas extends Canvas {
GeneralPath gp;
Ellipse2D ellipse;
Area area1, area2;
public DrawingCanvas() {
setBackground(Color.white);
setSize(350, 250); // width and height of canvas
int w = getWidth();
int h = getHeight();
gp = new GeneralPath();
gp.moveTo(w / 8, h / 2);
gp.lineTo(w / 2, h / 4);
gp.lineTo(7 * w / 8, h / 2);
gp.lineTo(w / 2, 3 * h / 4);
gp.closePath();
area1 = new Area(gp); // General path area object
ellipse = new Ellipse2D.Double(w / 4, h / 4, w / 2, h / 2);
area2 = new Area(ellipse); // Ellipse area object
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setStroke(new BasicStroke(2.0f));
g2D.draw(area1);
g2D.draw(area2);
}
}
}
Area.intersect
import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AreaIntersecting extends JPanel {
DrawingCanvas canvas;
public AreaIntersecting() {
canvas = new DrawingCanvas();
add(canvas);
canvas.area1 = new Area(canvas.gp);
canvas.area1.intersect(canvas.area2);
canvas.repaint();
}
public static void main(String arg[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add("Center", new AreaIntersecting());
frame.pack();
frame.setSize(new Dimension(400, 300));
frame.setVisible(true);
}
class DrawingCanvas extends Canvas {
GeneralPath gp;
Ellipse2D ellipse;
Area area1, area2;
public DrawingCanvas() {
setBackground(Color.white);
setSize(350, 250); // width and height of canvas
int w = getWidth();
int h = getHeight();
gp = new GeneralPath();
gp.moveTo(w / 8, h / 2);
gp.lineTo(w / 2, h / 4);
gp.lineTo(7 * w / 8, h / 2);
gp.lineTo(w / 2, 3 * h / 4);
gp.closePath();
area1 = new Area(gp); // General path area object
ellipse = new Ellipse2D.Double(w / 4, h / 4, w / 2, h / 2);
area2 = new Area(ellipse); // Ellipse area object
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setStroke(new BasicStroke(2.0f));
g2D.draw(area1);
g2D.draw(area2);
}
}
}
Areas support the following boolean operations: Union, Subtraction, Intersection, Exclusive-or (XOR)
/*
*
* Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import javax.swing.JApplet;
import javax.swing.JFrame;
public class Pear extends JApplet {
Ellipse2D.Double circle, oval, leaf, stem;
Area circ, ov, leaf1, leaf2, st1, st2;
public void init() {
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);
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Dimension d = getSize();
int w = d.width;
int h = d.height;
double ew = w / 2;
double eh = h / 2;
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);
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);
}
public static void main(String s[]) {
JFrame f = new JFrame("Pear");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JApplet applet = new Pear();
f.getContentPane().add("Center", applet);
applet.init();
f.pack();
f.setSize(new Dimension(150, 200));
f.show();
}
}
Area.Subtract
import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AreaSubtracting extends JPanel {
DrawingCanvas canvas;
public AreaSubtracting() {
canvas = new DrawingCanvas();
add(canvas);
canvas.area1 = new Area(canvas.gp);
canvas.area1.subtract(canvas.area2);
canvas.repaint();
}
public static void main(String arg[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add("Center", new AreaSubtracting());
frame.pack();
frame.setSize(new Dimension(400, 300));
frame.setVisible(true);
}
class DrawingCanvas extends Canvas {
GeneralPath gp;
Ellipse2D ellipse;
Area area1, area2;
public DrawingCanvas() {
setBackground(Color.white);
setSize(350, 250); // width and height of canvas
int w = getWidth();
int h = getHeight();
gp = new GeneralPath();
gp.moveTo(w / 8, h / 2);
gp.lineTo(w / 2, h / 4);
gp.lineTo(7 * w / 8, h / 2);
gp.lineTo(w / 2, 3 * h / 4);
gp.closePath();
area1 = new Area(gp); // General path area object
ellipse = new Ellipse2D.Double(w / 4, h / 4, w / 2, h / 2);
area2 = new Area(ellipse); // Ellipse area object
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setStroke(new BasicStroke(2.0f));
g2D.draw(area1);
g2D.draw(area2);
}
}
}