ITI0011:praktikum 11 N14

Allikas: Kursused
Redaktsioon seisuga 14. november 2014, kell 14:56 kasutajalt Ago (arutelu | kaastöö)
(erin) ←Vanem redaktsioon | Viimane redaktsiooni (erin) | Uuem redaktsioon→ (erin)
Mine navigeerimisribale Mine otsikasti

Tagasi ITI0011 lehele.

Üldine

Praktikum: 13.11.2014 kell 14:00

Kodutöö põhiosa realiseerimine. Vabakäejoone joonistamine JavaFX-iga.

Koodinäide

Kimp.java

<source lang="java">

import javafx.application.Application; import javafx.event.EventHandler; import javafx.event.EventType; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Polyline; import javafx.stage.Stage;

/**

* JavaFX application
* @author Ago
*
*/

public class Kimp extends Application {

/** * Program entry, here we just launch javafx application. * @param args */ public static void main(String[] args) { launch(args); }

@Override public void start(Stage primaryStage) throws Exception { /* * First we define main container * for our scene. We will be using BorderPane * which allows to set different components * on different borders or in the center. * For this example, even plain Pane would work * as we don't need anything else besides the main * area where to draw (we don't have any buttons etc). */ BorderPane root = new BorderPane(); /* * A new scene with root (BorderPane) set as the * main container. */ Scene scene = new Scene(root, 500, 500);

/* * As we already have a border pane, * a separate Pane is set to the center. * This separated pane is for drawing. */ Pane drawPane = new Pane(); root.setCenter(drawPane);

primaryStage.setScene(scene); primaryStage.show(); //System.exit(1);

// we could create new class which implements interface EventHandler

// class MyHandler implements EventHandler<MouseEvent> { // handle() // } // EventHandler<MouseEvent> myHandler = new MyHandler();

// instead we create the class and instantiate it "on-the-fly" /* * This handler doesn't to anything special, * it only prints out some text. Now your exercise is to * take the code examples from below (how to draw a line) * and put it into the handler. There are placeholders (as comments) * in the handle method. */ EventHandler<MouseEvent> myHandler = new EventHandler<MouseEvent>() {

@Override public void handle(MouseEvent event) { if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) { System.out.println("drag:" + event.getX() + ", " + event.getY()); // drag, TODO: // get the last line from out app ?? // add current point to the line } else if (event.getEventType() == MouseEvent.MOUSE_PRESSED) { System.out.println("press"); // press, TODO: // create a new line // add current point to the line // add line to our application (into draw pane) } } }; // let's add event handlers to the drawing pane drawPane.addEventHandler(MouseEvent.MOUSE_DRAGGED, myHandler); drawPane.addEventHandler(MouseEvent.MOUSE_PRESSED, myHandler);

// an example of how to use lambda (java 8) /*drawPane.addEventHandler(MouseEvent.MOUSE_DRAGGED, (event) -> { // draw line });*/

// an example of how to create a new handler implementation // and pass it to the method all in one place // (previously we had a variable for this handler, here we // don't need the variable - we just create the object and // pass it to the function) /*drawPane.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {

@Override public void handle(MouseEvent arg0) { // TODO Auto-generated method stub

} });*/

/* * The following section shows * how to draw a line (polyline). * * All those steps should be put * into the correct places in the handler (above). * */

// press: // create a new line Polyline line = new Polyline(); // add line to app drawPane.getChildren().add(line); // add a point to the line line.getPoints().add(10.0); // x line.getPoints().add(10.0); // y

// drag: // drawPane.getChildren() -> list // line = last line from getChildren() /* * While dragging, you need to * add points to the previously created line. * For that, you need to get the last line * in the children list. * getChildren() returns a list. You can * operate with it as with any regular list. * So, using .get() and .size(), you can * get the last line. */ // add another point to the line line.getPoints().add(100.0); // x line.getPoints().add(90.0); // y

// create another line line = new Polyline(); line.setStroke(Color.RED); drawPane.getChildren().add(line); line.getPoints().add(66.0); // x line.getPoints().add(12.0); // y

line.getPoints().add(67.0); // x line.getPoints().add(13.0); // y

line.getPoints().add(69.0); // x line.getPoints().add(15.0); // y }

</source>