-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProblem$14.java
90 lines (80 loc) · 2.88 KB
/
Problem$14.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package chapter_thirty_one;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
* *31.14 (Scale and rotate graphics) Write a program that enables the user to scale and rotate
* the STOP sign, as shown in Figure 31.53. The user can press the UP/DOWN arrow
* key to increase/decrease the size and press the RIGHT/LEFT arrow key to rotate
* left or right
*
*
* @author Sharaf Qeshta
* */
public class Problem$14 extends Application
{
@Override
public void start(Stage stage)
{
StackPane stackPane = new StackPane();
Polygon polygon = new Polygon();
stackPane.getChildren().add(polygon);
polygon.setFill(Color.RED);
polygon.setRotate(20);
ObservableList<Double> list = polygon.getPoints();
final double WIDTH = 200, HEIGHT = 200;
double centerX = WIDTH / 2, centerY = HEIGHT / 2;
double radius = Math.min(WIDTH, HEIGHT) * 0.4;
for (int i = 0; i < 8; i++)
{
list.add(centerX + radius * Math.cos(2 * i * Math.PI / 8));
list.add(centerY - radius * Math.sin(2 * i * Math.PI / 8));
}
Text text = new Text("STOP");
text.setFont(Font.font("Times new Roman", FontWeight.BOLD, 40));
text.setFill(Color.WHITE);
stackPane.getChildren().add(text);
Scene scene = new Scene(stackPane, 500, 500);
stage.setTitle("Problem$14");
stage.setScene(scene);
stage.show();
stackPane.requestFocus();
stackPane.setOnKeyPressed(e ->
{
if (e.getCode() == KeyCode.UP)
{
polygon.setScaleX(polygon.getScaleX() + 1);
polygon.setScaleY(polygon.getScaleY() + 1);
text.setScaleX(text.getScaleX() + 1);
text.setScaleY(text.getScaleY() + 1);
}
else if (e.getCode() == KeyCode.DOWN)
{
polygon.setScaleX(polygon.getScaleX() - 1);
polygon.setScaleY(polygon.getScaleY() - 1);
text.setScaleX(text.getScaleX() - 1);
text.setScaleY(text.getScaleY() - 1);
}
else if (e.getCode() == KeyCode.LEFT)
{
// go anticlockwise
polygon.setRotate(polygon.getRotate() - 1);
text.setRotate(text.getRotate() - 1);
}
else if (e.getCode() == KeyCode.RIGHT)
{
// go clockwise
polygon.setRotate(polygon.getRotate() + 1);
text.setRotate(text.getRotate() + 1);
}
});
}
}