Search code examples
javaswingjpanelpaint

creating squares on event


I am trying to create a game with the same system as snake. I have created a window with a JPanel on it, given it a background and drawn lines to show the user the squares.

Board is 600x600 (601x601 for all to be visible). Squares are 20x20.

Now I am trying to add a way to put coloured squares onto the board and also detect if a coloured square is there already ideally.

public class CreateWindow extends JFrame {

    JPanel GameArea;
    static JLayeredPane Java_Window;
    Image Background;

    public void CreateWindow() {
        Dimension Panel_Size = new Dimension(800, 800);
        this.setSize(800,800);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible( true );
        this.setTitle("LineRage");
        getContentPane().setBackground(Color.white); 

        Java_Window = new JLayeredPane();
        this.add(Java_Window);
        Java_Window.setPreferredSize(Panel_Size);

        GameArea = new JPanel()
        {
            @Override
            public void paint(Graphics g) {
                g.setColor(Color.BLACK);
                g.fillRect(0,0,601,601);

                g.setColor(Color.GRAY);
                // Cut map into sections
                int x;
                //draw vertical lines
                for(x = 0; x < 31; x++) {
                    g.drawLine(x*20,0,x*20,600);
                }    
                //draw horizontal lines
                for(x = 0; x < 31; x++) {
                    g.drawLine(0,x*20,600,x*20);
                }     
            }

            public void PaintSquare (int x,int y) {
                //Check if square painted

                //Paint square
                Rectangle rect = new Rectangle(x, y, 20, 20);
                GameArea.add(rect);
            }
        };
        Java_Window.add(GameArea, JLayeredPane.DEFAULT_LAYER);
        GameArea.setBounds(20, 20, 601, 601);
        GameArea.setVisible(true);
    }
}

So Java_Window (800x800) has a white background, Game_Area (601x601) has black background with 32 lines up along and across it to divide it into squares.

public void PaintSquare (int x, int y) {
    //Check if square painted

    //Paint square
    Rectangle square = new Rectangle(x, y, 20, 20);
    GameArea.add(square);
}

PaintSquare will be called from another object (the main game) and check the background of the square, if it is free it will paint a square on it (20x20).


Solution

  • Your exact question is unclear but here are some pointers:

    • Use paintComponent rather than paint when doing custom painting in Swing. Don't forget to call super.paintComponent(g).
    • java.awt.Rectangle is not derived from JComponent (or Component) so can't be added to a container.
    • One approach to take would be to use fillRect and "paint" the squares:

    Also, in Java, methods start with a lowercase letter. Adding this and the previous point together, you could do:

    public void paintSquare(Graphics g, int x, int y) {
       g.fillRect(x, y, 20, 20);
    }
    

    Here, the paintSquare method would be called from paintComponent.