top / index / prev / next / target / source
日記形式でつづる いがぴょんコラム ウェブページです。
警告アイコンを描画したい場合に利用できるサンプルです。任意のサイズで表示でき、また画像ファイルを同梱しないでよいのも魅力です。 ☆色合いに改善の余地があります。 [f:id:igapyon:20120920113148p:image]
package sample;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.ClosePath;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;
/**
* 警告アイコンを描画する JavaFX 2 サンプル
*/
public class Sample001 extends Application {
void drawErrorIcon(final int zoomRate, final Group root) {
final Path pathTriangle = new Path();
{
root.getChildren().add(pathTriangle);
pathTriangle.getElements().addAll(
new MoveTo(4.5 * zoomRate, 0.5 * zoomRate),
new CubicCurveTo(4.5 * zoomRate, 0.5 * zoomRate, 5 * zoomRate, 0 * zoomRate, 5.5 * zoomRate,
0.5 * zoomRate),
new LineTo(10 * zoomRate, 9 * zoomRate),
new CubicCurveTo(10 * zoomRate, 9 * zoomRate, 10 * zoomRate, 10 * zoomRate, 9 * zoomRate,
10 * zoomRate),
new LineTo(1 * zoomRate, 10 * zoomRate),
new CubicCurveTo(1 * zoomRate, 10 * zoomRate, 0 * zoomRate, 10 * zoomRate, 0 * zoomRate,
9 * zoomRate), new ClosePath());
pathTriangle.setStrokeWidth(0);
pathTriangle.setFill(Color.YELLOW);
}
{
final Light.Spot light = new Light.Spot();
light.setX(0);
light.setY(0);
light.setZ(15 * zoomRate);
final Lighting lighting = new Lighting();
lighting.setLight(light);
pathTriangle.setEffect(lighting);
}
{
final Circle circle = new Circle(5 * zoomRate, 3.8 * zoomRate, 0.8 * zoomRate);
root.getChildren().add(circle);
circle.setFill(Color.BLACK);
}
{
final Path path = new Path();
root.getChildren().add(path);
path.getElements().addAll(new MoveTo(5.8 * zoomRate, 3.8 * zoomRate),
new LineTo(5 * zoomRate, 7.5 * zoomRate), new LineTo(4.2 * zoomRate, 3.8 * zoomRate),
new ClosePath());
path.setStrokeWidth(0);
path.setFill(Color.BLACK);
}
{
final Circle circle = new Circle(5 * zoomRate, 8.5 * zoomRate, 0.7 * zoomRate);
root.getChildren().add(circle);
circle.setFill(Color.BLACK);
}
}
@Override
public void start(final Stage primaryStage) {
primaryStage.setTitle("Draw warning icon");
final Group root = new Group();
drawErrorIcon(10, root);
primaryStage.setScene(new Scene(root, 200, 200));
primaryStage.show();
}
public static void main(final String[] args) {
launch(args);
}
}