Erinevus lehekülje "ITI0011:praktikum 11 N8" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
(Uus lehekülg: 'Tagasi ITI0011 lehele. == Üldine == Praktikum: 13.11.2014 kell 8:00 == Koodinäide == ...')
 
8. rida: 8. rida:
 
== Koodinäide ==
 
== 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;
 +
 
 +
public class Kimp extends Application {
 +
 
 +
public static void main(String[] args) {
 +
launch(args);
 +
}
 +
 
 +
@Override
 +
public void start(Stage primaryStage) throws Exception {
 +
BorderPane root = new BorderPane();
 +
Scene scene = new Scene(root, 500, 500);
 +
 +
Pane drawPane = new Pane();
 +
root.setCenter(drawPane);
 +
 +
primaryStage.setScene(scene);
 +
primaryStage.show();
 +
// class MyHandler implements EventHandler<MouseEvent> {
 +
// handle()
 +
// }
 +
// EventHandler<MouseEvent> myHandler = new MyHandler();
 +
EventHandler<MouseEvent> myHandler = new EventHandler<MouseEvent>() {
 +
 
 +
@Override
 +
public void handle(MouseEvent event) {
 +
if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
 +
System.out.println("drag");
 +
// drag
 +
// 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
 +
// create a new line
 +
// add current point to the line
 +
// add line to our application (into draw pane)
 +
}
 +
}
 +
};
 +
 +
drawPane.addEventHandler(MouseEvent.MOUSE_DRAGGED, myHandler);
 +
drawPane.addEventHandler(MouseEvent.MOUSE_PRESSED, myHandler);
 +
/*drawPane.addEventHandler(MouseEvent.MOUSE_DRAGGED, (event) -> {
 +
// draw line
 +
});*/
 +
/*drawPane.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
 +
 
 +
@Override
 +
public void handle(MouseEvent arg0) {
 +
// TODO Auto-generated method stub
 +
 +
}
 +
});*/
 +
 +
// 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()
 +
 +
// 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>

Redaktsioon: 13. november 2014, kell 06:22

Tagasi ITI0011 lehele.

Üldine

Praktikum: 13.11.2014 kell 8:00


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;

public class Kimp extends Application {

public static void main(String[] args) { launch(args); }

@Override public void start(Stage primaryStage) throws Exception { BorderPane root = new BorderPane(); Scene scene = new Scene(root, 500, 500);

Pane drawPane = new Pane(); root.setCenter(drawPane);

primaryStage.setScene(scene); primaryStage.show(); // class MyHandler implements EventHandler<MouseEvent> { // handle() // } // EventHandler<MouseEvent> myHandler = new MyHandler(); EventHandler<MouseEvent> myHandler = new EventHandler<MouseEvent>() {

@Override public void handle(MouseEvent event) { if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) { System.out.println("drag"); // drag // 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 // create a new line // add current point to the line // add line to our application (into draw pane) } } };

drawPane.addEventHandler(MouseEvent.MOUSE_DRAGGED, myHandler); drawPane.addEventHandler(MouseEvent.MOUSE_PRESSED, myHandler); /*drawPane.addEventHandler(MouseEvent.MOUSE_DRAGGED, (event) -> { // draw line });*/ /*drawPane.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {

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

} });*/

// 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()

// 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>