Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By hardwired
  • 1 Post By sunde887

Thread: how can I draw a board in 2d array?

  1. #1
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default how can I draw a board in 2d array?

    hi
    i'm trying to create a game called "Gomuku", but I don't know how to start this is my first game ever.. I need to create 10x10 board, JPanel, JButton to start the game "New Game" and a class called Square.java that holds int x,y,accupied;

    this is Square.java class
    Java Code:
    public class Square {
        private final int row ;
        private final int col ;
        private int accupied=0;
    
    
        public Square(int r, int c) {
          row = r;
          col = c;
            
        }
    
       public void setAccupied(int ac) {
    		accupied = ac;
       }
       
    public int getAccupied()
      {
      	return accupied;
      }
      
            }//end square class
    Java Code:
    class Board extends JPanel
    {
    	private Square sq[][] ;
    	 
    	public void paintComponent(Graphics g)
    	{  sq[10][10] = new Square(10,10);
    		 for (int row = 0;  row < 10;  row++) {
                 for (int column = 0;  column < 10;  column++) {
                    Square[row][column] = 0;
                 }
              }
    g.drawRect();
    	}
    }//end class board
    I want to draw the squares now with this method g.drawRect(); but it takes four parameters :confused: do you know where to get examples like that maybe I will get benefit of them?
    I'm just beginning to learn how to create games like these using Square.java class and a JPanel.

    Thanks

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    you would need to use a thread if you wanted it to update constantly. I would recommend using images and just using a Gridlayout. Then just updating the images everytime someone makes a move. your way works, but it over complicates the problem. Also if you us buttons or images you can just add a mouse listener to it.
    My IP address is 127.0.0.1

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.geom.Line2D;
    import javax.swing.*;
    
    public class MakingAGrid extends JPanel {
        SquareRx[][] squares;
        final int PAD = 20;
    
        public MakingAGrid() {
            int ROWS = 10;
            int COLS = 10;
            squares = new SquareRx[ROWS][COLS];
            for(int i = 0; i < ROWS; i++) {
                for(int j = 0; j < COLS; j++) {
                    squares[i][j] = new SquareRx(i, j);
                }
            }
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            double xInc = (double)(w - 2*PAD)/squares[0].length;
            double yInc = (double)(h - 2*PAD)/squares.length;
            // Draw vertical grid lines.
            g2.setPaint(Color.blue);
            for(int i = 0; i <= squares[0].length; i++) {
                double x = PAD + i*xInc;
                g2.draw(new Line2D.Double(x, PAD, x, h-PAD));
            }
            // Draw horizontal grid lines.
            for(int i = 0; i <= squares.length; i++) {
                double y = PAD + i*yInc;
                g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
            }
        }
    
        public static void main(String[] args) {
            MakingAGrid test = new MakingAGrid();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class SquareRx {
        private final int row;
        private final int col;
        private boolean occupied = false;
    
        public SquareRx(int r, int c) {
            row = r;
            col = c;
        }
    
        public void setOccupied(boolean occupied) {
            this.occupied = occupied;
        }
    
        public boolean isOccupied() {
            return occupied;
        }
    }

  4. #4
    java_fun2007 is offline Member
    Join Date
    Nov 2007
    Posts
    35
    Rep Power
    0

    Default

    cool :) Thank you very much!

  5. #5
    Thebigchalupa is offline Member
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default

    How would you fill in those squares with the code posted above?

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class SquareGrid extends JPanel {
        SquareRx[][] squares;
        final int PAD = 20;
        final int ROWS = 10;
        final int COLS = 10;
    
        public SquareGrid() {
            addMouseListener(ml);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            if(squares == null) {
                initSquares();
            }
            // Draw squares.
            g2.setPaint(Color.blue);
            for(int i = 0; i < ROWS; i++) {
                for(int j = 0; j < COLS; j++) {
                    squares[i][j].draw(g2);
                }
            }
        }
    
        private void initSquares() {
            squares = new SquareRx[ROWS][COLS];
            int w = getWidth();
            int h = getHeight();
            double xInc = (double)(w - 2*PAD)/COLS;
            double yInc = (double)(h - 2*PAD)/ROWS;
            for(int i = 0; i < ROWS; i++) {
                double y = PAD + i*yInc;
                for(int j = 0; j < COLS; j++) {
                    double x = PAD + j*xInc;
                    Rectangle2D.Double r =
                        new Rectangle2D.Double(x, y, xInc, yInc);
                    squares[i][j] = new SquareRx(i, j, r);
                }
            }
        }
    
        private MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                Point p = e.getPoint();
                if(!isInGrid(p)) return;
                double xInc = (double)(getWidth() - 2*PAD)/COLS;
                double yInc = (double)(getHeight() - 2*PAD)/ROWS;
                int row = (int)((p.y-PAD)/yInc);
                int col = (int)((p.x-PAD)/xInc);
                System.out.println(squares[row][col]);
                boolean isSelected = squares[row][col].isSelected();
                squares[row][col].setSelected(!isSelected);
                repaint();
            }
        };
    
        private boolean isInGrid(Point p) {
            Rectangle r = getBounds();
            r.grow(-PAD, -PAD);
            return r.contains(p);
        }
    
        public static void main(String[] args) {
            SquareGrid test = new SquareGrid();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
            test.addComponentListener(test.cl);
        }
    
        private ComponentListener cl = new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                squares = null;
                repaint();
            }
        };
    }
    
    class SquareRx {
        private final int row;
        private final int col;
        Rectangle2D.Double rect;
        Color color = new Color(140,200,160);
        Color bgColor = Color.orange;
        Color selColor = Color.red;
        private boolean selected = false;
    
        public SquareRx(int r, int c, Rectangle2D.Double rect) {
            row = r;
            col = c;
            this.rect = rect;
        }
    
        public void draw(Graphics2D g2) {
            g2.setPaint(selected ? selColor : bgColor);
            g2.fill(rect);
            g2.setPaint(color);
            g2.draw(rect);
        }
    
        public void setSelected(boolean selected) {
            this.selected = selected;
        }
    
        public boolean isSelected() { return selected; }
    
        public String toString() {
            return "SQUARE[row:" + row + ", col:" + col +
                        ", selected:" + selected + "]";
        }
    }
    henna likes this.

  7. #7
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    i tested the code. it's really cool. What if when it a box is clicked, an image should how should i do that? I was also doing a game but aan image should appear when a box is clicked... thank you...

  8. #8
    ranss41 is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: how can I draw a board in 2d array?

    That's cool... but i'm going to do Battlefield game. i need only draw mapping part... so how to put images in each selected cells?? i mean Obstacle images and target image? please help me...

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: how can I draw a board in 2d array?

    It's been well over a year since the last post in this thread. If you have a question, please open a new thread. Closing this.
    DarrylBurke likes this.

Similar Threads

  1. Draw an arrow
    By Albert in forum SWT / JFace
    Replies: 3
    Last Post: 02-01-2008, 08:11 AM
  2. help me draw... please...
    By kureikougaiji in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 12:22 PM
  3. Draw on JPanel, Help
    By carl in forum Java 2D
    Replies: 1
    Last Post: 07-31-2007, 06:56 AM
  4. how to draw in Java
    By Heather in forum AWT / Swing
    Replies: 2
    Last Post: 07-12-2007, 11:01 AM
  5. How to draw a thick line
    By johnt in forum Java 2D
    Replies: 1
    Last Post: 05-31-2007, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •