1 year ago

#380062

test-img

user16879601

Why isnt my Paint Drawing on my Canvas? How Would I Draw a Checkerboard Based Upon a 2d Array?

I am slightly new to Java and have messed around with Swing and AWT a few times, but I cannot currently get what I have to work. I have two classes,

Class GameController:

import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;

public class GameController {
    Button giveButton;
    JFrame frame;

    public static void main () {
        GameController game = new GameController();
    }
    
    public GameController () {
        frame = new JFrame("Tic Tac Toe!");
        frame.setLayout(new FlowLayout());
        frame.setBackground(Color.black);
        Panel buttons = new Panel();
        buttons.setLayout(new FlowLayout());
        giveButton = new Button ("GIVE UP");
        buttons.add(giveButton);
        frame.add(buttons);
        frame.pack();
        frame.setVisible(true);
    }
} 

and class Board:

import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;

public class Board extends Canvas {
    private int [][]playingGrid = new int[3][3];
    int boxheight = 30;
    int boxwidth = 30;
    
    @Override
    public void paint (Graphics g){
        g.fillRect(10,20,30,40);
    }
}

I realize that the paint method I have now will not traverse my array and draw a 3x3 checkerboard like I want to do, but why isn't the paint method drawing anything when I execute? When I do fix this, how would I use a 2d array to make a checkerboard?

java

arrays

swing

awt

0 Answers

Your Answer

Accepted video resources