Created
December 4, 2013 16:16
-
-
Save skrb/7790355 to your computer and use it in GitHub Desktop.
Stroke のプロパティ
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
import java.util.stream.IntStream; | |
import javafx.application.Application; | |
import javafx.geometry.HPos; | |
import javafx.geometry.Insets; | |
import javafx.geometry.Pos; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.ColumnConstraints; | |
import javafx.scene.layout.GridPane; | |
import javafx.scene.paint.Color; | |
import javafx.scene.shape.LineTo; | |
import javafx.scene.shape.MoveTo; | |
import javafx.scene.shape.Path; | |
import javafx.scene.shape.StrokeLineCap; | |
import javafx.scene.shape.StrokeLineJoin; | |
import javafx.stage.Stage; | |
public class StrokeDemo extends Application { | |
@Override | |
public void start(Stage stage) { | |
GridPane root = new GridPane(); | |
root.setPrefSize(600, 500); | |
root.setPadding(new Insets(15.0)); | |
root.setAlignment(Pos.CENTER); | |
root.setVgap(20.0); | |
IntStream.range(0, 3).forEach(i -> { | |
ColumnConstraints cons = new ColumnConstraints(190); | |
cons.setHalignment(HPos.CENTER); | |
root.getColumnConstraints().add(cons); | |
}); | |
drawVariedStroke(root); | |
Scene scene = new Scene(root, 600, 500); | |
stage.setTitle("Stroke Demo"); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
private void drawVariedStroke(GridPane parent) { | |
// デフォルト (線幅: 1, 線色: 黒) | |
Path path = drawPath(); | |
parent.add(path, 0, 0); | |
Label label = new Label("widhth: 1 color: black"); | |
parent.add(label, 0, 1); | |
// 線幅: 10 | |
path = drawPath(); | |
path.setStrokeWidth(10.0); | |
parent.add(path, 1, 0); | |
label = new Label("widhth: 10 color: black"); | |
parent.add(label, 1, 1); | |
// 線幅: 10, 線色: 赤 | |
path = drawPath(); | |
path.setStrokeWidth(10.0); | |
path.setStroke(Color.RED); | |
parent.add(path, 2, 0); | |
label = new Label("widhth: 10 color: red"); | |
parent.add(label, 2, 1); | |
// 角のデフォルト (マイター接合) | |
Group group = new Group(); | |
path = drawThickPath(); | |
path.setStrokeLineJoin(StrokeLineJoin.MITER); | |
group.getChildren().add(path); | |
path = drawPath(); | |
path.setStroke(Color.WHITE); | |
group.getChildren().add(path); | |
parent.add(group, 0, 2); | |
label = new Label("line join: MITER"); | |
parent.add(label, 0, 3); | |
// 角を丸くする (ラウンド接合) | |
group = new Group(); | |
path = drawThickPath(); | |
path.setStrokeLineJoin(StrokeLineJoin.ROUND); | |
group.getChildren().add(path); | |
path = drawPath(); | |
path.setStroke(Color.WHITE); | |
group.getChildren().add(path); | |
parent.add(group, 1, 2); | |
label = new Label("line join: ROUND"); | |
parent.add(label, 1, 3); | |
// 角を削る (ベベル結合) | |
group = new Group(); | |
path = drawThickPath(); | |
path.setStrokeLineJoin(StrokeLineJoin.BEVEL); | |
group.getChildren().add(path); | |
path = drawPath(); | |
path.setStroke(Color.WHITE); | |
group.getChildren().add(path); | |
parent.add(group, 2, 2); | |
label = new Label("line join: BEVEL"); | |
parent.add(label, 2, 3); | |
// 線端のデフォルト (スクエア線端) | |
group = new Group(); | |
path = drawThickPath(); | |
path.setStrokeLineCap(StrokeLineCap.SQUARE); | |
group.getChildren().add(path); | |
path = drawPath(); | |
path.setStroke(Color.WHITE); | |
group.getChildren().add(path); | |
parent.add(group, 0, 4); | |
label = new Label("line cap: SQUARE"); | |
parent.add(label, 0, 5); | |
// 線端を丸くする (ラウンド線端) | |
group = new Group(); | |
path = drawThickPath(); | |
path.setStrokeLineCap(StrokeLineCap.ROUND); | |
group.getChildren().add(path); | |
path = drawPath(); | |
path.setStroke(Color.WHITE); | |
group.getChildren().add(path); | |
parent.add(group, 1, 4); | |
label = new Label("line cap: ROUND"); | |
parent.add(label, 1, 5); | |
// 線端を四角くする (BUTT線端) | |
group = new Group(); | |
path = drawThickPath(); | |
path.setStrokeLineCap(StrokeLineCap.BUTT); | |
group.getChildren().add(path); | |
path = drawPath(); | |
path.setStroke(Color.WHITE); | |
group.getChildren().add(path); | |
parent.add(group, 2, 4); | |
label = new Label("line cap: BUTT"); | |
parent.add(label, 2, 5); | |
// 点線 (線とスペースの長さが同じ) | |
path = drawPath(); | |
path.getStrokeDashArray().addAll(10.0); | |
path.setStrokeWidth(5.0); | |
parent.add(path, 0, 6); | |
label = new Label("dash: 10"); | |
parent.add(label, 0, 7); | |
// 点線 (線とスペースの長さが異なる) | |
path = drawPath(); | |
path.getStrokeDashArray().addAll(10.0, 5.0); | |
path.setStrokeWidth(5.0); | |
parent.add(path, 1, 6); | |
label = new Label("dash: 10, 5"); | |
parent.add(label, 1, 7); | |
// 一点鎖線 | |
path = drawPath(); | |
path.getStrokeDashArray().addAll(10.0, 2.0, 5.0, 2.0); | |
path.setStrokeWidth(5.0); | |
parent.add(path, 2, 6); | |
label = new Label("dash: 10, 2, 5, 2"); | |
parent.add(label, 2, 7); | |
} | |
private Path drawPath() { | |
Path path = new Path( | |
new MoveTo(40.0, 40.0), | |
new LineTo(140.0, 40.0), | |
new LineTo(40.0, 80.0) | |
); | |
return path; | |
} | |
private Path drawThickPath() { | |
Path path = drawPath(); | |
path.setFill(Color.PINK); | |
path.setStroke(Color.BLACK); | |
// 線の太さを10にする | |
path.setStrokeWidth(20.0); | |
return path; | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment