Question:
I need help with drawing circles and the graph for a gui for connect 4?
darren
2008-04-27 16:03:23 UTC
I need your help....

I am working on a connect four game, and I am not sure how to code the 7 rows and six columns for the game...also I need help in drawing the circles on the grid.....

Thank you so much

Disclaimer......if you will not help then just move on...Apparently if I knew how to do it, I wouldn't ask.....I notice that some people get smart when someone asks a question.....I am quite sure that there is something that you may need help with also.....

Thanks
Three answers:
Donnie Murdo
2008-04-27 16:36:25 UTC
I made a Java application for my Honours year project that used a lot of Java2D.



I used a BufferedImage to give me a canvas upon which I drew a grid. In the underlying class which used the BufferedImage I used formulae and fixed width/height constants to calucalte the x,y positions of a particular cell.



For example;



Say you had a 7x6 grid of squares, each with sides of 10 pixels (Just think squares but we'll draw circles inside them), then I would calculate the x/y positions as follows;



private static final int CELL_DIMENSION = 10;



// Get the top left position of the cell

// from a given (column, row) position,

// where 0 is 1st row/column

private int[] getCellPosition( int column, int row ) {



int xPosition = column * CELL_DIMENSION;

int yPosition = row * CELL_DIMENSION;



return new int[] { xPosition, yPosition};

}



So when the user drops the counter thing into a column, you store which rows in the column are already taken, then from that figure out which cell is to be filled in.



Call the above method using the caluclated column and row positions and it will return the pixel positions to draw from on the BufferedImage. Then just get the graphics context of the BufferedImage - myBufferedImage.getGraphics(); - and fill an oval at the returned (x,y) positions and using the fixed dimension constant.



Bare in mind the above example means all the circles will be touching so adjust the forumale in the method to suit your needs. And also store in a data structure that the current cell is now occupied.



Then just output the BufferedImage graphics context to your JPanel in the GUI



Hope this helps



=)
?
2016-11-09 05:32:17 UTC
i think of there's a in-built function to allure to a line. In swing or awt or something? like, drawLine(x1,y1,x2,y2)? wherein case you should basically calculate the values, then use the draw line?
Blackcompe
2008-04-27 17:04:56 UTC
drawString() and drawEllipse() for Java


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...