1
\$\begingroup\$

Is there a way to simplify my code underneath? Basically I'm drawing a series of rectangles against each other on a mouse dragged event. The negative numbers are read from a 2D array in an other class (Board). I'm using positive numbers for dots (you need to connect dots with beams/blocks) also in an other class. That's why I'm using the negative numbers. Every case in the switch statement does the same action except for the color selection: setFill(Color.) The cases range from -1 tot -9. I'm only posting 2 cases to reduce the code on this page. You can clearly see the repetition of code. I'm new to coding since this year.

       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;}}}
\$\endgroup\$
4
  • \$\begingroup\$ you can put the code that different between cases in the case statement (in this case the set color part). And put the rest of the code after the switch. Don't forget to filter the value first between -1 and -9 before the switch, otherwise that part of the code would be executed for any values. I feel like the whole code is a bit clunky, but I can't give you more advice how to make your code better, because I don't know the scope of that code. \$\endgroup\$ – Moch Yusup May 1 at 23:17
  • \$\begingroup\$ How should I do the filtering? I indeed tried before to put the repetition of code outside of the switch and that part is indeed executed for all the values. I'm making a game like flow free. This part of the code is where you drag the mouse and draw blocks to create a pipe. \$\endgroup\$ – Finsho May 2 at 13:21
  • \$\begingroup\$ You can do it like this: int val = board.getBoard()[j][i]; if(val < -9 || val > -1) continue; Color color; switch(val) {case -1: color = Color.RED; break;/* ... -9 */} gc.setFill(color); // rest of the code \$\endgroup\$ – Moch Yusup May 3 at 5:39
  • \$\begingroup\$ Thank you very much, that did the job! I forgot about the "continue", that way my code was still being processed for the whole board. \$\endgroup\$ – Finsho May 3 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.