Created
June 26, 2016 10:21
-
-
Save CalebWhiting/b6d45db85b447e1d798983894491e683 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package demo; | |
| import com.sun.javafx.scene.*; | |
| import javafx.application.*; | |
| import javafx.event.Event; | |
| import javafx.event.*; | |
| import javafx.geometry.*; | |
| import javafx.scene.*; | |
| import javafx.scene.control.Button; | |
| import javafx.scene.input.*; | |
| import javafx.scene.layout.*; | |
| import javafx.stage.*; | |
| import javax.imageio.*; | |
| import java.awt.*; | |
| import java.awt.image.*; | |
| import java.io.*; | |
| import java.util.*; | |
| import java.util.List; | |
| /** | |
| * @author Caleb Whiting | |
| */ | |
| public class SplineAnalyser extends Application { | |
| private static final int BUTTON_COUNT = 3; | |
| private final Random rng = new Random(); | |
| private boolean tracking = false; | |
| private List<Point2D> spline = new LinkedList<>(); | |
| private Pane root; | |
| private Button[] buttons = new Button[BUTTON_COUNT]; | |
| private Point2D[][] splines = new Point2D[buttons.length][]; | |
| private Button start; | |
| public static void main(String[] args) { | |
| launch(args); | |
| } | |
| @Override | |
| public void start(Stage stage) { | |
| stage.setEventDispatcher(new SceneEventDispatcher(stage) { | |
| @Override | |
| public Event dispatchEvent(Event event, EventDispatchChain tail) { | |
| if (tracking && event.getEventType() == MouseEvent.MOUSE_MOVED) { | |
| MouseEvent m = (MouseEvent) event; | |
| spline.add(new Point2D(m.getX(), m.getY())); | |
| } | |
| if (event.getEventType() == WindowEvent.WINDOW_CLOSE_REQUEST) { | |
| System.exit(0); | |
| } | |
| return super.dispatchEvent(event, tail); | |
| } | |
| }); | |
| root = new Pane(); | |
| start = new Button("Click to start"); | |
| root.getChildren().setAll(start); | |
| start.setOnAction(evt -> { | |
| tracking = true; | |
| root.getChildren().clear(); | |
| startUserInput(root); | |
| }); | |
| stage.setTitle("Spline Analyser"); | |
| stage.setScene(new Scene(root, 600,600)); | |
| stage.show(); | |
| } | |
| private void startUserInput(Pane inputPane) { | |
| for (int i = 0; i < buttons.length; i++) { | |
| Button button = buttons[i] = new Button("Click me!"); | |
| button.setTranslateX(rng.nextInt((int) (inputPane.getWidth() - button.getWidth()))); | |
| button.setTranslateY(rng.nextInt((int) (inputPane.getHeight() - button.getHeight()))); | |
| button.setVisible(i == 0); | |
| final int index = i; | |
| button.setOnAction(evt -> { | |
| button.setVisible(false); | |
| splines[index] = spline.toArray(new Point2D[spline.size()]); | |
| spline.clear(); | |
| if (index == buttons.length - 1) { | |
| button.setVisible(false); | |
| inputPane.getChildren().setAll(start); | |
| tracking = false; | |
| writeSplinesToImage(inputPane); | |
| return; | |
| } | |
| buttons[index + 1].setVisible(true); | |
| }); | |
| } | |
| inputPane.getChildren().setAll(buttons); | |
| } | |
| private void writeSplinesToImage(Pane pane) { | |
| BufferedImage image = new BufferedImage((int) pane.getWidth(), (int) pane.getHeight(), BufferedImage.TYPE_INT_RGB); | |
| Graphics2D g = (Graphics2D) image.getGraphics(); | |
| g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
| g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | |
| g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); | |
| g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); | |
| g.setColor(Color.BLACK); | |
| g.fillRect(0, 0, image.getWidth(), image.getHeight()); | |
| Button[] buttons = new Button[this.buttons.length + 1]; | |
| buttons[0] = start; | |
| System.arraycopy(this.buttons, 0, buttons, 1, this.buttons.length); | |
| g.setColor(new Color(255, 255, 255, 50)); | |
| for (Button button : buttons) { | |
| int x = (int) (button.getLayoutX() + button.getTranslateX()); | |
| int y = (int) (button.getLayoutY() + button.getTranslateY()); | |
| g.drawRect(x, y, (int) button.getWidth(), (int) button.getHeight()); | |
| } | |
| Point2D[][] splines1 = this.splines; | |
| for (int i = 0; i < splines1.length; i++) { | |
| g.setColor(i % 2 == 0 ? Color.ORANGE : Color.CYAN); | |
| Point2D[] spline = splines1[i]; | |
| for (Point2D pt : spline) { | |
| g.drawLine((int) pt.getX(), (int) pt.getY(), (int) pt.getX(), (int) pt.getY()); | |
| } | |
| } | |
| try { | |
| ImageIO.write(image, "png", new File("spline-analyzer.png")); | |
| System.out.println("Written to file!"); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment