0

我在画布上画了一条直线,并用纯色填充。我想用黑色边框与这条直线接壤。

4

2 回答 2

4

您可以使用两个不同大小和颜色的 fillRect

例子:

final Canvas canvas = new Canvas(250,250);
GraphicsContext gc = canvas.getGraphicsContext2D();

gc.setFill(Color.BLUE);
gc.fillRect(0,0,100,20);
gc.setFill(Color.RED);
gc.fillRect(1,1,98,18);
于 2014-12-09T11:32:30.383 回答
2

除了填充您的形状外,还要在图形上下文中指定一个笔划并要求它对形状进行笔划。

这是一个示例(改编自 Oracle Canvas 教程):

描边形状

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class CanvasStrokeDemo extends Application {
    @Override public void start(Stage stage) throws Exception {
        final Canvas canvas = new Canvas(250, 250);
        GraphicsContext gc = canvas.getGraphicsContext2D();

        // load the graphics context up with instructions to draw a shape.
        drawDShape(gc);

        // fill the shape.
        gc.setFill(Color.BLUE);
        gc.fill();

        // stroke an outline (border) around the shape.
        gc.setStroke(Color.GREEN);
        gc.setLineWidth(10);
        gc.stroke();

        stage.setScene(new Scene(new Group(canvas), Color.WHITE));    
        stage.show();
    }

    private void drawDShape(GraphicsContext gc) {
        gc.beginPath();
        gc.moveTo(50, 50);
        gc.bezierCurveTo(150, 20, 150, 150, 75, 150);
        gc.closePath();
    }

    public static void main(String[] args) { launch(args); }
}

GraphicsContext JavaDoc中有更多关于画布绘图 API 的信息。

于 2014-12-09T20:44:11.617 回答