Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import javax.swing.JFrame;

public class MyGame extends JFrame implements KeyListener {


    private static final int size = 100;
    private Font font = new Font("TimesRoman", Font.BOLD, 20);
    private HashMap cLu = new HashMap() {
        {

            put(0, new Color(0xcdc1b4));
            put(2, new Color(0xeee4da));
            put(4, new Color(0xede0c8));
            put(8, new Color(0xf2b179));
            put(16, new Color(0xf59563));
            put(32, new Color(0xf67c5f));
            put(64, new Color(0xf65e3b));
            put(128, new Color(0xedcf72));
            put(256, new Color(0xedcc61));
            put(512, new Color(0xedc850));
            put(1024, new Color(0xedc53f));
            put(2048, new Color(0xedc22e));

        }
    };

    private int[][] grill = new int[4][4];

    public void init() {
        appear(1);
        addKeyListener(this);
    }

    private void appear(int h) {
        ArrayList available = new ArrayList<>();
        for (int i = 0; i = 0; j--) {
                    if (grill[i][j] == 0 && grill[i][j + 1] == 0) {
                        continue;
                    }
                    if (grill[i][j] == grill[i][j + 1]) {
                        grill[i][j + 1] = grill[i][j] * 2;
                        grill[i][j] = 0;
                    } else if (grill[i][j + 1] == 0) {

                        grill[i][j + 1] = grill[i][j];
                        grill[i][j] = 0;
                        j = grill[0].length - 1;

                    }

                }

            }

        } else if (m == KeyEvent.VK_LEFT) {

            for (int j = 0; j = 0; i--) {
                    if (grill[i][j] == 0 && grill[i + 1][j] == 0) {
                        continue;
                    }
                    if (grill[i][j] == grill[i + 1][j]) {
                        grill[i + 1][j] = 2 * grill[i][j];
                        grill[i][j] = 0;
                    } else if (grill[i + 1][j] == 0) {
                        grill[i + 1][j] = grill[i][j];
                        grill[i][j] = 0;
                        i = grill.length - 1;
                    }
                }
            }
        }

    }

    @Override
    public void paint(Graphics g) {
        g.clearRect(0, 0, grill.length, grill[0].length);
        FontMetrics fm = g.getFontMetrics();
        g.setFont(font);
        for (int i = 0; i < grill.length; i++) {
            for (int j = 0; j < grill[0].length; j++) {

                g.setColor(cLu.get(grill[i][j]));
                            g.fillRoundRect(i * size,  j * size, 95,95, 40, 40);
                //g.setColor(cLu.get(0));
               // g.drawRect(i * size, j * size, size, size);

                g.setColor(new Color(0x776e65));
                String number = String.valueOf(grill[i][j]);
                // center the number on top of the square
                int x = (i * size) + (size / 2) - (fm.stringWidth(number) / 2);
                int y = (j * size) + (size / 2) + (fm.getAscent() / 2);
                g.drawString(number, x, y);

            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        int a = e.getKeyCode();
        if (a == KeyEvent.VK_UP || a == KeyEvent.VK_DOWN || a == KeyEvent.VK_RIGHT || a == KeyEvent.VK_LEFT) {
            move(a);
            appear(1);
            repaint();

        }
    }

    public static void main(String[] args)  {

        JFrame frame = new JFrame();
        MyGame m = new MyGame(); 
        frame.setVisible(true);
        frame.setTitle("2048");
        frame.setSize(410, 448);
        frame.setLocationRelativeTo(null);
        frame.add(m, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}


What I have tried:

i want to use the frame but it has Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container

pleas help me !
Thanks^-^
Posted
Updated 31-Mar-17 9:22am
v2
Comments
Patrice T 31-Mar-17 15:30pm    
Please show position of error in code.
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900