Home>

When I call the drawBoard method of the Board class with the main method of the Main class,
I got the following error:

I tried to use the Graphics object with reference to an external site
Do not use new to create a new object like other objects,
public static void drawBoard(Graphics g) {
}
I don't understand the reason to write it in the place where the dummy argument is put like.
If i can do things around there, I would appreciate it if you explain in detail m(__)m

error contents
MacBook-Pro-2 othello %javac Board.java Main.java
Main.java:8: Error: The method drawBoard in class Board cannot be applied to the specified type.
        Board.drawBoard();
             ^
  Expected value: Graphics
  Found: no argument
  Reason: Actual argument list and dummy argument list have different length
1 error
Main.java
public class Main {
  public static void main(String args[]) {
    // initialize the array
    Board.initBoard();
    // Initialize Othello version
    Board.drawBoard();
  }
}
Board.java
import java.awt.Graphics;
import java.awt.Color;
public class Board {
    // Field (variable declared in class block) declaration
    public static final int blank = 0;
    public static final int width = 8;
    public static final int height = 8;
    public static final int gs = 1;
    public static int[][] board = new int[height][width];
    // Method to initialize the Othello board
    public static void initBoard() {
        for(int i = 0;i<height;i++) {
            for(int a = 0;i<width;i++) {
                board[i][a] = blank;
            }
        }
    }
    // Method to draw the Othello board
    public static void drawBoard(Graphics g) {
        // specify the color
        g.setColor(new Color(0,128,128));
        // fill the whole square
        g.fillRect(0, 0,width,height);
        // draw lines vertically and horizontally to draw squares
        g.setColor(Color.black);
        for(int i = 0;i<height;i++) {
            // draw a vertical line
            // g.drawLine(startX,startY,endX,endY);
            g.drawLine(gs*i,0,gs*i,height);
        }
        for(int i = 0;i<width;i++) {
            // draw a horizontal line
            // g.drawLine(startX,startY,endX,endY);
            g.drawLine(0,gs*i,width,gs*i);
        }
        // draw the outer frame (rectangle)
        g.drawRect(0,0,width,height);
    }
}
  • Answer # 1

    Represents a drawing area when called from mainGraphics gBecause I didn't put it in the argument.
    You need to create a drawing area by inheriting Applet or Frame.

    It's faster to pull it from other sample code and improve it.

  • Answer # 2

    Do you want to do this?

    import java.awt.Graphics;
    import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    class Main extends JFrame {
        public static final int blank = 0, black = 1, white = 2;
        public static final int width = 8, height = 8;
        public static final int gs = 80;
        public int[][] board = new int[height][width];
        public static void main(String[] args) {new Main();}
        Main() {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(650, 680);
            setTitle("Othello");
            setLocationRelativeTo(null);
            setResizable(false);
            add(new Board());// Add drawing area
            setVisible(true);
            initBoard();
        }
        void initBoard() {
            for (int i = 0;i<height;i++)
                for (int j = 0;j<width;j++)
                    board[i][j] = blank;
            board[3][4] = board[4][3] = black;
            board[3][3] = board[4][4] = white;
        }
        class Board extends JPanel {
            public void paintComponent(Graphics g) {
                g.setColor(new Color(0, 128, 128));// specify the color
                g.fillRect(0, 0, gs * width, gs * height);// fill the whole
                g.setColor(Color.black);
                for (int i = 0;i<height;i++)
                    g.drawLine(gs * i, 0, gs * i, gs * height);// draw a vertical line
                for (int i = 0;i<width;i++)
                    g.drawLine(0, gs * i, gs * width, gs * i);// draw a horizontal lineg.drawRect(0, 0, gs * width, gs * height);// draw the outer frame (rectangle)
                for (int i = 0;i<width;i++) {
                    for (int j = 0;j<height;j++) {
                        int b = board[i][j];
                        if (b != blank) {
                            g.setColor(b == black ?Color.black :Color.white);
                            g.fillOval(gs*i+4, gs*j+4, gs-8, gs-8);
                        }
                    }
                }
            }
        }
    }


    Addendum
    File can be split into Main.java and Board.java
    The class board has been made independent.
    I added a process when clicking the mouse button.

    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    class Main extends JFrame {
        public static void main(String[] args) {new Main();}
        Main() {
            setSize(650, 680);
            setTitle("Othello");
            setLocationRelativeTo(null);
            setResizable(false);
            setDefaultCloseOperation(EXIT_ON_CLOSE);// Close with [x] button
            add(new Board());// Add drawing area
            setVisible(true);
        }
    }
    class Board extends JPanel {
        static final int blank = 0, black = 1, white = 2;
        static final int width = 8, height = 8;static final int gs = 80;
        int[][] board = new int[height][width];
        int turn = black;
        Board() {
            for (int i = 0;i<height;i++)
                for (int j = 0;j<width;j++) board[i][j] = blank;
            board[3][3] = board[4][4] = white;
            board[3][4] = board[4][3] = black;
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    int x = e.getX()/gs, y = e.getY()/gs;
                    if (x<8&&y<8) {
                        board[x][y] = turn;
                        turn = black + white-turn;
                        repaint();
                    }
                }
            });
        }
        public void paintComponent(Graphics g) {
            g.setColor(new Color(0, 128, 128));// specify the color
            g.fillRect(0, 0, gs * width, gs * height);// fill the whole
            g.setColor(Color.black);
            for (int i = 0;i<height;i++)
                g.drawLine(gs * i, 0, gs * i, gs * height);// draw a vertical line
            for (int i = 0;i<width;i++)
                g.drawLine(0, gs * i, gs * width, gs * i);// draw a horizontal line
            g.drawRect(0, 0, gs * width, gs * height);// draw the outer frame (rectangle)
            for (int i = 0;i<height;i++) {
                for (int j = 0;j<width;j++) {
                    int piece = board[i][j];
                    if (piece != blank) {
                        g.setColor(piece == black ?Color.black :Color.white);
                        g.fillOval(gs * i + 4, gs * j + 4, gs-8, gs-8);
                    }
                }
            }
        }
    }