各種の図形をそれぞれ一つのオブジェクトとして高度な機能を持たせたクラスが抽象クラス javafx.scene.shape.Shape の派生クラスとして用意されている。
また、Path の構成要素として、抽象クラス javafx.scene.shape.PathElement の派生クラスが用意されている。
各図形は、その特徴量(Circle では中心座標 centerX, centerY と半径 radius)を DoubleProperty 型の変数としてもち、値が変化するとイベントが発生して再描画したり、他のクラスのプロパティと同期を取る(値を一致させる)事ができる。(後述)
Shape 共通の Property としては、塗りつぶしの色やパターン(fill), 境界線(stroke)があり、それぞれのセッター(例えば setFill(Color.RED)など)で特性値を設定できる。
Node 共通の Property としては、描画特性(layoutX, scaleY, opacity, rotate)、装飾(blendMode, effect)、GUI制御(cursor, focused, hover, onDragDetected, onMouseClicked, pressed)等がある。
例題1:JavaFXShapeTest.java
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class JavaFXShapeTest extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
root.setCenter( drawGraphics() ); //図形描画を一つのメソッドにまとめた一例
// デフォルト表示枠は中心に自動調整される。デフォルトの座標原点は固定されていない。
Scene scene = new Scene(root, 640, 480);
primaryStage.setTitle("JavaFX Graphics");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public Group drawGraphics(){ // Group に描画要素をまとめて返すメソッドを追加
Group group = new Group(); //Groupのインスタンスを生成し、その参照をgroupに代入
Rectangle rect = new Rectangle(300,200); // 原点に 300 x 200 の矩形を生成
rect.setFill(Color.AQUA); // 塗りつぶし色を設定
rect.setStroke(Color.color(0.2, .8, 0)); // 境界線の色をRGBで設定
rect.setStrokeWidth(20); // 境界線の幅を設定
rect.setStrokeType(StrokeType.INSIDE); // 境界線の表示形式を設定
group.getChildren().add(rect); // group の子ノードにrectを追加
Circle circ = new Circle(100); // 原点に半径100の円を生成
circ.setFill(Color.rgb(255, 255, 0, 0.5)); // 塗りつぶし色RGBと透明度の設定
circ.setStroke(Color.BLUE);
circ.getStrokeDashArray().addAll(25.,20.,5.,20.);// 境界線の破線間隔設定
group.getChildren().add(circ); // groupの子ノードにcircを追加
// 座標例示
ObservableList<Shape> shapes = FXCollections.observableArrayList();
for(int i=0; i<3; i++){
double x = i * 100;
for(int j=0; j<3; j++){
double y = j * 100;
Circle c = new Circle(x,y,30); // 座標(x,y)に半径100の円を生成
c.setFill(Color.color(1,0,0,0.5));
shapes.add(c);
Text t = new Text(x,y,"("+(int)x+","+(int)y+")");
shapes.add(t);
}
}
group.getChildren().addAll(shapes);
return group; // groupの参照値を返す
}
}
各種図形クラス(参照:JavaFX8 API Documents, javaSE-clienttechnologies)
クラス | 機能、使用例 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Arc | 弧:Arc(centerX, centerY, radiusX, radiusY, startAngle, length)
|
||||||||||||||||||
Circle | 円:Circle(centerX, centerY, radius, Paint fill)
|
||||||||||||||||||
CubicCurve | 3次ベジェ曲線 (Cubic Bézier curve) :CubicCurve(startX, startY, controlX1, controlY1, controlX2, controlY2, endX, endY)
|
||||||||||||||||||
Ellipse | 楕円:Ellipse(centerX, centerY, radiusX, radiusY)
|
||||||||||||||||||
Line | 線分:Line(startX, startY, endX, endY)
|
||||||||||||||||||
Path | パス(PathElement のリスト):Path(Collection<? extends PathElement> elements)
| ||||||||||||||||||
|
|||||||||||||||||||
Polygon | 多角形:Polylgon(x1,y1, x2,y2, x3,y3, ....)
|
||||||||||||||||||
Polyline | 折れ線:Polyline(x1,y1, x2,y2, x3,y3, ....)
|
||||||||||||||||||
QuadCurve | 2次曲線:QuadCurve(startX, startY, controlX, controlY, endX, endY)
|
||||||||||||||||||
Rectangle | 矩形:Rectangle(x, y, width, height)
|
||||||||||||||||||
SVGPath | Scalable Vector Graphics(SVG)のパスデータ:
|
||||||||||||||||||
Text | 文字列:Text(x, y, String text)
|
例題2:JavaFXShapeTest2.java
//drawGraphics()で利用するクラス
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.scene.shape.ClosePath;
import javafx.scene.shape.CubicCurve;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.HLineTo;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Polyline;
import javafx.scene.shape.QuadCurve;
import javafx.scene.shape.QuadCurveTo;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.SVGPath;
import javafx.scene.shape.StrokeType;
import javafx.scene.shape.VLineTo;
import javafx.scene.text.Text;
public class JavaFXShapeTest extends Application {
// 描画メソッドのみ修正(他は省略)
public Group drawGraphics() {
Group group = new Group();
Arc arc = new Arc(50, 50, 30, 20, 45, 270);
arc.setFill(Color.ANTIQUEWHITE);
arc.setType(ArcType.ROUND);
group.getChildren().add(arc);
Circle circle = new Circle(100, 100, 50, Color.RED);
group.getChildren().add(circle);
CubicCurve cubic = new CubicCurve(0, 50, 25, 0, 75, 100, 100, 50);
group.getChildren().add(cubic);
Ellipse ellipse = new Ellipse(50, 50, 50, 20);
ellipse.setFill(Color.rgb(255, 0, 0, 0.3));
group.getChildren().add(ellipse);
Line line = new Line(0, 0, 100, 100);
group.getChildren().add(line);
Path path = new Path(new MoveTo(200, 10), new LineTo(300, 200));
path.getElements().add(new ArcTo(100, 200, 30, 190, 20, true, true));
path.getElements().add(new CubicCurveTo(50, 100, 20, 190, 200, 150));
path.getElements().add(new QuadCurveTo(50, 100, 20, 190));
path.getElements().add(new HLineTo(150));
path.getElements().add(new VLineTo(250));
path.getElements().add(new ClosePath());
group.getChildren().add(path);
Polygon polygon = new Polygon(100, 100, 300, 100, 200, 300);
polygon.setFill(Color.color(0, 1, 0, 0.5));
group.getChildren().add(polygon);
Polyline polyline = new Polyline(100, 100, 300, 100, 200, 300);
group.getChildren().add(polyline);
QuadCurve quad = new QuadCurve(100, 200, 200, 100, 300, 200);
quad.setFill(Color.color(0, 0, 1, 0.5));
group.getChildren().add(quad);
Rectangle rect = new Rectangle(20, 100, 30, 200);
rect.setFill(Color.color(0, 1, 1, 0.5));
group.getChildren().add(rect);
SVGPath svg = new SVGPath();
svg.setContent("M20,180 Q50,0,100,100 T180,20");
svg.setFill(Color.color(.7,.2,0.6,.5));
group.getChildren().add(svg);
Text text = new Text(100, 300, "GRAPHICS");
group.getChildren().add(text);
return group;
}
}
参考資料:
- JavaFX 2で始めるGUI開発 第15回 基本的なシェイプ:ITPro Java技術最前線