Есть ли способ упростить мой код внизу? В основном я рисую серию прямоугольников друг против друга при перетаскивании мышью. Отрицательные числа считываются из 2D-массива в другом классе (Board). Я использую положительные числа для точек (вам нужно соединить точки с балками / блоками) также в другом классе. Вот почему я использую отрицательные числа. Каждый case в операторе switch выполняет одно и то же действие, за исключением выбора цвета: setFill (Color.) Диапазон значений от -1 до -9. Я публикую только 2 случая, чтобы уменьшить код на этой странице. Вы можете четко видеть повторение кода. Я новичок в программировании с этого года.
view.getCanvas().setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
System.out.println("mouse dragged");
event.setDragDetect(false);
dragx = (int) (event.getX() / board.getGRIDWIDTH());
dragy = (int) (event.getY() / board.getGRIDWIDTH());
if (dragx >= 0 && dragy >= 0 &&
dragy < board.getBoard().length && dragx < board.getBoard()[0].length && board.getBoard()[dragy][dragx] == 0) {
if ((dragx == currentx + 1 && dragy == currenty) ||
(dragx == currentx - 1 && dragy == currenty) ||
(dragx == currentx && dragy == currenty + 1) ||
(dragx == currentx && dragy == currenty - 1)) {
board.getBoard()[dragy][dragx] = -board.getBoard()[starty][startx];
currentx = dragx;
currenty = dragy;
}
}
GraphicsContext gc = view.getCanvas().getGraphicsContext2D();
for (int i = 0; i < board.getBoard().length; i++) { //row
for (int j = 0; j < board.getBoard()[i].length; j++) { //column
pipex = i * board.getGRIDWIDTH();
pipey = j * board.getGRIDWIDTH();
boolean left = false;
boolean right = false;
boolean above = false;
boolean below = false;
if (i > 0 && Math.abs(board.getBoard()[j][i]) == Math.abs(board.getBoard()[j][i - 1])) {
left = true;
}
if (j > 0 && Math.abs(board.getBoard()[j][i]) == Math.abs(board.getBoard()[j - 1][i])) {
above = true;
}
if (i < board.getBoard().length - 1 && Math.abs(board.getBoard()[j][i]) == Math.abs(board.getBoard()[j][i + 1])) {
right = true;
}
if (j < board.getBoard().length - 1 && Math.abs(board.getBoard()[j][i]) == Math.abs(board.getBoard()[j + 1][i])) {
below = true;
}
switch (board.getBoard()[j][i]) {
case -1:
gc.setFill(Color.RED);
gc.fillRect(pipex + model.getRectOffset(), pipey + model.getRectOffset(), model.getPipeWidth(), model.getPipeWidth());
if (above && below) {
gc.fillRect(pipex + model.getRectOffset(), pipey, model.getPipeWidth(), board.getGRIDWIDTH());
}
if (left && right) {
gc.fillRect(pipex, pipey + model.getRectOffset(), board.getGRIDWIDTH(), model.getPipeWidth());
}
if (above && left) {
gc.fillRect(pipex, pipey + model.getRectOffset(), model.getPipeWidth(), model.getPipeWidth());
gc.fillRect(pipex + model.getRectOffset(), pipey, model.getPipeWidth(), model.getPipeWidth());
}
if (above && right) {
gc.fillRect(pipex + (board.getGRIDWIDTH() / 2), pipey + model.getRectOffset(), model.getPipeWidth(), model.getPipeWidth());
gc.fillRect(pipex + model.getRectOffset(), pipey, model.getPipeWidth(), model.getPipeWidth());
}
if (below && left) {
gc.fillRect(pipex + model.getRectOffset(), pipey + (board.getGRIDWIDTH() - model.getRectOffset()), model.getPipeWidth(), (board.getGRIDWIDTH() - model.getRectOffset()));
gc.fillRect(pipex, pipey + model.getRectOffset(), (board.getGRIDWIDTH() - model.getRectOffset()), model.getPipeWidth());
}
if (below && right) {
gc.fillRect(pipex + model.getRectOffset(), pipey + (board.getGRIDWIDTH() - model.getRectOffset()), model.getPipeWidth(), (board.getGRIDWIDTH() - model.getRectOffset()));
gc.fillRect(pipex + model.getRectOffset(), pipey + model.getRectOffset(), (board.getGRIDWIDTH() - model.getRectOffset()), model.getPipeWidth());
}
break;
case -2:
gc.setFill(Color.BLUE);
gc.fillRect(pipex + model.getRectOffset(), pipey + model.getRectOffset(), model.getPipeWidth(), model.getPipeWidth());
if (above && below) {
gc.fillRect(pipex + model.getRectOffset(), pipey, model.getPipeWidth(), board.getGRIDWIDTH());
}
if (left && right) {
gc.fillRect(pipex, pipey + model.getRectOffset(), board.getGRIDWIDTH(), model.getPipeWidth());
}
if (above && left) {
gc.fillRect(pipex, pipey + model.getRectOffset(), model.getPipeWidth(), model.getPipeWidth());
gc.fillRect(pipex + model.getRectOffset(), pipey, model.getPipeWidth(), model.getPipeWidth());
}
if (above && right) {
gc.fillRect(pipex + (board.getGRIDWIDTH() / 2), pipey + model.getRectOffset(), model.getPipeWidth(), model.getPipeWidth());
gc.fillRect(pipex + model.getRectOffset(), pipey, model.getPipeWidth(), model.getPipeWidth());
}
if (below && left) {
gc.fillRect(pipex + model.getRectOffset(), pipey + (board.getGRIDWIDTH() - model.getRectOffset()), model.getPipeWidth(), (board.getGRIDWIDTH() - model.getRectOffset()));
gc.fillRect(pipex, pipey + model.getRectOffset(), (board.getGRIDWIDTH() - model.getRectOffset()), model.getPipeWidth());
}
if (below && right) {
gc.fillRect(pipex + model.getRectOffset(), pipey + (board.getGRIDWIDTH() - model.getRectOffset()), model.getPipeWidth(), (board.getGRIDWIDTH() - model.getRectOffset()));
gc.fillRect(pipex + model.getRectOffset(), pipey + model.getRectOffset(), (board.getGRIDWIDTH() - model.getRectOffset()), model.getPipeWidth());
}
break;}}}