Results 1 to 4 of 4
  1. #1
    MartinBenes71 is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default Button Game horror

    okay so I need a lot of help. I need to make 1000 buttons move and one of those buttons has to run away from the mouse. I already have the code for calling the 1000 buttons, but i dont know what to do next, how to make the buttons move and what not.

    heres my code:

    import javax.swing.*;
    import java.util.*;
    class ButtonGame extends JFrame implements java.awt.event.ActionListener {

    private JButton[] buttons;
    private JPanel p;
    private Random random;

    public static void main(String[] args){
    ButtonGame app = new ButtonGame();
    }
    ButtonGame(){
    System.out.println("Hi");
    this.setVisible(true);
    this.setBounds(10,10,500,500);

    p = new JPanel(null);
    this.add(p);

    random = new Random();
    buttons = new JButton[100];
    for(int i = 0; i < buttons.length; i++){
    buttons[i] = new JButton("Click Me");
    buttons[i].addActionListener(this);
    buttons[i].setBounds(random.nextInt(1500) + 10,
    random.nextInt(700) + 10,
    random.nextInt(150) + 10,
    random.nextInt(250) + 10);
    p.add(buttons[i]);
    }
    }
    public void actionPerformed(java.awt.event.ActionEvent e){
    System.out.println("Ouch");
    ((JButton)e.getSource()).setVisible(false);
    }
    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    well, you'll have to use some kind of absolute layout manager for the placement of your button, and may have to override the painting methods in your JPanel depending on what it is you're trying to do. Perhaps you could place the buttons using nested loops, or store their position in a 2d array. 'Running' away from the mouse will be tough because I'm not sure java will let components like buttons overlap - I've never tried. I assume the 1000 buttons are supposed to be in a 100x100 grid?

  3. #3
    MartinBenes71 is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    well what i mean by the button moves ahead of the mouse cursor or follows it. the grid is 500x500,I can bring it down though. hmm I shall try the nested loops/2d array. I kinda figured out the button being ahead/behind the cursor. thanks for the help

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Looks like both are maths are wrong. You said 1000 buttons? 500x500 is 250000 buttons. I said 100x100 but I meant something more like 32x32 = 1024.

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. Progress bar, threads and swing horror
    By taptapthat in forum AWT / Swing
    Replies: 0
    Last Post: 11-06-2009, 11:40 AM
  3. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM
  4. Need help with a menu button in a game
    By pjr5043 in forum Java Applets
    Replies: 1
    Last Post: 12-07-2008, 10:30 PM

Posting Permissions

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