Hey Guys, I am posting this in a new post because i got something different.
This is my Assignment:
This assignment handles topics of events and GUI-elements. It's a little game. The task: create a button that randomly relocates to a different position on the screen each time it is hit. The player has to hit the button 15 times, the score is the time needed.
Your first task: write the necessary event handling and the game-framework (counter, score etc.). Measure the time using java's System.currentTimeMillis() method (see API). Display the time elapsed and the number of remaining hits on separate JLabels.
I have worked on this code for 4 days straight now. It is due in 24 hours and i really need your guys help.
This is the code i have so far. I need to add a north and south panel and make the timing and clicker work.
The members have helped me so much, but i still cannot get it.
Can some1 please help me:
Code:import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class JumpingPanel extends JFrame{
public static final int BUTTON_WIDTH = 45;
public static final int BUTTON_HEIGHT = 45;
private static final Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize(); // Dimension of the screen
private JPanel middle; // The content panel of this frame
private TargetPanel target; // The button array
private static void resizeComponent(Component c) {
Dimension d = c.getSize(null);
c.setPreferredSize(new Dimension((int)(0.9*d.width), (int)(0.9*d.height)));
}
// The panel with 3x3 array of buttons
private static class TargetPanel extends JPanel implements ActionListener {
private JButton theMainButton; // Central button in button array
private Random rand; // Random number generator used in relocating
/** When we hit a button, if the central one, we relocate
the target panel
*/
public void actionPerformed(ActionEvent ae) {
JButton source = (JButton)ae.getSource();
if (source == theMainButton) {
resizeComponent(this);
setLocation(rand.nextInt(screenSize.width-3*BUTTON_WIDTH),
rand.nextInt(screenSize.height-3*BUTTON_HEIGHT));
validate();
}
}
// Here is the target panel with the nine buttons
public TargetPanel() {
rand = new Random();
setLayout(new GridLayout(3,3));
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
JButton button = new JButton();
if (r == 1 && c == 1) {
theMainButton = button;
theMainButton.addActionListener(this);
}
button.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
add(button);
}
}
setMaximumSize(new Dimension(3*BUTTON_WIDTH,3*BUTTON_HEIGHT));
Border border = BorderFactory.createLineBorder(Color.BLACK, 4);
setBorder(border);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JumpingPanel frame = new JumpingPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Jumping Panel");
frame.setMinimumSize(screenSize);
frame.middle = (JPanel)frame.getContentPane();
frame.middle.setLayout(new FlowLayout());
frame.target = new TargetPanel();
frame.middle.add(frame.target);
frame.setVisible(true);
}
}

