1

Is there a way to add objects to a JFrame without using Layout Manager? I have tile objects(for the game 2048) that I am trying to add to a JFrame so I can call the JFrame then have a loop forever where the tiles are forever repainting themselves and I can press arrows to make them move depending on constraints(like if it can move in a specific direction.

Why I don't want to use a specific layout manager - my objects are tiles in the game 2048- which means they are constantly changing position which would mess with the layout manager setup ie flowlayout where all the JPanel objects are in a specific order and position.

Heres where I am trying to instantiate the JFrame:

   public static void main(String[] args) throws InterruptedException {
       //set up JFrame, tile objects
       frame = new JFrame();
        a = new tile(100, 100, frame);
        b = new tile(200, 200, frame);
       frame.addKeyListener(a);
        frame.add(a); 
        frame.add(b);


        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

               


        frame.setSize(500, 500);
        frame.setVisible(true); 
        //a loop so that it is continuously repainting and when i press a key something else happens
        while(true) {
            a.repaint();
            b.repaint();
            Thread.sleep(10);
        } 
camickr
  • 321,443
  • 19
  • 166
  • 288
  • *have a loop forever* - don't use an infinite loop. For animation you should use a `Swing Timer`. *I can press arrows to make them move* - how do you know which tile to move? Generally for a game where you have multiple objects to paint you would use custom painting. Check out: https://stackoverflow.com/questions/54028090/get-width-and-height-of-jpanel-outside-of-the-class/54028681#54028681. It is not a game, but it demonstrates a Swing Timer with animation. – camickr Jun 27 '21 at 14:56
  • @camickr I will fix that later but for now I am interested in how to add objects to JFRame thanks – babayaga007 Jun 27 '21 at 17:55
  • Create a drawing JPanel and draw the 2048 squares on the drawing JPanel. Create a logical model of the values of your tiles, and draw the tiles on the drawing JPanel based on the logical model. If this is confusing, take a look at my [2048 game](https://github.com/ggleblanc2/2048) on GitHub. – Gilbert Le Blanc Jun 27 '21 at 22:25
  • ok thanks guys, will try – babayaga007 Jun 27 '21 at 22:28

1 Answers1

1

It is possible to work with Swing without LayoutManager. Not using a LayoutManager allows and requires the application to have full control on the absolute position of the components.

Check out these fine resources:

In a nutshell, creating a container without a layout manager involves the following steps:

  • Set the container's layout manager to null by calling setLayout(null).
  • Call the Component class's setBounds() method for each of the container's children.
  • Call the Component class's repaint() method.
Queeg
  • 7,748
  • 1
  • 16
  • 42
  • 1
    Just because you can do something, doesn't mean you should do something. Swing was designed to be used with [layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). – Gilbert Le Blanc Jun 28 '21 at 08:18