Java Tutorial/2D Graphics/Curve — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 15:25, 31 мая 2010

CubicCurve2D.Float

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.CubicCurve2D;
public class MainClass extends Frame {
  public static void main(String[] args) {
    (new MainClass()).setVisible(true);
  }
  public MainClass() {
    super("Shape Sampler");
    setSize(400, 550);
  }
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    CubicCurve2D cubcurve = new CubicCurve2D.Float(30, 400, 150, 400, 200, 500, 350, 450);
    g2d.draw(cubcurve);
  }
}





QuadCurve2D.Float

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.QuadCurve2D;
public class ShapeSampler extends Frame {
  public static void main(String[] args) {
    (new ShapeSampler()).setVisible(true);
  }
  public ShapeSampler() {
    super("Shape Sampler");
    setSize(400, 550);
  }
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    QuadCurve2D quadcurve = new QuadCurve2D.Float(50, 300, 320, 300, 350, 400);
    g2d.draw(quadcurve);
  }
}





Select a Curve To Display Its Points and Flatness Index

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.util.Vector;
import javax.swing.JFrame;
public class QuadCurveControlPoint extends JFrame {
  DrawingCanvas canvas = new DrawingCanvas();
  public QuadCurveControlPoint() {
    getContentPane().add(canvas);
    setSize(500, 500);
    setVisible(true);
  }
  public static void main(String arg[]) {
    new QuadCurveControlPoint();
  }
  class DrawingCanvas extends Canvas {
    Vector quadCurves = new Vector();
    QuadCurve2D selectedCurve = null;
    Rectangle2D boundingRec = null;
    public DrawingCanvas() {
      setSize(400, 200);
      quadCurves.addElement(new QuadCurve2D.Float(20, 20, 80, 160, 120, 20));
      quadCurves.addElement(new QuadCurve2D.Float(120, 100, 160, 40, 200, 180));
      for (int i = 0; i < quadCurves.size(); i++) {
        QuadCurve2D curve = (QuadCurve2D) quadCurves.elementAt(i);
        selectedCurve = curve;
        boundingRec = curve.getBounds2D();
        System.out.println(selectedCurve.getX1());
        System.out.println(selectedCurve.getY1());
        System.out.println(selectedCurve.getCtrlX());
        System.out.println(selectedCurve.getCtrlY());
        System.out.println(selectedCurve.getX2());
        System.out.println(selectedCurve.getY2());
        double flatness = selectedCurve.getFlatness();
        System.out.println(flatness);
      }
    }
    public void paint(Graphics g) {
      Graphics2D g2D = (Graphics2D) g;
      for (int i = 0; i < quadCurves.size(); i++) {
        g2D.draw((QuadCurve2D) quadCurves.elementAt(i));
      }
      if (boundingRec != null) {
        drawHighlightSquares(g2D, boundingRec);
      }
    }
    public void drawHighlightSquares(Graphics2D g2D, Rectangle2D r) {
      double x = r.getX();
      double y = r.getY();
      double w = r.getWidth();
      double h = r.getHeight();
      g2D.setColor(Color.black);
      g2D.fill(new Rectangle.Double(x - 3.0, y - 3.0, 6.0, 6.0));
      g2D.fill(new Rectangle.Double(x + w * 0.5 - 3.0, y - 3.0, 6.0, 6.0));
    }
  }
}