Making a maze. Movement is carried out on the frame, depending on the value in the array map , where 1 - you can move, 2 - wall. Well, as a result, it turns out that I move one pixel at a time and for a large maze I have to create a large array. How to increase the size of the maze without increasing the array?

 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class RealMaze implements ActionListener { int x = 1; int y = 1; JButton button; MyDrawPanel myDrawPanel; JFrame frame; public static void main(String[] args) { RealMaze realMaze = new RealMaze(); realMaze.go(); } public void go() { frame = new JFrame(); myDrawPanel = new MyDrawPanel(); button = new JButton("Test"); frame.setSize(100, 100); frame.setVisible(true); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(BorderLayout.CENTER, myDrawPanel); frame.getContentPane().add(BorderLayout.SOUTH, button); button.addActionListener(this); } //Движение по фрейму public void actionPerformed(ActionEvent event) { if (map[x][y] == 1) { x++; System.out.println(x); System.out.println(map[x][y]); } frame.repaint(); } class MyDrawPanel extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.gray); g.fillRect(x, y, 10, 10); } } //Карта, 2 - стена. int[][] map = new int[][]{ {1,1,1,1,1,1,1,2,1,1}, {1,1,1,1,1,1,1,2,1,1}, {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, {2,2,1,1,1,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1} }; } 

    2 answers 2

    You need to change the step size when outputting. That is, define a certain modifier by which you will multiply the coordinate when outputting to the frame.

    For example:

     class MyDrawPanel extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.gray); // Получу размер стороны квадрата. int sideLength = getSideLength(); // нарисую квадрат масштабируя координаты. g.fillRect(x*sideLength, y*sideLength, sideLength, sideLength); } private int getSideLength() { // минимальная сторона компоненты. int minComponentSide = (this.getHight() > this.getWidth()) ? this.getWidth() : this.getHight(); // максимальная сторона карты. int maxMapSide = (map.length > map[0].length) ? map.length : map[0].length; // длина стороны, чтобы карта уместилась и комнаты были квадратные. return minComponentSide / maxMapSide; } } 

      I can offer an alternative - use the picture!

       BufferedImage image = ImageIO.read(this.getClass.getResources("image.png"); int clr= image.getRGB(x,y); 

      You make a black and white maze with a picture. You load here, and going through the coordinates, you check that the figure is not equal to 0 (black).