@Override public void actionPerformed(ActionEvent a) { JTextField mapWidthInput = new JTextField("11"), mapHeightInput = new JTextField("11"); JPanel optionPanel = new JPanel(new GridLayout(0, 1)); width = 0; height = 0; String Width = "", Height = ""; optionPanel.add(new JLabel("Map Width: ")); optionPanel.add(mapWidthInput); optionPanel.add(new JLabel("Map Height: ")); optionPanel.add(mapHeightInput); int result = JOptionPane.showConfirmDialog( Main.parentFrame, optionPanel, "Map Options", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (result == JOptionPane.OK_OPTION) { Width = mapWidthInput.getText(); Height = mapHeightInput.getText(); try { width = Integer.parseInt(Width); height = Integer.parseInt(Height); } catch (NumberFormatException err) { JOptionPane.showMessageDialog(null, "Is it that hard to just use numbers? \n" + err); return; } } if (width >= 900 && width <= 10 || height >= 600 || height <= 10 || width % 2 == 0 || height % 2 == 0) { JOptionPane.showMessageDialog( null, "Invalid Input(s). Width must be 11-899 and height must be 11-599. Only odd numbers."); return; } startTime = System.currentTimeMillis(); map = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); int GX = 0, GY = 0, maxGX = 0, maxGY = 0; cells = new cell[width][height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (x % 2 == 0 || y % 2 == 0) map.setRGB(x, y, Color.black.getRGB()); else map.setRGB(x, y, Color.white.getRGB()); if (y % 2 == 1) if (x % 2 == 1) { cells[GX][GY] = new cell(x, y, GX, GY); // JOptionPane.showMessageDialog(Main.parentFrame, // "GX: " + GX + " GY: " + GY); GY++; } if (GX > maxGX) maxGX = GX; if (GY > maxGY) maxGY = GY; } if (x % 2 == 1) { GX++; GY = 0; } } image = (Image) map; scale = Math.min(600 / height, 900 / width); Main.Canvas.setBorder(BorderFactory.createLineBorder(Color.black)); Main.map = map; image = map.getScaledInstance(width * scale, height * scale, Image.SCALE_DEFAULT); Main.Canvas.setIcon(new ImageIcon(image)); Random r = new Random(System.currentTimeMillis()); int startX = r.nextInt(maxGX + 1), startY = r.nextInt(maxGY + 1); startCell = currentCell = cells[startX][startY]; Generate inst = new Generate(); inst.execute(); Main.map = null; Main.checkMapBuffered(); }
@Override protected Void doInBackground() throws Exception { do { int direction = getNextCell(currentCell, cells); if (direction >= 0) { if (direction == 0) { for (int i = 1; i < 3; i++) map.setRGB(currentCell.x - i, currentCell.y, Color.white.getRGB()); currentCell = cells[currentCell.gridX - 1][currentCell.gridY]; } else if (direction == 1) { for (int i = 1; i < 3; i++) map.setRGB(currentCell.x, currentCell.y - i, Color.white.getRGB()); currentCell = cells[currentCell.gridX][currentCell.gridY - 1]; } else if (direction == 2) { for (int i = 1; i < 3; i++) map.setRGB(currentCell.x + i, currentCell.y, Color.white.getRGB()); currentCell = cells[currentCell.gridX + 1][currentCell.gridY]; } else if (direction == 3) { for (int i = 1; i < 3; i++) map.setRGB(currentCell.x, currentCell.y + i, Color.white.getRGB()); currentCell = cells[currentCell.gridX][currentCell.gridY + 1]; } } else { // JOptionPane.showMessageDialog(Main.parentFrame, // "current X: " + currentCell.x + " current y: " + // currentCell.y); if (map.getRGB(currentCell.x - 1, currentCell.y) == Color.white.getRGB() && !cells[currentCell.gridX - 1][currentCell.gridY].backtracked) { currentCell.backtracked = true; currentCell = cells[currentCell.gridX - 1][currentCell.gridY]; } else if (map.getRGB(currentCell.x, currentCell.y - 1) == Color.white.getRGB() && !cells[currentCell.gridX][currentCell.gridY - 1].backtracked) { currentCell.backtracked = true; currentCell = cells[currentCell.gridX][currentCell.gridY - 1]; } else if (map.getRGB(currentCell.x + 1, currentCell.y) == Color.white.getRGB() && !cells[currentCell.gridX + 1][currentCell.gridY].backtracked) { currentCell.backtracked = true; currentCell = cells[currentCell.gridX + 1][currentCell.gridY]; } else if (map.getRGB(currentCell.x, currentCell.y + 1) == Color.white.getRGB() && !cells[currentCell.gridX][currentCell.gridY + 1].backtracked) { currentCell.backtracked = true; currentCell = cells[currentCell.gridX][currentCell.gridY + 1]; } } if (Main.trail.isSelected()) { image = map.getScaledInstance(width * scale, height * scale, Image.SCALE_DEFAULT); Main.Canvas.setIcon(new ImageIcon(image)); } // JOptionPane.showMessageDialog(Main.parentFrame, "X: " + // currentCell.x + " Y: " + currentCell.y); } while (!(currentCell.x == startCell.x && currentCell.y == startCell.y)); map.setRGB(1, 1, Color.red.getRGB()); map.setRGB(width - 2, height - 2, Color.blue.getRGB()); Main.map = map; image = map.getScaledInstance(width * scale, height * scale, Image.SCALE_DEFAULT); Main.Canvas.setIcon(new ImageIcon(image)); Main.checkMapBuffered(); endTime = System.currentTimeMillis(); JOptionPane.showMessageDialog( Main.parentFrame, "Total elapsed time: " + (endTime - startTime) + " ms"); return null; }