Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-30-2008, 12:13 AM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
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
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
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 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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-30-2008, 12:33 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-30-2008, 03:44 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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; } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-02-2008, 05:28 PM
Member
 
Join Date: Nov 2007
Posts: 35
java_fun2007 is on a distinguished road
cool Thank you very much!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 03:08 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org