Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
Like Tree3Likes

Thread: 2048

  1. #21
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  2. #22
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Red face Re: 2048

    Quote Originally Posted by JosAH View Post
    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();
            mf.addFrame();
            
        }
    }
    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);
        }
        public void addFrame(){
            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);
                    add(BC[i][j]);
                }
            
        }
    }
    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. #23
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: 2048

    Use an array of Color objects?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #24
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Default Re: 2048

    Quote Originally Posted by JosAH View Post
    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. #25
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: 2048

    Quote Originally Posted by Flopet17 View Post
    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

    ps. b.t.w. I'm moving this thread because there's nothing advanced about it.
    Flopet17 likes this.
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 2 FirstFirst 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
  •