Datasets:
ArXiv:
License:
File size: 2,033 Bytes
c574d3a |
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 |
package rd222dv_assign3.UpDown;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
public class UpDownPane extends BorderPane {
private class Position {
int x;
int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
}
private GridPane grid;
private Circle circle;
private int rows = 7;
private int col = 7;
private Position circlePos = new Position((rows - rows % 2) / 2, (col - col % 2) / 2);
public UpDownPane() {
grid = new GridPane();
grid.setStyle("-fx-background-color: #202020");
grid.setHgap(1);
grid.setVgap(1);
circle = new Circle(200, 150, 40, Color.YELLOW);
for (int i = 0; i < rows; i++)
for (int j = 0; j < col; j++) {
if (i == circlePos.x && j == circlePos.y)
grid.add(circle, i, j);
else {
Rectangle rect = new Rectangle(80, 80);
rect.setFill(Color.GREEN);
grid.add(rect, i, j);
}
}
this.setTop(grid);
}
public void handle(KeyEvent event) {
switch (event.getCode()) {
case UP: {
if (circlePos.y == 0)
circlePos.y = rows - 1;
else
circlePos.y--;
break;
}
case DOWN: {
if (circlePos.y == rows - 1)
circlePos.y = 0;
else
circlePos.y++;
break;
}
case LEFT: {
if (circlePos.x == 0)
circlePos.x = col - 1;
else
circlePos.x--;
break;
}
case RIGHT: {
if (circlePos.x == col - 1)
circlePos.x = 0;
else
circlePos.x++;
break;
}
default:
break;
}
grid.getChildren().removeAll(grid.getChildren());
for (int i = 0; i < rows; i++)
for (int j = 0; j < col; j++) {
if (i == circlePos.x && j == circlePos.y)
grid.add(circle, i, j);
else {
Rectangle rect = new Rectangle(80, 80);
rect.setFill(Color.GREEN);
grid.add(rect, i, j);
}
}
}
}
|