试图画板

时间:2017-05-07 00:16:12

标签: java paintcomponent

我有一个带有if语句的paintComponent。在每种情况下,应在面板上显示正方形。什么都没有出现。

 @Override
public void paintComponent(Graphics g) {

    //for loop to draw current board
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j <= columns; j++) {

            if (board[i][j] == '.') {
                g.setColor(Color.black);
                g.drawRect((xSize / 5) * i, (ySize / 6) * j, 50, 50);
            }

            if (board[i][j] == '#') {
                g.setColor(Color.magenta);
                g.drawRect((xSize / 5) * i, (ySize / 6) * j, 50, 50);
            }

            if (board[i][j] == '%') {
                g.setColor(Color.orange);
                g.drawRect((xSize / 5) * i, (ySize / 6) * j, 50, 50);
            }

            if (board[i][j] == '@') {
                g.setColor(Color.pink);
                g.drawRect((xSize / 5) * i, (ySize / 6) * j, 50, 50);
            }

        }
    }

}//end of paint component

1 个答案:

答案 0 :(得分:2)

你有两个问题,

  1. 您不应使用幻数,使用您可用的变量。

    g.fillRect(xSize * i, ySize * j, xSize, ySize);
    
  2. 不要过度循环列。您应该始终使用<,因为数组从零开始编制索引。

    for (int j = 0; j < columns; j++) {
    
  3. enter image description here

    工作示例

    import java.awt.*;
    import javax.swing.*;
    
    public class DrawPanel extends JPanel {
        private static final long serialVersionUID = 568117316148341762L;
    
        private int columns;
        private int rows;
        private int xSize;
        private int ySize;
        private char[][] board;
    
        public DrawPanel(int columns, int rows, int xSize, int ySize) {
            super();
    
            this.columns = columns;
            this.rows = rows;
            this.xSize = xSize;
            this.ySize = ySize;
            this.board = new char[rows][columns];
    
            this.setPreferredSize(new Dimension(columns * xSize, rows * ySize));
    
            setRandomTiles();
        }
    
        private void setRandomTiles() {
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    board[i][j] = randTile();
                }
            }
        }
    
        private char randTile() {
            switch ((int) (Math.random() * 4)) {
                case 0: return '.';
                case 1: return '#';
                case 2: return '%';
                case 3: return '@';
                default: return ' ';
            }
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            // for loop to draw current board
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    switch (board[i][j]) {
                        case '.':
                            g.setColor(Color.BLACK);
                            break;
                        case '#':
                            g.setColor(Color.MAGENTA);
                            break;
                        case '%':
                            g.setColor(Color.ORANGE);
                            break;
                        case '@':
                            g.setColor(Color.PINK);
                            break;
                    }
    
                    g.fillRect(xSize * i, ySize * j, xSize, ySize);
                }
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame f = new JFrame("Draw Panel");
                    DrawPanel p = new DrawPanel(30, 30, 10, 10);
    
                    f.setContentPane(p);
                    f.pack();
                    f.setLocationRelativeTo(null);
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.setVisible(true);
                }
            });
        }
    }
    

    您可以使用BufferedReader从文件加载电路板。

    import java.awt.*;
    import java.io.BufferedReader;
    import java.io.*;
    import javax.swing.*;
    
    public class DrawPanel extends JPanel {
        private static final long serialVersionUID = 568117316148341762L;
    
        private int cols;
        private int rows;
        private int xSize;
        private int ySize;
        private char[][] board;
    
        public DrawPanel(int rows, int cols, int xSize, int ySize) {
            super();
    
            this.rows = rows;
            this.cols = cols;
            this.xSize = xSize;
            this.ySize = ySize;
            this.board = new char[rows][cols];
    
            init();
            initRandomTiles();
        }
    
        public DrawPanel(String filename) {
            super();
    
            try {
                loadBoard(filename);
                init();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        protected void init() {
            this.setPreferredSize(new Dimension(cols * xSize, rows * ySize));
        }
    
        private void loadBoard(String filename) throws IOException {
            InputStream is = this.getClass().getClassLoader().getResourceAsStream(filename);
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    
            String[] metadata = br.readLine().split("\\s+");
    
            this.rows = Integer.parseInt(metadata[0], 10);
            this.cols = Integer.parseInt(metadata[1], 10);
            this.xSize = Integer.parseInt(metadata[2], 10);
            this.ySize = Integer.parseInt(metadata[3], 10);
            this.board = new char[rows][cols];
    
            int row = 0;
            String line = null;
            while ((line = br.readLine()) != null) {
                board[row++] = line.trim().toCharArray();
            }
        }
    
        private void initRandomTiles() {
            for (int row = 0; row < rows; row++) {
                for (int col = 0; col < cols; col++) {
                    board[row][col] = randTile();
                }
            }
        }
    
        private char randTile() {
            switch ((int) (Math.random() * 4)) {
                case 0: return '.';
                case 1: return '#';
                case 2: return '%';
                case 3: return '@';
                default: return ' ';
            }
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            for (int row = 0; row < rows; row++) {
                for (int col = 0; col < cols; col++) {
                    switch (board[row][col]) {
                        case '.':
                            g.setColor(Color.BLACK);
                            break;
                        case '#':
                            g.setColor(Color.MAGENTA);
                            break;
                        case '%':
                            g.setColor(Color.ORANGE);
                            break;
                        case '@':
                            g.setColor(Color.PINK);
                            break;
                    }
    
                    g.fillRect(xSize * col, ySize * row, xSize, ySize);
                }
            }
    
            g.dispose();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame f = new JFrame("Draw Panel");
                    DrawPanel p = new DrawPanel("resources/board.txt");
    
                    f.setContentPane(p);
                    f.pack();
                    f.setLocationRelativeTo(null);
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.setVisible(true);
                }
            });
        }
    }
    

    board.txt

    13 19 10 10
    ...................
    .%#######%##@####%.
    .#...............#.
    .@...............#.
    .#...............#.
    .#...............#.
    .%...............%.
    .#...............#.
    .#...............#.
    .#...............@.
    .#...............#.
    .%####@##%#######%.
    ...................