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

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
(Uus lehekülg: 'Tagasi ITI0011 lehele. == Üldine == Praktikum: 30.10.2014 kell 8:00 Teemad: * JavaFX * JavaFX klassi arvutites ** https://courses.cs.ttu.ee/pages/ITI0011:JavaFX#JavaFX_ka...')
 
 
15. rida: 15. rida:
 
== Koodinäite ==
 
== Koodinäite ==
  
 +
<source lang="java">
 +
import javafx.application.Application;
 +
import javafx.event.EventHandler;
 +
import javafx.geometry.Point2D;
 +
import javafx.scene.Node;
 +
import javafx.scene.Scene;
 +
import javafx.scene.control.Menu;
 +
import javafx.scene.control.MenuBar;
 +
import javafx.scene.control.MenuItem;
 +
import javafx.scene.input.MouseEvent;
 +
import javafx.scene.layout.BorderPane;
 +
import javafx.scene.layout.Pane;
 +
import javafx.scene.paint.Color;
 +
import javafx.scene.shape.Circle;
 +
import javafx.scene.shape.Polyline;
 +
import javafx.stage.Stage;
 +
 +
public class Main extends Application {
 +
 +
class MyMouseHandler implements EventHandler<MouseEvent> {
 +
private Pane main;
 +
 +
public MyMouseHandler(Pane main) {
 +
this.main = main;
 +
}
 +
 +
@Override
 +
public void handle(MouseEvent event) {
 +
if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
 +
//event.getButton();
 +
Circle c = new Circle(event.getX(), event.getY(), 20);
 +
c.setFill(Color.TRANSPARENT);
 +
c.setStroke(Color.GREEN);
 +
main.getChildren().add(c);
 +
} else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
 +
Point2D pmouse = new Point2D(event.getX(), event.getY());
 +
for (Node n : main.getChildren()) {
 +
if (n instanceof Circle) {
 +
Circle c = (Circle)n;
 +
Point2D pc = new Point2D(c.getCenterX(), c.getCenterY());
 +
double r = pc.distance(pmouse);
 +
c.setRadius(r);
 +
}
 +
}
 +
}
 +
}
 +
 +
}
 +
 +
public static void main(String[] args) {
 +
launch(args);
 +
}
 +
 +
@Override
 +
public void start(Stage primaryStage) throws Exception {
 +
BorderPane borderPane = new BorderPane();
 +
Scene scene = new Scene(borderPane, 400, 400);
 +
 +
Pane main = new Pane();
 +
borderPane.setCenter(main);
 +
 +
MenuBar menuBar = new MenuBar();
 +
Menu menuFile = new Menu("File");
 +
MenuItem menuFileSave = new MenuItem("Save");
 +
menuBar.getMenus().add(menuFile);
 +
menuFile.getItems().add(menuFileSave);
 +
 +
borderPane.setBottom(menuBar);
 +
 +
Circle c = new Circle(100, 100, 50);
 +
c.setFill(Color.TRANSPARENT);
 +
c.setStroke(Color.RED);
 +
main.getChildren().add(c);
 +
 +
Polyline line = new Polyline();
 +
line.setStroke(Color.BLUE);
 +
line.getPoints().add(10.0); // x
 +
line.getPoints().add(10.0); // y
 +
line.getPoints().add(100.0); // x
 +
line.getPoints().add(100.0); // y
 +
 +
main.getChildren().add(line);
 +
 +
primaryStage.setScene(scene);
 +
primaryStage.show();
 +
 +
EventHandler<MouseEvent> mouseHandler = new MyMouseHandler(main);
 +
main.setOnMousePressed(mouseHandler);
 +
main.setOnMouseDragged(mouseHandler);
 +
}
 +
 +
}
 +
 +
</source>
  
 
== Exercise ==
 
== Exercise ==

Viimane redaktsioon: 30. oktoober 2014, kell 06:39

Tagasi ITI0011 lehele.

Üldine

Praktikum: 30.10.2014 kell 8:00

Teemad:

Koodinäite

<source lang="java"> import javafx.application.Application; import javafx.event.EventHandler; import javafx.geometry.Point2D; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.input.MouseEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Polyline; import javafx.stage.Stage;

public class Main extends Application {

class MyMouseHandler implements EventHandler<MouseEvent> { private Pane main;

public MyMouseHandler(Pane main) { this.main = main; }

@Override public void handle(MouseEvent event) { if (event.getEventType() == MouseEvent.MOUSE_PRESSED) { //event.getButton(); Circle c = new Circle(event.getX(), event.getY(), 20); c.setFill(Color.TRANSPARENT); c.setStroke(Color.GREEN); main.getChildren().add(c); } else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) { Point2D pmouse = new Point2D(event.getX(), event.getY()); for (Node n : main.getChildren()) { if (n instanceof Circle) { Circle c = (Circle)n; Point2D pc = new Point2D(c.getCenterX(), c.getCenterY()); double r = pc.distance(pmouse); c.setRadius(r); } } } }

}

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

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

Pane main = new Pane(); borderPane.setCenter(main);

MenuBar menuBar = new MenuBar(); Menu menuFile = new Menu("File"); MenuItem menuFileSave = new MenuItem("Save"); menuBar.getMenus().add(menuFile); menuFile.getItems().add(menuFileSave);

borderPane.setBottom(menuBar);

Circle c = new Circle(100, 100, 50); c.setFill(Color.TRANSPARENT); c.setStroke(Color.RED); main.getChildren().add(c);

Polyline line = new Polyline(); line.setStroke(Color.BLUE); line.getPoints().add(10.0); // x line.getPoints().add(10.0); // y line.getPoints().add(100.0); // x line.getPoints().add(100.0); // y

main.getChildren().add(line);

primaryStage.setScene(scene); primaryStage.show();

EventHandler<MouseEvent> mouseHandler = new MyMouseHandler(main); main.setOnMousePressed(mouseHandler); main.setOnMouseDragged(mouseHandler); }

}

</source>

Exercise

Create an application which allows the user to draw a circle. There are several options how to achieve this:

  • using mouse dragging: the center point is fixed when the dragging starts (mouse press), the radius is changed while dragging. if the dragging ends, the radius is fixed. during the dragging, the circle could be visible.
  • using clicking: when clicking first time, the center point is fixed, when clicking another time, the radius is fixed (and circle is drawn).
  • some other way

Optional requirements:

  • more than one circles can be drawn