Page 1 of 2 12 LastLast
Results 1 to 20 of 25
Like Tree3Likes

Thread: 2048

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

    Unhappy 2048

    Hello guys,
    I want to implement the 2048 game in Java.
    I made the game logics, but now I need to implement the interface.
    I made 2 classes GameBox and ContainerBox that extends JComponent.
    I painted in GameBox a rectagle and now I want to initialize a matrics of GameBoxes in ContainerBox so I used:
    GameBox GB[][] = new GameBox[4][4];
    I am thinking about drawing the Boxes in something like this:
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)

    but I don't know how to draw those rectangles in the for...

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: 2048

    how to draw those rectangles
    If you are using the Graphics class, it has methods for drawing things like rectangles.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: 2048

    I know...
    This is my code:

    Java Code:
    import java.awt.Graphics;
    import javax.swing.JComponent;
    
    public class GameBox extends JComponent{
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawRect(0,0, 100, 100);
    
        }
        
    }
    Java Code:
    import java.awt.Graphics;
    import javax.swing.JComponent;
    
    public class BoxContainer extends JComponent{
        public void paintComponent(Graphics g) {
            
            GameBox GB[][] = new GameBox[4][4];
            for(int i=0;i<4;i++)
                for(int j=0;j<4;j++){
                    GB[i][j].paintComponent(g);
                    g.drawRect(i*100, j*100, 100, 100);
                    this.add(GB[i][j]);
                }
        }
        
    }
    Java Code:
    public class MainFrame extends JFrame{
        public MainFrame(){
            setVisible(true);
        }
        public void addFrame(){
            setTitle("2048");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(404,436);
            setVisible(true);
            
            BoxContainer BC = new BoxContainer();
            Container pane = getContentPane();
            pane.add(BC);
            
        }
    }
    Java Code:
    public class Game2048 {
        public static void main(String[]args){
            MainFrame mf = new MainFrame();
            mf.addFrame();
        }
    }
    How can I select in the BoxContainer class where to draw the rectangle that is in the GameBox class?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: 2048

    where to draw the rectangle
    Where are the rectangles to be drawn? Can the GameBox class hold the location to draw its rectangle?
    I don't like the code calling the paintComponent() method directlly. That method should only be called by the JVM when the component needs to be redrawn. Call the repaint() method to tell the JVM that the component needs to redraw itself.

    See the tutorial on custom painting: http://docs.oracle.com/javase/tutori...ing/index.html

    Also the code calls setVisible(true) BEFORE all the components have been added to the frame. Components added AFTER the call to setVisible() might not be shown right away.
    Last edited by Norm; 04-04-2014 at 03:38 PM.
    Flopet17 likes this.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: 2048

    Think about a matrics with 4x4 squares...
    In the GameBox I draw a square and I want in the BoxContainer to call that square from the GameBox 16 times, and draw so at the end I have the 4x4 squares.
    Only use the square drawn in the GameBox, to draw the 4x4 squares in BoxContainer.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: 2048

    IF you want to build a 4x4 grid of components, use a GridLayout and have the layout manager build it for you.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: 2048

    I want to create this game in Java:
    2048
    I have the GameLogic already built, but now I have to create the interface...
    How do you suggest me to do it?
    My teacher told me to use JComponent to draw...

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: 2048

    See the link about custom painting: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Basically extend a component, override its paintComponent() method and do the drawing there using the Graphics passed to the method.
    Flopet17 likes this.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: 2048

    What I am doing wrong?
    It is still not working, it's not showing anything...
    Java Code:
    import java.awt.Graphics;
    import javax.swing.JComponent;
    
    public class BoxContainer extends JComponent{
        public void paintComponent(Graphics g) {
            
            GameBox GB[][] = new GameBox[4][4];
            for(int i=0;i<4;i++)
                for(int j=0;j<4;j++){
                    GB[i][j]= new GameBox(i*100,j*100,100,100);
                    add(GB[i][j]);
                    repaint();
                }
        }
    }
    Java Code:
    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;
        public GameBox(int x, int y, int w, int h){
            this.posX=x;
            this.posY=y;
            this.posW=w;
            this.posH=h;
        }
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawRect(posX, posY, posW, posH);
    
        }
        
    }

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: 2048

    Only the class that is to show the GUI needs to extend JComponent. The other class shouldn't be a component.

    Hard to test the posted code. It needs a main() method.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: 2048

    Quote Originally Posted by Norm View Post
    Only the class that is to show the GUI needs to extend JComponent. The other class shouldn't be a component.

    Hard to test the posted code. It needs a main() method.
    I don't understand what to do :(...

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: 2048

    If you want to draw a bunch of rectangles in a component, define a class that extends JPanel, override its paintComponent() method and add code there to draw the rectangles.
    The data for the rectangles could come from instances of a class that holds the rectangle's location and other things.
    Add an instance of the extended JPanel class to a JFrame.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: 2048

    What is wrong in my code? Why is not working?
    Java Code:
    public class MainFrame extends JFrame{
        public MainFrame(){
            setVisible(true);
        }
        public void addFrame(){
            setTitle("2048");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(404,436);
            
            BoxContainer BC = new BoxContainer();
            Container pane = getContentPane();
            pane.add(BC);
            
            setVisible(true);
            
        }
    }
    Java Code:
    public class Game2048 {
        public static void main(String[]args){
            MainFrame mf = new MainFrame();
            mf.addFrame();
        }
    }
    The main is the same... I only change the GameBox and BoxComponent class, there I have a problem and I don't understand why is not working...
    Last edited by Flopet17; 04-04-2014 at 04:28 PM.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: 2048

    I don't know. I'm not able to compile it for testing.

    Try what I've suggested.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: 2048

    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.Container;
    import javax.swing.JFrame;
    
    public class MainFrame extends JFrame{
        public MainFrame(){
            setVisible(true);
        }
        public void addFrame(){
            setTitle("2048");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(404,436);
            
            BoxContainer BC = new BoxContainer();
            Container pane = getContentPane();
            pane.add(BC);
            
            setVisible(true);
            
        }
    }
    Java Code:
    package game2048;
    
    import java.awt.Graphics;
    import javax.swing.JComponent;
    
    public class BoxContainer extends JComponent{
        public void paintComponent(Graphics g) {
            
            GameBox GB[][] = new GameBox[4][4];
            for(int i=0;i<4;i++)
                for(int j=0;j<4;j++){
                    GB[i][j]= new GameBox(i*100,j*100,100,100);
                    add(GB[i][j]);
                    repaint();
                }
        }
    }
    Java Code:
    package game2048;
    
    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;
        public GameBox(int x, int y, int w, int h){
            this.posX=x;
            this.posY=y;
            this.posW=w;
            this.posH=h;
        }
        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawRect(posX, posY, posW, posH);
    
        }
        
    }
    Here is the code... Can you tell me what is wrong? :(

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: 2048

    Why not try my suggestion first.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: 2048

    Because I have some instructions of how to do it from my teacher...
    He told me to use the following classes:
    Propun urmatoarele clase:

    - MainApplication - has main, instantiates MainFrame,

    - MainFrame - extends JFrame and has BoxContainer

    - BoxContainer - extends JComponent and contains all the rectangles (GameBox)

    - GameBox - extinde JComponent and draw one Rectangle

    This is they way I have to do it...

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: 2048

    Ok. There are several bugs in the code. You need to see what the current code is doing.
    Start debugging the code by adding println() statements to all the classes constructors and to the paintComponent() methods.
    For example:
    Java Code:
            System.out.println("GB x="+x +", y="+y);                        //for the GB constructor
      
        System.out.println("GB pC x="+posX + ", y="+posY + ", w="+posW + ", h="+posH + " bnds="+getBounds()); // for GB paintComponent()
    
        System.out.println("BC pC bnds="+getBounds() + " " + getComponents().length);   // for BC paintComponent()
    The print out will show you a problem.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: 2048

    It's overriding my components. why?

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default Re: 2048

    It's overriding my components.
    Can you explain what you mean?
    Why do you think that?
    What happens when the code executes?
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Posting Permissions

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