1. ## Re: 2048

A silly little question from me: what are the rules of this '2048' game? (that link wasn't of any help to me ...)

kind regards,

Jos

2. Member
Join Date
Mar 2014
Posts
60
Rep Power
0

## Re: 2048

Originally Posted by JosAH
A silly little question from me: what are the rules of this '2048' game? (that link wasn't of any help to me ...)

kind regards,

Jos
You have to add up the numbers in order to get the 2048 piece... If you combine 2+2=4 .. 4+4=8... and so on. It's a very cool game, not that easy.

I have another question.
I have managed to draw the rectangles... But now I want to edit the color of them in certain condition. For example if i == 2 the rectangle to be filled in with BLUE color, if i==3 RED... and so on...

This is my code so far:
Java Code:
```package game2048;
public class Game2048 {
public static void main(String[]args){
MainFrame mf = new MainFrame();

}
}```
Java Code:
```package game2048;

import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.OverlayLayout;

public class MainFrame extends JFrame{
public MainFrame(){
setVisible(true);
}
setTitle("2048");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(427,504);
Container c = getContentPane();

c.setLayout(new OverlayLayout(c));

GameBox BC[][] = new GameBox[4][4];

for(int i=0;i<4;i++)
for(int j=0;j<4;j++){
if(i==0)
{
BC[i][j] = new GameBox(Color.GREEN);
}
BC[i][j] = new GameBox(i*100+i*5+5,j*100+j*5+50,100,100);
}

}
}```
Java Code:
```package game2048;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;

public class GameBox extends JComponent{
private int posX=0;
private int posY=0;
private int posW =100;
private int posH =100;
private Color col = Color.RED;
public GameBox(int x, int y, int w, int h){
this.posX=x;
this.posY=y;
this.posW=w;
this.posH=h;
}
public GameBox(Color col) {
this.col = col;
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(col);
g.fillRect(posX, posY, posW, posH);
g.setColor(Color.black);
g.drawRect(posX, posY, posW, posH);
}

}```
Thank you!

3. ## Re: 2048

Use an array of Color objects?

kind regards,

Jos

4. Member
Join Date
Mar 2014
Posts
60
Rep Power
0

## Re: 2048

Originally Posted by JosAH
Use an array of Color objects?

kind regards,

Jos
But what if I want later in the program, in another class to change the color into something else?

5. ## Re: 2048

Originally Posted by Flopet17
But what if I want later in the program, in another class to change the color into something else?
Then change the Colors in that array or use an entirely diffferent array of Colors. The solutions are numerous.

kind regards,

Jos

Page 2 of 2 First 12

#### Posting Permissions

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