-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo1.java
271 lines (192 loc) · 6.73 KB
/
demo1.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package com.example.ColorSwitchGame;
import javafx.application.Application;
import javafx.stage.Stage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.event.EventHandler;
public class demo1 extends Application {
private Timeline t;
private int xSpeed = 2;
Scene scene2;
public boolean collision = false;
public void start(Stage first) throws FileNotFoundException {
Image image = new Image(new FileInputStream("C:\\Users\\Prasa\\Desktop\\download.png"));
//Setting the image view
ImageView imageView = new ImageView(image);
//Setting the position of the image
imageView.setX(200);
imageView.setY(400);
//setting the fit height and width of the image view
imageView.setFitHeight(700);
imageView.setFitWidth(400);
//Setting the preserve ratio of the image view
imageView.setPreserveRatio(true);
Group root = new Group(imageView);
Scene field = new Scene(root, 800, 800);
field.setFill(Color.BLACK);
Circle ball = new Circle(20);
ball.setFill(Color.WHITE);
// ball.setScaleX(300);
// ball.setScaleY(500);
ball.setCenterX(field.getHeight()/2);
ball.setCenterY(field.getWidth()/2);
root.getChildren().add(ball);
int position = 100;
field.setOnKeyReleased(e ->{ //event handler for space
switch (e.getCode()) {
case SPACE:
ball.setTranslateY(ball.getCenterY() + position);
break;
}
});
Button b = new Button();
b.setText("EXIT");
b.relocate(650, 700);
Button b1 = new Button();
b1.setText("PAUSE");
b1.relocate(500, 700);
root.getChildren().add(b1);
Button b2 = new Button();
b2.setText("PLAY");
b2.relocate(150, 700);
root.getChildren().add(b2);
Button b3 = new Button();
b3.setText("SAVED GAME");
b3.relocate(300, 700);
root.getChildren().add(b3);
//Creating Rectangle
Rectangle rect = new Rectangle(200, 100, 200, 200);
rect.relocate(300, 100);
// rect.setFill();
rect.setStroke(Color.WHITE);
rect.setFill(Color.TRANSPARENT);
rect.setStrokeWidth(5);
// rect.setVisible(false);
// root.getChildren().add(rect);
//Instantiating RotateTransition class
RotateTransition rotate = new RotateTransition();
//Setting Axis of rotation
rotate.setAxis(Rotate.Z_AXIS);
// setting the angle of rotation
rotate.setByAngle(360);
//setting cycle count of the rotation
rotate.setCycleCount(100);
//Setting duration of the transition
rotate.setDuration(Duration.seconds(10));
//the transition will be auto reversed by setting this to true
rotate.setAutoReverse(true);
// rotate.setNode(rect);
// rotate.play();
root.getChildren().add(rect);
first.setScene(field);
first.show();
b2.setOnAction(e-> {
// t.play();
if(e.getSource() == b2) {
// rect.setVisible(true);
rotate.setNode(rect);
rotate.play();
t.play();
imageView.setVisible(false);
}
});
b1.setOnAction(e->{
// t.pause();
if(e.getSource() == b1) {
rotate.pause();
t.pause();
}
});
Label label= new Label("Here All data will be store....");
VBox layout= new VBox(20);
layout.getChildren().add(label);
Group root1 = new Group();
b3.setOnAction(e -> first.setScene(scene2));
// scene2= new Scene(root1, 800, 800, Color.BLACK);
scene2= new Scene(layout, 800, 800, Color.BLACK);
root.getChildren().add(b);
b.setOnAction(e -> Platform.exit());
first.setTitle("Color Switch Game...");
//
field.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.SPACE) {
xSpeed=-xSpeed;
}
}
});
// pauseGame(btnPause,ball);
// startGame(btnStart,ball);
KeyFrame k = new KeyFrame(Duration.millis(8), e ->{ // speed..
moveBall(ball);
});
t = new Timeline(k);
t.setCycleCount(Animation.INDEFINITE);
}
private void moveBall(Circle ball){
ball.setCenterY(ball.getCenterY() - xSpeed);
if(ball.getCenterY()>=700 || ball.getCenterY()<=100){
xSpeed=-xSpeed;
}
}
// private void startGame(Button start, Circle ball){
// start.setOnAction(e->{
// t.play();
// });
// }
//
// private void pauseGame(Button pause, Circle ball){
// pause.setOnAction(e->{
// t.pause();
// });
// }
/*
public void Collision(Rectangle rectangle, Circle ball) {
Rectangle rect = bounds();
ball = new Circle(20);
if(ball.intersects(rect)) {
collision = true;
}
else {
collision = false;
}
}
public Rectangle bounds() {
return (new Rectangle(200, 100, 200, 200));
}
public class Ball extends Circle {
private String color;
public Ball(String color){
this.color = color;
}
public String getColor() {
return color;
}
}
*/
public static void main(String[] args){
Application.launch(args);
}
}