Created
July 27, 2014 11:36
-
-
Save stoffie/1e1c5d021014ded480ee to your computer and use it in GitHub Desktop.
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
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package javafxapplication1; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import javafx.application.Application; | |
import javafx.beans.value.ChangeListener; | |
import javafx.beans.value.ObservableValue; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Insets; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.CheckBox; | |
import javafx.scene.control.ColorPicker; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.RadioButton; | |
import javafx.scene.control.Slider; | |
import javafx.scene.control.ToggleGroup; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.GridPane; | |
import javafx.scene.layout.StackPane; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.paint.Color; | |
import javafx.scene.shape.Circle; | |
import javafx.scene.shape.Polygon; | |
import javafx.scene.shape.Rectangle; | |
import javafx.scene.shape.Shape; | |
import javafx.stage.Stage; | |
abstract class Brush { | |
abstract Shape paint(double x, double y, int size); | |
} | |
class CircleBrush extends Brush { | |
@Override | |
Shape paint(double x, double y, int size) { | |
size = size / 2; | |
Circle c = new Circle(size); | |
c.setCenterX(x); | |
c.setCenterY(y); | |
return c; | |
} | |
} | |
class SquareBrush extends Brush { | |
@Override | |
Shape paint(double x, double y, int size) { | |
Rectangle r = new Rectangle(size, size); | |
r.setX(x - size/2); | |
r.setY(y - size/2); | |
return r; | |
} | |
} | |
class TriangleBrush extends Brush { | |
@Override | |
Shape paint(double x, double y, int size) { | |
Polygon p = new Polygon(0,size, size,size, size/2,0); | |
p.setTranslateX(x - size/2); | |
p.setTranslateY(y - size/2); | |
return p; | |
} | |
} | |
class DrawingArea extends StackPane { | |
int brushSize; | |
Brush currentBrush; | |
HashMap<Brush, ArrayList<Shape>> hashMap = new HashMap<>(); | |
HashMap<Brush, Boolean> visibilityMap = new HashMap<>(); | |
HashMap<Brush, Color> colorMap = new HashMap<>(); | |
DrawingArea() { | |
super(); | |
setStyle("-fx-background-color: #666333;"); | |
setOnMouseClicked(new EventHandler<MouseEvent>() { | |
@Override | |
public void handle(MouseEvent t) { | |
//System.out.println("" + t.getX() + "," + t.getY()); | |
Shape s = currentBrush.paint(t.getX(), t.getY(), brushSize); | |
s.setManaged(false); | |
s.setVisible(visibilityMap.get(currentBrush)); | |
s.setFill(colorMap.get(currentBrush)); | |
getChildren().add(s); | |
hashMap.get(currentBrush).add(s); | |
} | |
}); | |
} | |
void showBrush(Brush b, boolean show) { | |
visibilityMap.put(b, show); | |
Iterator<Shape> i = hashMap.get(b).iterator(); | |
while (i.hasNext()) { | |
i.next().setVisible(show); | |
} | |
} | |
void brushColor(Brush b, Color c) { | |
colorMap.put(b, c); | |
Iterator<Shape> i = hashMap.get(b).iterator(); | |
while (i.hasNext()) { | |
i.next().setFill(c); | |
} | |
} | |
} | |
class StatsWindow { | |
Stage stage = new Stage(); | |
Label statsCircles = new Label(); | |
Label statsSquares = new Label(); | |
Label statsTriangles = new Label(); | |
public StatsWindow() { | |
Label stats = new Label("Statistiche:"); | |
VBox vbox = new VBox(stats, statsCircles, statsSquares, statsTriangles); | |
vbox.setSpacing(10); | |
vbox.setPadding(new Insets(10)); | |
Scene scene = new Scene(vbox, 200, 150); | |
stage.setScene(scene); | |
} | |
public void show (int circles, int squares, int triangles) { | |
statsCircles.setText("Cerchi: " + circles); | |
statsSquares.setText("Quadrati: " + squares); | |
statsTriangles.setText("Triangoli: " + triangles); | |
stage.show(); | |
} | |
} | |
/** | |
* | |
* @author damiano | |
*/ | |
public class JavaFXApplication1 extends Application { | |
// gui | |
Label labelPennello = new Label("Scelta pennello:"); | |
Label labelComandi = new Label("Comandi:"); | |
Label labelSlider = new Label("Dimensione pennello:"); | |
Label lableShow = new Label("Mostra figure:"); | |
ColorPicker squareColor = new ColorPicker(); | |
ColorPicker triangleColor = new ColorPicker(); | |
ColorPicker circleColor = new ColorPicker(); | |
CheckBox chkSquares = new CheckBox("Quadrati"); | |
CheckBox chkTriangles = new CheckBox("Triangoli"); | |
CheckBox chkCircles = new CheckBox("Cerchi"); | |
Slider slider = new Slider(); | |
Button stats = new Button("Statistiche"); | |
Button reset = new Button("Reset"); | |
ToggleGroup group = new ToggleGroup(); | |
RadioButton rbCircle = new RadioButton("Cerchio"); | |
RadioButton rbTriangle = new RadioButton("Triangolo"); | |
RadioButton rbSquare = new RadioButton("Quadrato"); | |
GridPane grid = new GridPane(); | |
VBox vbox = new VBox( | |
labelPennello, | |
grid, | |
labelSlider, | |
slider, | |
lableShow, | |
chkCircles, | |
chkTriangles, | |
chkSquares, | |
labelComandi, | |
stats, | |
reset); | |
BorderPane root = new BorderPane(); | |
Scene scene = new Scene(root, 400, 400); | |
// drawing | |
DrawingArea da = new DrawingArea(); | |
StatsWindow statsWindow = new StatsWindow(); | |
Brush circleBrush = new CircleBrush(); | |
Brush squareBrush = new SquareBrush(); | |
Brush triangleBrush = new TriangleBrush(); | |
@Override | |
public void start(Stage primaryStage) { | |
initDrawingArea(); | |
initSideBar(); | |
initStage(); | |
} | |
void initDrawingArea() { | |
da.brushSize = 20; | |
da.hashMap.put(circleBrush, new ArrayList<>()); | |
da.hashMap.put(squareBrush, new ArrayList<>()); | |
da.hashMap.put(triangleBrush, new ArrayList<>()); | |
da.visibilityMap.put(circleBrush, Boolean.TRUE); | |
da.visibilityMap.put(squareBrush, Boolean.TRUE); | |
da.visibilityMap.put(triangleBrush, Boolean.TRUE); | |
da.colorMap.put(circleBrush, Color.WHITE); | |
da.colorMap.put(squareBrush, Color.WHITE); | |
da.colorMap.put(triangleBrush, Color.WHITE); | |
da.currentBrush = circleBrush; | |
} | |
void initSideBar() { | |
grid.add(rbCircle, 0, 0); | |
grid.add(circleColor, 1, 0); | |
grid.add(rbTriangle, 0, 1); | |
grid.add(triangleColor, 1, 1); | |
grid.add(rbSquare, 0, 2); | |
grid.add(squareColor, 1, 2); | |
grid.setVgap(10); | |
grid.setHgap(10); | |
circleColor.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
da.brushColor(circleBrush, circleColor.getValue()); | |
} | |
}); | |
squareColor.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
da.brushColor(squareBrush, squareColor.getValue()); | |
} | |
}); | |
triangleColor.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
da.brushColor(triangleBrush, triangleColor.getValue()); | |
} | |
}); | |
chkCircles.setSelected(true); | |
chkSquares.setSelected(true); | |
chkTriangles.setSelected(true); | |
chkCircles.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
da.showBrush(circleBrush, | |
chkCircles.selectedProperty().get()); | |
} | |
}); | |
chkSquares.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
da.showBrush(squareBrush, | |
chkSquares.selectedProperty().get()); | |
} | |
}); | |
chkTriangles.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
da.showBrush(triangleBrush, | |
chkTriangles.selectedProperty().get()); | |
} | |
}); | |
// slider affects the brush size | |
slider.setMin(5); | |
slider.setMax(200); | |
slider.setValue(10); | |
slider.setShowTickMarks(true); | |
slider.setShowTickLabels(true); | |
slider.valueProperty().addListener(new ChangeListener<Number>() { | |
@Override | |
public void changed(ObservableValue<? extends Number> ov, Number oldVal, Number newVal) { | |
da.brushSize = newVal.intValue(); | |
} | |
}); | |
// change brush according to the selected radio button | |
rbCircle.setToggleGroup(group); | |
rbCircle.setSelected(true); | |
rbTriangle.setToggleGroup(group); | |
rbSquare.setToggleGroup(group); | |
rbCircle.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
da.currentBrush = circleBrush; | |
} | |
}); | |
rbSquare.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
da.currentBrush = squareBrush; | |
} | |
}); | |
rbTriangle.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
da.currentBrush = triangleBrush; | |
} | |
}); | |
// show a popup window when the stat pubbon in pressed | |
stats.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
statsWindow.show( | |
da.hashMap.get(circleBrush).size(), | |
da.hashMap.get(squareBrush).size(), | |
da.hashMap.get(triangleBrush).size()); | |
} | |
}); | |
// reset the whole drawing area | |
reset.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent t) { | |
da.getChildren().clear(); | |
da.hashMap.get(circleBrush).clear(); | |
da.hashMap.get(triangleBrush).clear(); | |
da.hashMap.get(squareBrush).clear(); | |
} | |
}); | |
// vbox appearence | |
vbox.setSpacing(10); | |
vbox.setPadding(new Insets(10)); | |
vbox.setStyle("-fx-background-color: #EEEEEE;"); | |
} | |
void initStage() { | |
root.setCenter(da); | |
root.setLeft(vbox); | |
Stage primaryStage = new Stage(); | |
primaryStage.setTitle("Hello World!"); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
/** | |
* The main() method is ignored in correctly deployed JavaFX application. | |
* main() serves only as fallback in case the application can not be | |
* launched through deployment artifacts, e.g., in IDEs with limited FX | |
* support. NetBeans ignores main(). | |
* | |
* @param args the command line arguments | |
*/ | |
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