Java by API/java.awt.geom/GeneralPath — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
Содержание
GeneralPath: closePath()
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Point2D.Float point = new Point2D.Float(100, 100); // store start point
GeneralPath p = new GeneralPath(GeneralPath.WIND_NON_ZERO);
p.moveTo(point.x, point.y);
p.lineTo(point.x + 20.0f, point.y - 5.0f); // Line from start to A
point = (Point2D.Float)p.getCurrentPoint();
p.lineTo(point.x + 5.0f, point.y - 20.0f); // Line from A to B
point = (Point2D.Float)p.getCurrentPoint();
p.lineTo(point.x + 5.0f, point.y + 20.0f); // Line from B to C
point = (Point2D.Float)p.getCurrentPoint();
p.lineTo(point.x + 20.0f, point.y + 5.0f); // Line from C to D
point = (Point2D.Float)p.getCurrentPoint();
p.lineTo(point.x - 20.0f, point.y + 5.0f); // Line from D to E
point = (Point2D.Float)p.getCurrentPoint();
p.lineTo(point.x - 5.0f, point.y + 20.0f); // Line from E to F
point = (Point2D.Float)p.getCurrentPoint();
p.lineTo(point.x - 5.0f, point.y - 20.0f); // Line from F to g
p.closePath(); // Line from G to start
g2D.draw(p);
}
}
public class Main {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
}
}
GeneralPath: curveTo(float x1, float y1, float x2, float y2, float x3, float y3)
import java.awt.geom.GeneralPath;
public class Main {
public static void main(String[] args) {
GeneralPath shape = new GeneralPath();
shape.moveTo(1, 1);
shape.lineTo(2, 2);
shape.quadTo(3, 3, 4, 4);
shape.curveTo(5, 5, 6, 6, 7, 7);
shape.closePath();
}
}
GeneralPath: lineTo(float x, float y)
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(300, 200);
jf.setVisible(true);
}
}
class MyCanvas extends JComponent {
Shape shape;
public MyCanvas() {
shape = create();
}
protected Shape create() {
GeneralPath path = new GeneralPath();
path.moveTo(50, 30);
path.lineTo(30, 200);
return path;
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.black);
g2.draw(shape);
}
}
GeneralPath: moveTo(float x, float y)
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.GeneralPath;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(300, 200);
jf.setVisible(true);
}
}
class MyCanvas extends JComponent {
Shape shape;
public MyCanvas() {
shape = create();
}
protected Shape create() {
GeneralPath path = new GeneralPath();
path.moveTo(50, 30);
path.lineTo(30, 200);
return path;
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.black);
g2.draw(shape);
}
}
GeneralPath: quadTo(float x1, float y1, float x2, float y2)
import java.awt.geom.GeneralPath;
public class Main {
public static void main(String[] args) {
GeneralPath shape = new GeneralPath();
shape.moveTo(1, 1);
shape.lineTo(2, 2);
shape.quadTo(3, 3, 4, 4);
shape.curveTo(5, 5, 6, 6, 7, 7);
shape.closePath();
}
}
GeneralPath.WIND_NON_ZERO
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
class MyCanvas extends JComponent {
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Point2D.Float point = new Point2D.Float(100, 100); // store start point
GeneralPath p = new GeneralPath(GeneralPath.WIND_NON_ZERO);
p.moveTo(point.x, point.y);
p.lineTo(point.x + 20.0f, point.y - 5.0f); // Line from start to A
point = (Point2D.Float)p.getCurrentPoint();
p.lineTo(point.x + 5.0f, point.y - 20.0f); // Line from A to B
point = (Point2D.Float)p.getCurrentPoint();
p.lineTo(point.x + 5.0f, point.y + 20.0f); // Line from B to C
point = (Point2D.Float)p.getCurrentPoint();
p.lineTo(point.x + 20.0f, point.y + 5.0f); // Line from C to D
point = (Point2D.Float)p.getCurrentPoint();
p.lineTo(point.x - 20.0f, point.y + 5.0f); // Line from D to E
point = (Point2D.Float)p.getCurrentPoint();
p.lineTo(point.x - 5.0f, point.y + 20.0f); // Line from E to F
point = (Point2D.Float)p.getCurrentPoint();
p.lineTo(point.x - 5.0f, point.y - 20.0f); // Line from F to g
p.closePath(); // Line from G to start
g2D.draw(p);
}
}
public class Main {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
}
}