下面列出了怎么用javafx.scene.shape.LineTo的API类实例代码及写法,或者点击链接到github查看源代码。
private static void applyPath(GraphicsContext pGraphics, Path pPath)
{
pGraphics.beginPath();
for(PathElement element : pPath.getElements())
{
if(element instanceof MoveTo)
{
pGraphics.moveTo(((int)((MoveTo) element).getX()) + 0.5, ((int)((MoveTo) element).getY()) + 0.5);
}
else if(element instanceof LineTo)
{
pGraphics.lineTo(((int)((LineTo) element).getX()) + 0.5, ((int)((LineTo) element).getY()) + 0.5);
}
else if (element instanceof QuadCurveTo)
{
QuadCurveTo curve = (QuadCurveTo) element;
pGraphics.quadraticCurveTo(((int)curve.getControlX())+0.5, ((int)curve.getControlY()) + 0.5,
((int) curve.getX()) + 0.5, ((int) curve.getY()) + 0.5);
}
}
}
protected void updatePath() {
getElements().clear();
final int n = Math.max(getRecursion(), 1);
final double flakeRadius = getRadius();
final double side = Math.abs(flakeRadius * Math.sqrt(3) / Math.pow(3, n));
xState = 0.0;
yState = 0.0;
angleState = 0.0;
getElements().add(new MoveTo(flakeRadius, 0.0f));
for (int i = 0; i < 3; i++) {
koch(n, side);
angleState -= 120.0;
}
getElements().add(new LineTo(flakeRadius, 0.0f));
setTranslateX(this.getCenterX() - flakeRadius);
setTranslateY(this.getCenterY() - flakeRadius);
}
private Path getCShape(Line pConnectionPoints)
{
final int x1 = Math.max(pConnectionPoints.getX1(), pConnectionPoints.getX2()) + ENDSIZE;
final int y1 = pConnectionPoints.getY1();
final int x2 = x1 + ENDSIZE;
final int y2 = pConnectionPoints.getY2();
final int ymid = (pConnectionPoints.getY1() + pConnectionPoints.getY2()) / 2;
MoveTo moveTo = new MoveTo(pConnectionPoints.getX1(), y1);
LineTo lineTo1 = new LineTo(x1, y1);
QuadCurveTo quadTo1 = new QuadCurveTo(x2, y1, x2, ymid);
QuadCurveTo quadTo2 = new QuadCurveTo(x2, y2, x1, y2);
LineTo lineTo2 = new LineTo(pConnectionPoints.getX2(), y2);
Path path = new Path();
path.getElements().addAll(moveTo, lineTo1, quadTo1, quadTo2, lineTo2);
return path;
}
private void drawChart(final List<Point> POINTS) {
if (POINTS.isEmpty()) return;
Point[] points = smoothing ? Helper.subdividePoints(POINTS.toArray(new Point[0]), 8) : POINTS.toArray(new Point[0]);
fillPath.getElements().clear();
fillPath.getElements().add(new MoveTo(0, height));
strokePath.getElements().clear();
strokePath.getElements().add(new MoveTo(points[0].getX(), points[0].getY()));
for (Point p : points) {
fillPath.getElements().add(new LineTo(p.getX(), p.getY()));
strokePath.getElements().add(new LineTo(p.getX(), p.getY()));
}
fillPath.getElements().add(new LineTo(width, height));
fillPath.getElements().add(new LineTo(0, height));
fillPath.getElements().add(new ClosePath());
if (dataPointsVisible) { drawDataPoints(POINTS, tile.isFillWithGradient() ? tile.getGradientStops().get(0).getColor() : tile.getBarColor()); }
}
public void renderCanvas(final Interval targetInterval, final Path canvas)
{
final double tX0 = targetInterval.min(0);
final double tX1 = targetInterval.max(0);
final double tY0 = targetInterval.min(1);
final double tY1 = targetInterval.max(1);
final double[] c000 = new double[] {tX0, tY0, 0};
final double[] c100 = new double[] {tX1, tY0, 0};
final double[] c010 = new double[] {tX0, tY1, 0};
final double[] c110 = new double[] {tX1, tY1, 0};
canvas.getElements().add(new MoveTo(perspectiveX(c000), perspectiveY(c000)));
canvas.getElements().add(new LineTo(perspectiveX(c100), perspectiveY(c100)));
canvas.getElements().add(new LineTo(perspectiveX(c110), perspectiveY(c110)));
canvas.getElements().add(new LineTo(perspectiveX(c010), perspectiveY(c010)));
canvas.getElements().add(new ClosePath());
}
private static Path processPath(final List<String> PATH_LIST, final PathReader READER) {
final Path PATH = new Path();
PATH.setFillRule(FillRule.EVEN_ODD);
while (!PATH_LIST.isEmpty()) {
if ("M".equals(READER.read())) {
PATH.getElements().add(new MoveTo(READER.nextX(), READER.nextY()));
} else if ("L".equals(READER.read())) {
PATH.getElements().add(new LineTo(READER.nextX(), READER.nextY()));
} else if ("C".equals(READER.read())) {
PATH.getElements().add(new CubicCurveTo(READER.nextX(), READER.nextY(), READER.nextX(), READER.nextY(), READER.nextX(), READER.nextY()));
} else if ("Q".equals(READER.read())) {
PATH.getElements().add(new QuadCurveTo(READER.nextX(), READER.nextY(), READER.nextX(), READER.nextY()));
} else if ("H".equals(READER.read())) {
PATH.getElements().add(new HLineTo(READER.nextX()));
} else if ("L".equals(READER.read())) {
PATH.getElements().add(new VLineTo(READER.nextY()));
} else if ("A".equals(READER.read())) {
PATH.getElements().add(new ArcTo(READER.nextX(), READER.nextY(), 0, READER.nextX(), READER.nextY(), false, false));
} else if ("Z".equals(READER.read())) {
PATH.getElements().add(new ClosePath());
}
}
return PATH;
}
/**
* @param from The index of the first character.
* @param to The index of the last character.
* @return An array with the PathElement objects which define an
* underline from the first to the last character.
*/
PathElement[] getUnderlineShape(int from, int to) {
// get a Path for the text underline
List<PathElement> result = new ArrayList<>();
PathElement[] shape = rangeShape( from, to );
// The shape is a closed Path for one or more rectangles AROUND the selected text.
// shape: [MoveTo origin, LineTo top R, LineTo bottom R, LineTo bottom L, LineTo origin, *]
// Extract the bottom left and right coordinates for each rectangle to get the underline path.
for ( int ele = 2; ele < shape.length; ele += 5 )
{
LineTo bl = (LineTo) shape[ele+1];
LineTo br = (LineTo) shape[ele];
double y = br.getY() - 2.5;
result.add( new MoveTo( bl.getX(), y ) );
result.add( new LineTo( br.getX(), y ) );
}
return result.toArray(new PathElement[0]);
}
private void smooth(final List<Double> DATA_LIST) {
Task<Point[]> smoothTask = new Task<Point[]>() {
@Override protected Point[] call() {
return Helper.smoothSparkLine(DATA_LIST, minValue, maxValue, graphBounds, noOfDatapoints);
}
};
smoothTask.setOnSucceeded(t -> {
Point[] smoothedPoints = smoothTask.getValue();
int lengthMinusOne = smoothedPoints.length - 1;
sparkLine.getElements().clear();
sparkLine.getElements().add(new MoveTo(smoothedPoints[0].getX(), smoothedPoints[0].getY()));
for (int i = 1 ; i < lengthMinusOne ; i++) {
sparkLine.getElements().add(new LineTo(smoothedPoints[i].getX(), smoothedPoints[i].getY()));
}
dot.setCenterX(smoothedPoints[lengthMinusOne].getX());
dot.setCenterY(smoothedPoints[lengthMinusOne].getY());
});
Thread smoothThread = new Thread(smoothTask);
smoothThread.setDaemon(true);
smoothThread.start();
}
private void smooth(final List<Double> DATA_LIST) {
Task<Point[]> smoothTask = new Task<Point[]>() {
@Override protected Point[] call() {
return Helper.smoothSparkLine(DATA_LIST, minValue, maxValue, graphBounds, noOfDatapoints);
}
};
smoothTask.setOnSucceeded(t -> {
Point[] smoothedPoints = smoothTask.getValue();
int lengthMinusOne = smoothedPoints.length - 1;
sparkLine.getElements().clear();
sparkLine.getElements().add(new MoveTo(smoothedPoints[0].getX(), smoothedPoints[0].getY()));
for (int i = 1 ; i < lengthMinusOne ; i++) {
sparkLine.getElements().add(new LineTo(smoothedPoints[i].getX(), smoothedPoints[i].getY()));
}
dot.setCenterX(smoothedPoints[lengthMinusOne].getX());
dot.setCenterY(smoothedPoints[lengthMinusOne].getY());
});
Thread smoothThread = new Thread(smoothTask);
smoothThread.setDaemon(true);
smoothThread.start();
}
@Override
public void flushPathElement(final PathElement elt) {
if(elt instanceof LineTo) {
final LineTo lineTo = (LineTo) elt;
lineTo.xProperty().unbind();
lineTo.yProperty().unbind();
}else {
if(elt instanceof MoveTo) {
final MoveTo moveTo = (MoveTo) elt;
moveTo.xProperty().unbind();
moveTo.yProperty().unbind();
}else {
if(elt instanceof CubicCurveTo) {
final CubicCurveTo cct = (CubicCurveTo) elt;
cct.xProperty().unbind();
cct.yProperty().unbind();
cct.controlX1Property().unbind();
cct.controlX2Property().unbind();
cct.controlY1Property().unbind();
cct.controlY2Property().unbind();
}
}
}
}
@Override
public Optional<PathElement> createPathElement(final PathElement elt) {
if(elt instanceof LineTo) {
return Optional.of(createLineTo(((LineTo) elt).getX(), ((LineTo) elt).getY()));
}
if(elt instanceof MoveTo) {
return Optional.of(createMoveTo(((MoveTo) elt).getX(), ((MoveTo) elt).getY()));
}
if(elt instanceof ClosePath) {
return Optional.of(createClosePath());
}
if(elt instanceof CubicCurveTo) {
final CubicCurveTo curve = (CubicCurveTo) elt;
return Optional.of(createCubicCurveTo(curve.getControlX1(), curve.getControlY1(), curve.getControlX2(), curve.getControlY2(), curve.getX(), curve.getY()));
}
return Optional.empty();
}
@Override protected void handleEvents(final String EVENT_TYPE) {
super.handleEvents(EVENT_TYPE);
if ("VISIBILITY".equals(EVENT_TYPE)) {
Helper.enableNode(titleText, !tile.getTitle().isEmpty());
Helper.enableNode(valueText, tile.isValueVisible());
Helper.enableNode(timeSpanText, !tile.isTextVisible());
redraw();
} else if ("VALUE".equals(EVENT_TYPE)) {
if(tile.isAnimated()) { tile.setAnimated(false); }
if (!tile.isAveragingEnabled()) { tile.setAveragingEnabled(true); }
double value = clamp(minValue, maxValue, tile.getValue());
addData(value);
handleCurrentValue(value);
} else if ("AVERAGING".equals(EVENT_TYPE)) {
noOfDatapoints = tile.getAveragingPeriod();
// To get smooth lines in the chart we need at least 4 values
if (noOfDatapoints < 4) throw new IllegalArgumentException("Please increase the averaging period to a value larger than 3.");
for (int i = 0; i < noOfDatapoints; i++) { dataList.add(minValue); }
pathElements.clear();
pathElements.add(0, new MoveTo());
for (int i = 1 ; i < noOfDatapoints ; i++) { pathElements.add(i, new LineTo()); }
sparkLine.getElements().setAll(pathElements);
redraw();
}
}
private void createNeedle() {
double needleWidth = size * 0.04;
double needleHeight = size * 0.4675;
needle.getElements().clear();
needle.getElements().add(new MoveTo(0.3125 * needleWidth, 0.015957446808510637 * needleHeight));
needle.getElements().add(new CubicCurveTo(0.3125 * needleWidth, 0.005319148936170213 * needleHeight,
0.4375 * needleWidth, 0.0,
0.5 * needleWidth, 0.0));
needle.getElements().add(new CubicCurveTo(0.5625 * needleWidth, 0.0,
0.6875 * needleWidth, 0.005319148936170213 * needleHeight,
0.6875 * needleWidth, 0.015957446808510637 * needleHeight));
needle.getElements().add(new CubicCurveTo(0.6875 * needleWidth, 0.015957446808510637 * needleHeight,
needleWidth, 0.9946808510638298 * needleHeight,
needleWidth, 0.9946808510638298 * needleHeight));
needle.getElements().add(new LineTo(0.0, 0.9946808510638298 * needleHeight));
needle.getElements().add(new CubicCurveTo(0.0, 0.9946808510638298 * needleHeight,
0.3125 * needleWidth, 0.015957446808510637 * needleHeight,
0.3125 * needleWidth, 0.015957446808510637 * needleHeight));
needle.getElements().add(new ClosePath());
needle.setFill(new LinearGradient(needle.getLayoutBounds().getMinX(), 0,
needle.getLayoutBounds().getMaxX(), 0,
false, CycleMethod.NO_CYCLE,
new Stop(0.0, gauge.getNeedleColor().darker()),
new Stop(0.5, gauge.getNeedleColor()),
new Stop(1.0, gauge.getNeedleColor().darker())));
}
private void drawAverage() {
double scaledWidth = width * 1.106;
double centerX = width * 0.5;
double centerY = height * 0.77;
double minValue = gauge.getMinValue();
// Draw average
average.getElements().clear();
double averageAngle = START_ANGLE - (gauge.getAverage() - minValue) * angleStep;
double averageSize = Helper.clamp(2.0, 2.5, 0.01 * scaledWidth);
double sinValue = Math.sin(Math.toRadians(averageAngle));
double cosValue = Math.cos(Math.toRadians(averageAngle));
average.getElements().add(new MoveTo(centerX + scaledWidth * 0.38 * sinValue, centerY + scaledWidth * 0.38 * cosValue));
sinValue = Math.sin(Math.toRadians(averageAngle - averageSize));
cosValue = Math.cos(Math.toRadians(averageAngle - averageSize));
average.getElements().add(new LineTo(centerX + scaledWidth * 0.35 * sinValue, centerY + scaledWidth * 0.35 * cosValue));
sinValue = Math.sin(Math.toRadians(averageAngle + averageSize));
cosValue = Math.cos(Math.toRadians(averageAngle + averageSize));
average.getElements().add(new LineTo(centerX + scaledWidth * 0.35 * sinValue, centerY + scaledWidth * 0.35 * cosValue));
average.getElements().add(new ClosePath());
average.setFill(gauge.getAverageColor());
average.setStroke(gauge.getTickMarkColor());
}
private void onMouseClicked(MouseEvent me) {
if (!me.getButton().equals(MouseButton.PRIMARY)) {
me.consume();
return;
}
if (Settings.getEditShape() == POLYGON) {
Point2D pt = imageView.parentToLocal(me.getX(), me.getY());
if (path == null) {
// Start a polygon shape with double-click or SHORTCUT + mouse click
if (!me.isShortcutDown() && me.getClickCount() != 2) {
me.consume();
return;
}
beginShape(pt);
}
else {
// End a polygon shape with double-click (and space key)
if (me.getClickCount() == 2) {
endShape(pt);
return;
}
path.getElements().add(new LineTo(pt.getX(), pt.getY()));
points.add(pt);
updatePath(pt);
}
}
me.consume();
}
private void initPath(final Path path) {
final ObservableList<PathElement> elts = path.getElements();
final MoveTo moveTo = pathProducer.createMoveTo(0d, 0d);
moveTo.xProperty().bind(model.getPtAt(0).xProperty());
moveTo.yProperty().bind(model.getPtAt(0).yProperty());
elts.add(moveTo);
IntStream.range(1, model.getNbPoints()).forEach(i -> {
final LineTo lineto = pathProducer.createLineTo(0d, 0d);
lineto.xProperty().bind(model.getPtAt(i).xProperty());
lineto.yProperty().bind(model.getPtAt(i).yProperty());
elts.add(lineto);
});
}
public static List<Double> getPolygonPoints(javafx.scene.shape.Path path) {
List<Double> points = new ArrayList(path.getElements().size());
IntStream.range(0, path.getElements().size()).forEach(idx -> {
var el = path.getElements().get(idx);
if (el instanceof MoveTo) {
points.add(((MoveTo) el).getX());
points.add(((MoveTo) el).getY());
}
else if (el instanceof LineTo) {
points.add(((LineTo) el).getX());
points.add(((LineTo) el).getY());
}
});
return points;
}
public SmartArrow() {
/* Create this arrow shape */
getElements().add(new MoveTo(0, 0));
getElements().add(new LineTo(-5, 5));
getElements().add(new MoveTo(0, 0));
getElements().add(new LineTo(-5, -5));
/* Add the corresponding css class */
styleProxy = new SmartStyleProxy(this);
styleProxy.addStyleClass("arrow");
}
public static Shape getToolBarShape(final double width, final double height, final double radii) {
final double centreX = 0.0;
final double centreY = 0.0;
final double halfWidth = 0.5 * width + 2 * radii;
final double halfHeight = 0.5 * height;
Path path = new Path();
// go to left-top most corner
path.getElements().add(new MoveTo(centreX - halfWidth - 4 * radii, centreY - halfHeight));
// cubic sweep down
path.getElements().add(new CubicCurveTo(//
centreX - halfWidth - 2 * radii, centreY - halfHeight, // first control point
centreX - halfWidth - 2 * radii, centreY + halfHeight, // second control point
centreX - halfWidth, centreY + halfHeight)); // to coordinate
// line on bottom
path.getElements().add(new LineTo(centreX + halfWidth, centreY + halfHeight));
// cubic sweep up
path.getElements().add(new CubicCurveTo(//
centreX + halfWidth + 2 * radii, centreY + halfHeight, // first control point
centreX + halfWidth + 2 * radii, centreY - halfHeight, // second control point
centreX + halfWidth + 4 * radii, centreY - halfHeight)); // to coordinate
// return to top left corner
path.getElements().add(new LineTo(centreX - halfWidth - 2 * radii, centreY - halfHeight));
return path;
}
private void updatePath(final MouseEvent event, final Bounds plotAreaBounds) {
final ObservableList<PathElement> path = crosshairPath.getElements();
path.clear();
path.add(new MoveTo(plotAreaBounds.getMinX() + 1, event.getY()));
path.add(new LineTo(plotAreaBounds.getMaxX(), event.getY()));
path.add(new MoveTo(event.getX(), plotAreaBounds.getMinY() + 1));
path.add(new LineTo(event.getX(), plotAreaBounds.getMaxY()));
}
private void koch(int n, double size) {
if (n == 0) {
xState += size * Math.cos(Math.toRadians(angleState));
yState += size * Math.sin(Math.toRadians(angleState));
this.getElements().add(new LineTo(xState, yState));
} else {
koch(n - 1, size);
angleState += 60.0;
koch(n - 1, size);
angleState -= 120.0;
koch(n - 1, size);
angleState += 60.0;
koch(n - 1, size);
}
}
@Override protected void handleEvents(final String EVENT_TYPE) {
super.handleEvents(EVENT_TYPE);
if ("VISIBILITY".equals(EVENT_TYPE)) {
Helper.enableNode(titleText, !tile.getTitle().isEmpty());
Helper.enableNode(text, tile.isTextVisible());
Helper.enableNode(valueText, tile.isValueVisible());
Helper.enableNode(unitText, !tile.getUnit().isEmpty());
Helper.enableNode(timeSpanText, !tile.isTextVisible());
Helper.enableNode(averageLine, tile.isAverageVisible());
Helper.enableNode(averageText, tile.isAverageVisible());
Helper.enableNode(stdDeviationArea, tile.isAverageVisible());
redraw();
} else if ("VALUE".equals(EVENT_TYPE)) {
if(tile.isAnimated()) { tile.setAnimated(false); }
if (!tile.isAveragingEnabled()) { tile.setAveragingEnabled(true); }
double value = clamp(minValue, maxValue, tile.getValue());
addData(value);
handleCurrentValue(value);
} else if ("AVERAGING".equals(EVENT_TYPE)) {
noOfDatapoints = tile.getAveragingPeriod();
// To get smooth lines in the chart we need at least 4 values
if (noOfDatapoints < 4) throw new IllegalArgumentException("Please increase the averaging period to a value larger than 3.");
for (int i = 0; i < noOfDatapoints; i++) { dataList.add(minValue); }
pathElements.clear();
pathElements.add(0, new MoveTo());
for (int i = 1 ; i < noOfDatapoints ; i++) { pathElements.add(i, new LineTo()); }
sparkLine.getElements().setAll(pathElements);
redraw();
}
}
@SuppressWarnings("unused")
private void smooth(final List<Double> DATA_LIST) {
int size = DATA_LIST.size();
Point[] points = new Point[size];
low = Statistics.getMin(DATA_LIST);
high = Statistics.getMax(DATA_LIST);
if (Helper.equals(low, high)) {
low = minValue;
high = maxValue;
}
range = high - low;
double minX = graphBounds.getX();
double maxX = minX + graphBounds.getWidth();
double minY = graphBounds.getY();
double maxY = minY + graphBounds.getHeight();
double stepX = graphBounds.getWidth() / (noOfDatapoints - 1);
double stepY = graphBounds.getHeight() / range;
for (int i = 0 ; i < size ; i++) {
points[i] = new Point(minX + i * stepX, maxY - Math.abs(low - DATA_LIST.get(i)) * stepY);
}
Point[] smoothedPoints = Helper.subdividePoints(points, 16);
int length = smoothedPoints.length;
sparkLine.getElements().clear();
sparkLine.getElements().add(new MoveTo(smoothedPoints[0].getX(), smoothedPoints[0].getY()));
for (int i = 1 ; i < length - 1 ; i++) {
sparkLine.getElements().add(new LineTo(smoothedPoints[i].getX(), smoothedPoints[i].getY()));
}
dot.setCenterX(smoothedPoints[length - 1].getX());
dot.setCenterY(smoothedPoints[length - 1].getY());
}
@Override protected void handleEvents(final String EVENT_TYPE) {
super.handleEvents(EVENT_TYPE);
if ("VISIBILITY".equals(EVENT_TYPE)) {
Helper.enableNode(titleText, !tile.getTitle().isEmpty());
Helper.enableNode(text, tile.isTextVisible());
Helper.enableNode(valueText, tile.isValueVisible());
Helper.enableNode(unitText, !tile.getUnit().isEmpty());
Helper.enableNode(timeSpanText, !tile.isTextVisible());
Helper.enableNode(averageLine, tile.isAverageVisible());
Helper.enableNode(averageText, tile.isAverageVisible());
Helper.enableNode(stdDeviationArea, tile.isAverageVisible());
redraw();
} else if ("VALUE".equals(EVENT_TYPE)) {
if (!tile.isAveragingEnabled()) { tile.setAveragingEnabled(true); }
addData(clamp(minValue, maxValue, tile.getValue()));
} else if ("AVERAGING".equals(EVENT_TYPE)) {
noOfDatapoints = tile.getAveragingPeriod();
// To get smooth lines in the chart we need at least 4 values
if (noOfDatapoints < 4) throw new IllegalArgumentException("Please increase the averaging period to a value larger than 3.");
for (int i = 0; i < noOfDatapoints; i++) { dataList.add(minValue); }
pathElements.clear();
pathElements.add(0, new MoveTo());
for (int i = 1 ; i < noOfDatapoints ; i++) { pathElements.add(i, new LineTo()); }
sparkLine.getElements().setAll(pathElements);
redraw();
} else if ("HIGHLIGHT_SECTIONS".equals(EVENT_TYPE)) {
boolean isHighlightSections = tile.isHighlightSections();
sectionCanvas.setVisible(!isHighlightSections);
sectionCanvas.setManaged(!isHighlightSections);
highlightSectionCanvas.setVisible(isHighlightSections);
highlightSectionCanvas.setManaged(isHighlightSections);
}
}
private void drawTriangle() {
MoveTo moveTo = new MoveTo(0, 0.028 * size);
LineTo lineTo1 = new LineTo(0.022 * size, 0);
LineTo lineTo2 = new LineTo(0.044 * size, 0.028 * size);
LineTo lineTo3 = new LineTo(0, 0.028 * size);
ClosePath closePath = new ClosePath();
triangle.getElements().setAll(moveTo, lineTo1, lineTo2, lineTo3, closePath);
}
private Path createStickManPath(Node pNode)
{
Path path = new Path();
int neckX = pNode.position().getX() + WIDTH / 2;
int neckY = pNode.position().getY() + HEAD_SIZE + PADDING;
int hipX = neckX;
int hipY = neckY + BODY_SIZE;
float dx = (float) (LEG_SIZE / Math.sqrt(2));
float feetX1 = hipX - dx;
float feetX2 = hipX + dx + 1;
float feetY = hipY + dx + 1;
path.getElements().addAll(
new MoveTo(neckX, neckY),
new QuadCurveTo(neckX + HEAD_SIZE / 2, neckY, neckX + HEAD_SIZE / 2, neckY - HEAD_SIZE / 2),
new QuadCurveTo(neckX + HEAD_SIZE / 2, neckY - HEAD_SIZE, neckX, neckY - HEAD_SIZE),
new QuadCurveTo(neckX - HEAD_SIZE / 2, neckY - HEAD_SIZE, neckX-HEAD_SIZE / 2, neckY - HEAD_SIZE / 2),
new QuadCurveTo(neckX - HEAD_SIZE / 2, neckY, neckX, neckY),
new LineTo(hipX, hipY),
new MoveTo(neckX - ARMS_SIZE / 2, neckY + BODY_SIZE / 3),
new LineTo(neckX + ARMS_SIZE / 2, neckY + BODY_SIZE / 3),
new MoveTo(feetX1, feetY),
new LineTo(hipX, hipY),
new LineTo(feetX2, feetY));
return path;
}
private double[] getXYFromPathElement(final PathElement ELEMENT) {
if (ELEMENT instanceof MoveTo) {
return new double[]{ ((MoveTo) ELEMENT).getX(), ((MoveTo) ELEMENT).getY() };
} else if (ELEMENT instanceof LineTo) {
return new double[] { ((LineTo) ELEMENT).getX(), ((LineTo) ELEMENT).getY() };
} else {
return new double[] { -1, -1 };
}
}
/**
* Node that represents the playing area/desktop where the puzzle pieces sit
*/
Desk(int numOfColumns, int numOfRows) {
setStyle("-fx-background-color: #cccccc; "
+ "-fx-border-color: #464646; "
+ "-fx-effect: innershadow( two-pass-box , rgba(0,0,0,0.8) , 15, 0.0 , 0 , 4 );");
double DESK_WIDTH = Piece.SIZE * numOfColumns;
double DESK_HEIGHT = Piece.SIZE * numOfRows;
setPrefSize(DESK_WIDTH, DESK_HEIGHT);
setMaxSize(DESK_WIDTH, DESK_HEIGHT);
autosize();
// create path for lines
Path grid = new Path();
grid.setStroke(Color.rgb(70, 70, 70));
getChildren().add(grid);
// create vertical lines
for (int col = 0; col < numOfColumns - 1; col++) {
grid.getElements().addAll(
new MoveTo(Piece.SIZE + Piece.SIZE * col, 5),
new LineTo(Piece.SIZE + Piece.SIZE * col, Piece.SIZE * numOfRows - 5));
}
// create horizontal lines
for (int row = 0; row < numOfRows - 1; row++) {
grid.getElements().addAll(
new MoveTo(5, Piece.SIZE + Piece.SIZE * row),
new LineTo(Piece.SIZE * numOfColumns - 5, Piece.SIZE + Piece.SIZE * row));
}
}
public PathSample() {
super(180,90);
// Create path shape - square
Path path1 = new Path();
path1.getElements().addAll(
new MoveTo(25, 25),
new HLineTo(65),
new VLineTo(65),
new LineTo(25, 65),
new ClosePath()
);
path1.setFill(null);
path1.setStroke(Color.RED);
path1.setStrokeWidth(2);
// Create path shape - curves
Path path2 = new Path();
path2.getElements().addAll(
new MoveTo(100, 45),
new CubicCurveTo(120, 20, 130, 80, 140, 45),
new QuadCurveTo(150, 0, 160, 45),
new ArcTo(20, 40, 0, 180, 45, true, true)
);
path2.setFill(null);
path2.setStroke(Color.DODGERBLUE);
path2.setStrokeWidth(2);
// show the path shapes;
getChildren().add(new Group(path1, path2));
// REMOVE ME
setControls(
new SimplePropertySheet.PropDesc("Path 1 Stroke", path1.strokeProperty()),
new SimplePropertySheet.PropDesc("Path 2 Stroke", path2.strokeProperty())
);
// END REMOVE ME
}
public PathSample() {
super(180,90);
// Create path shape - square
Path path1 = new Path();
path1.getElements().addAll(
new MoveTo(25, 25),
new HLineTo(65),
new VLineTo(65),
new LineTo(25, 65),
new ClosePath()
);
path1.setFill(null);
path1.setStroke(Color.RED);
path1.setStrokeWidth(2);
// Create path shape - curves
Path path2 = new Path();
path2.getElements().addAll(
new MoveTo(100, 45),
new CubicCurveTo(120, 20, 130, 80, 140, 45),
new QuadCurveTo(150, 0, 160, 45),
new ArcTo(20, 40, 0, 180, 45, true, true)
);
path2.setFill(null);
path2.setStroke(Color.DODGERBLUE);
path2.setStrokeWidth(2);
// show the path shapes;
getChildren().add(new Group(path1, path2));
// REMOVE ME
setControls(
new SimplePropertySheet.PropDesc("Path 1 Stroke", path1.strokeProperty()),
new SimplePropertySheet.PropDesc("Path 2 Stroke", path2.strokeProperty())
);
// END REMOVE ME
}