/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
* Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved.
* Use is subject to license terms.
*
* This file is available and licensed under the following license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Oracle Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.shape.StrokeLineJoin;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.control.Button;
def APP_PADDING = 4;
def CONTENT_PADDING = 4;
def Rect_Padding = 1;
def D = 22;
def circle_widthstroke = 2;
def BG = Color.WHITE;
def Padding = 2;
def Line_Padding = 2;
def FG = Color.BLACK;
def scene_width = 600;
def scene_height = 400;
def COLORS = [
Color.RED,
Color.ORANGE,
Color.YELLOW,
Color.GREEN,
Color.LIGHTBLUE,
Color.BLUE,
Color.MAGENTA,
Color.BLACK
];
var COLOR = Color.BLACK;
var SIZE = 1;
var buttons: HBox;
def canvas = Group {}
function undo() {
def index = sizeof canvas.content;
if (index > 0) {
delete canvas.content[index - 1] from canvas.content
}
}
Stage {
title: "Draw (JavaFX Draw)"
scene: Scene {
width: scene_width
height: scene_height
content: [
Rectangle {
fill: BG
width: bind canvas.scene.width - Rect_Padding
height: bind canvas.scene.height - Rect_Padding
var path: Path;
onMousePressed: function(mouse) {
path = Path {
stroke: COLOR
strokeWidth: SIZE
strokeLineCap: StrokeLineCap.ROUND
strokeLineJoin: StrokeLineJoin.ROUND
elements: MoveTo { x: mouse.x y: mouse.y }
}
insert path into canvas.content
}
onMouseDragged: function(mouse) {
insert LineTo { x: mouse.x y: mouse.y } into path.elements
}
onKeyPressed: function(key) {
if (key.controlDown and key.code == KeyCode.VK_Z) {
undo()
}
}
}
canvas,
VBox {
layoutX: APP_PADDING
layoutY: APP_PADDING
spacing: CONTENT_PADDING
content: [
HBox {
spacing: CONTENT_PADDING
content: for (color in COLORS) Rectangle {
fill: color
width: D
height: D
blocksMouse: true
cursor: Cursor.HAND
onMousePressed: function(e) {
COLOR = color
}
}
}
HBox {
spacing: CONTENT_PADDING
content: for (size in [1..5]) Group {
content: [
Circle {
strokeWidth: circle_widthstroke
stroke: bind if (SIZE == size) then FG else null
fill: BG
radius: D/Padding
blocksMouse: true
cursor: Cursor.HAND
onMousePressed: function(e) {
SIZE = size
}
}
Line {
def o = (D - size)/Padding - Line_Padding;
strokeWidth: size
stroke: bind COLOR
strokeLineCap: StrokeLineCap.ROUND
startX: -o
endX: o
}
]
}
}
]
}
buttons = HBox {
layoutX: APP_PADDING
layoutY: bind canvas.scene.height - buttons.height - APP_PADDING
spacing: CONTENT_PADDING
content: [
Button {
text: "Clear"
cursor: Cursor.HAND
focusTraversable: false
onMousePressed: function(e) {
delete canvas.content
}
},
Button {
focusTraversable: false
text: "Undo"
cursor: Cursor.HAND
onMousePressed: function(e) {
undo()
}
}
]
}
]
}
}