0

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).

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    What is the question? – Reimeus Mar 04 '13 at 12:49
  • Sorry for being unclear, the question is, how can I create squares to occupy a space through calling the PaintSquare(). I have read a bunch of stuff online but nothing I have been able to make work. – Chris Brown Mar 04 '13 at 17:06

2 Answers2

1

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.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

Follow Reimeus' advice, and in addition, you need to create a GUI model.

Define a Rectangle array the same size as the game board in a game model class.

Rectangle[][] board;

That way, you can test for an overlapping snake in your model class, rather than worry about what you've already painted.

Your paintComponent method becomes pretty simple.

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    model.draw(g);
}

See this answer for a fuller explanation.

Community
  • 1
  • 1
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Would an int 2 dimensional array be effective? Eventually there will be multiple users then a switch to find if free, a users snake is on there or another object? – Chris Brown Mar 05 '13 at 15:55
  • An int array wouldn't be sufficient. You model has to contain enough information to draw itself, which is why I used the Rectangle class. You can define your own class and make it into a two dimensional array. Your class would include the defining rectangle, and whatever other information you want to store in your model. – Gilbert Le Blanc Mar 05 '13 at 20:06
  • What if I used For int[30][30] switch (int[x][y]) case 1: paintSquare(g, x*20,y*20, Color.RED); paintSquare (Graphics g,int x,int y, Color c) g.setColor(c); g.fillRect(x, y, 20, 20); – Chris Brown Mar 06 '13 at 15:36
  • @Chris Brown: You don't seem to understand the difference between a game model and a game GUI yet. – Gilbert Le Blanc Mar 07 '13 at 13:41