3

I am trying to build a small monopoly game. Here you can see the part of the code that I am having trouble with.

public class Board extends JPanel {
    private ImageIcon image;
    private JLabel label;
    static Player p1;
    static Player p2;

    public Board() {
        p1 = new Player("Jack", 1); 
        p2 = new Player("Matt", 2);
        setLayout(null);
        image = new ImageIcon("board.jpg");
        label = new JLabel(new ImageIcon("/Users/Onurcan/Documents/"
                + "workspace/Monopoly/src/board.jpg"));

        add(p1.getToken());
        add(label);
    }

    public class Monopoly {


    public Monopoly() {
        JFrame frame = new JFrame("Monopoly");
        frame.setSize(1378, 900);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Board(), BorderLayout.WEST);
        frame.setVisible(true);
    }   
}

As you can guess I create a new Monopoly object in my test class to run this program. When I remove the setLayout(null) I can see both token and board in program. But without typing setLayout(null) I cannot move my token to starting point. While creating player if second parameter is 1 that means token is a hat, if second parameter is 2 it means it is a shoe. So I am asking that how can I do not lose visual with setLayout(null)? Or withouy setting layout to null how can I change my tokens position?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mehr
  • 170
  • 1
  • 1
  • 8
  • 1
    null layout is about hardcoding possition, please did bother you with Oracle tutorial about Layout managers – mKorbel Apr 09 '15 at 05:55
  • My guess offhand is your token is being added to the layout, which is awkward ad best and not the best way to handle animation. You should be using a graphics context or something like that and operating within the rectangle (or a layer above) where the image is placed. – clearlight Apr 09 '15 at 05:56
  • 1
    Check out this site for another approach to consider: http://zetcode.com/tutorials/javagamestutorial/movingsprites/ – clearlight Apr 09 '15 at 05:58

2 Answers2

3

Actually there are two was to position components on container.

  1. Bad way. Using null layout and manually setting bounds or size or location. It's really bad approach because moder UI is not suppose to reflect pixel perfect things good enough.

  2. Define proper LayoutManager (or multiple nested panels with own LayoutManagers). Thus all component' sizes/position are calculated according to LayoutManager's rules.

StanislavL
  • 56,971
  • 9
  • 68
  • 98
2

So I am asking that how can I do not lose visual with setLayout(null)?

Start by taking a look at Laying Out Components Within a Container. The layout management API is responsible for making decisions about how big components should be and where they should be located, based on their implementation.

Once you remove this, your component doesn't know how it should be sized or positioned. In fact, by default it's 0x0x0x0.

Layout management is a fundamental aspect of the Swing API

without setting layout to null how can I change my tokens position?

Well, the short answer is to call setBounds on the components and give them a size and position. This approach can be troublesome at best, because the layout management API is so central to the whole Swing API

The longer answer, and the one I would prefer, would be to write a layout manager which could make the decisions for you. Knowing where each player is on the board and how best to place the pieces on the board based on it's configuration

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I do not have much information about writing a layout manager. But I will start to research now. Thanks for your help. – Mehr Apr 09 '15 at 06:23