• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with making a 4x4 grid. code and details inside.  RSS feed

 
kenny gill
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to make a 4x4 grid of boxes with each having a different color.

i have some code written for the grid but am still pretty clueless on how to continue it and also how to import the colors.

I told my professor(which is an online course) that I was never shown an example of a grid but his reply was very short.

I'm working with Applets. just fyi

here is the box code



and here is some of the grid code:



Also, here are the instructions if it helps clarify anything:

"In your Java1_Homework working set, create a project named java1_BoxProject. We will use this project for a number of lessons, and this gives us a chance to get it started.

In it, define a new class named Box.java.

Box.java should have these instance variables (all of type int): upperLeftX, upperLeftY, height, width, and one more instance variable of type Color named boxColor. Make them all private and provide Getters and Setters for each instance variable.

Provide the body of the following Constructor that sets the values of the instance variables:

public Box(int upperX, int upperY, int h, int w, Color myColor)

Create a method called display(Graphics g) that uses setColor(boxColor); and fillRect(upperLeftX,upplerLeftY,width, height); to fill our box.

Also in the java1_BoxProject, Make a Java applet named Grid.java. This applet should display a 4 x 4 grid of boxes, each with a different color (there are 13 standard colors you can look up in the Color Class in the API, you can use a few twice to get 16 boxes).

You must create 16 instances of the Box class and display them. Do not reuse just one box object."


thanks
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
draw your 4 x 4 grid on paper, write inside each box one of the 13 colors (+ 3 duplicates).
with the first box (top left), write at each corner (all 4 corners) their respective x,y.
each one of those x,y's represents the upperLeftX and upperLeftY of 4 boxes.
repeat until 4 x 4 grid complete.

you can create 16 boxes individually from the above info, or, if you can find a
relationship between all the x,y's, you can create all 16 in a loop.
 
kenny gill
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

but I've never made a grid and don't know how to continue it.

sorry but my lesson has not gone over grids and i haven't been shown or taught arrays yet.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> but I've never made a grid

ever played tic-tac-toe?

that's a 3 x 3 grid, so just add an extra set of boxes to the right, another set underneath,
and there's your 4 x 4 grid
 
kenny gill
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I mean coding a grid. Could you give me an example so I can work upon it and understand?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Could you give me an example so I can work upon it and understand?

you need to work it out - start by doing a 1 x 1 grid (just one box)
have a go, and if you get stuck, post the code you've tried
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!