user2974951
user2974951

Reputation: 10375

JPanel buttons and such

So I'm doing this http://en.wikipedia.org/wiki/Futoshiki and I have my JPanel with my buttons (5x5), now I need to insert a random number (3-7) of < or > operators in between some buttons as per the image. How would I go about doing that? I'm not asking for the code, but rather for the logic one would use to complete such a task. These operators must of course be legal, as they'll be used later to solve the game.

public class Test implements ActionListener {
    JButton[][] gumbi = new JButton[5][5];

public Test() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);

    JPanel panel = new JPanel(new GridLayout(5, 5));

    Random op = new Random();
    int operatorji = op.nextInt(5) + 3;     //the random number of operators

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            gumbi[i][j] = new JButton("0");
            gumbi[i][j].addActionListener(this);
            panel.add(gumbi[i][j]);
        }
    }   

    frame.add(panel);
    frame.setVisible(true);
}

Also, how would I also add ∨ and ∧ operators (vertically speaking)?

Upvotes: 0

Views: 234

Answers (1)

axl411
axl411

Reputation: 930

Here's what I would do based on your code:

  • Make some horizontal and vertical gaps between buttons (to make space for operators)
  • For displaying operators, I would just draw them on the panel
  • I would write a method like drawOperator(String operator, int h, int v, Graphics2D g), using h, v to locate the operator (imaging a X*X grid system to locate the operator), and somehow draw the operator into the correct gap between buttons.
  • The locations of these operators can also be randomly generated (since the location is an imaginary X*X grid)

Might be a little too much for a small exercise though ^^

Upvotes: 1

Related Questions