Results 1 to 4 of 4
Thread: Button Game horror
- 01-21-2010, 10:20 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
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);
}
}
- 01-21-2010, 11:04 PM #2
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?
- 01-22-2010, 12:39 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
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
- 01-24-2010, 05:20 AM #4
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
Progress bar, threads and swing horror
By taptapthat in forum AWT / SwingReplies: 0Last Post: 11-06-2009, 10:40 AM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM -
Need help with a menu button in a game
By pjr5043 in forum Java AppletsReplies: 1Last Post: 12-07-2008, 09:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks