Question: In my code below, I don't know how to add: 1. A randomly moving JButton 2. A score for every time the JButton is clicked

In my code below, I don't know how to add:

1. A randomly moving JButton

2. A score for every time the JButton is clicked

3. A JProgressBar that counts down 60 seconds

APP

public class app {

public static void main(String args[]) { MainFrame mjf = new MainFrame(); } }

MAINFRAME

import java.awt.*; import javax.swing.*;

public class MainFrame extends JFrame {

GamePanel game;

public MainFrame() { super("Lab 09 - A Game"); MacLayoutSetup(); game = new GamePanel(); getContentPane().add(game, "Center"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(1200, 600); setVisible(true); }

public void MacLayoutSetup() { // On some MACs it might be necessary to have the statement below //for the background color of the button to appear try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } } }

GAME PANEL import java.awt.Color; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JPanel;

public class GamePanel extends JPanel {

JButton b1; int score = 0;

public GamePanel() { super(); setBackground(Color.white); b1 = new JButton("click me"); add(b1); }

@Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("score = " + score, 10, 520); g.drawString("Press Spacebar to start the game", 10, 540); g.drawString("You have 60 seconds to keep clicking on the button to score", 10, 560); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!