Lab 10 – The Student Game

$25.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (3 votes)

What will you learn-Combined use of Timer and the tracking of user interactions

Deliverablesapp.java, myJFrame.java, myJPanel.java, and other necessary Java files

ContentsThe objective of the lab is to create a game in which the player has to click on a moving student-button to score.
1.The student button has to move constantly (using the timer)
2.The application has to keep the score
3.The actual score has to be shown.
4.Implement at least one of the extras listed below (#3 to #7)

– level of difficulty: High
– level of usefulness for the final project: Very High

I am giving you
– the timer example
– the new hybrid student-button (use yours from Lab 09)
– the mouse motion and mouse click examples
– the null layout example

Suggestion: start with the basic timer Java example and made the necessary changes to it.

Suggested steps:
#1 get the student button moving
you need the get timer started
you need a null layout (check the layout Java examples)
you need to set a different position for the button every time the timer ticks

#2 keep the score on a separate button, every click on the student button increases the score by 1.

when these two are working, then:

#3 changes the image of the student button when it is clicked

when #3 is implemented, then:

#4 add a slider to make the button move faster or slower
– you need to use the setDelay() method applied to the timer.

when #4 is implemented, then:

#5 makes the movement smooth instead of jumping from one place to another place very far away.

#6 makes the student-button run faster when the mouse approaches it

when #5 and 6 are implemented, then

#7 whatever else you want to add to it

Lab10 guide

Lab 10 Guide for a basic version

Start with app.java, myJFrame.java, and myJPanel.java

1- in myJPanel, add 3 buttons with the following text inside each one

– b1 “fake timer”

– b2 “click me”

– b3 “score”

2- see the example of null layout here

Now using setLayout(null); for the panel, try to place the 3 buttons somewhere

for each button, you will need a setBounds(new Rectangle(x,y,width,length)

For instance, for a button b2

b2.setBounds(new Rectangle(10, 20, 50, 30);

3- implement a listener for b2 “click me”, for when it is clicked it adds 1 to a counter and shows the counter on b3 “score”.

4- implement a listener for b1 “fake timer”, for when it is clicked it moves b2 “click me” to another random place.

You will need a

b2.setBounds(new Rectangle(x, y, 50, 30);

x and y needs to be random values.

Go to student.java and see how you can create a random number.

5- the Timer

The timer will replace the fucntion of the “fake timer” button.

Once it gets started with “tim.start( );”, the timer generates events that are caught in the actionPerformed method.

see the lesson here.

Remember that the Timer:

– is a variable/class like any other

– it has a start( ) method to get it ticking, a stop( ) method to stop it.

– it has a delay, which is the time interval between ticks.

– when it has been start, it generates events (like a button) that are caught by the action performed method.

There you can ask if(obj==tim)

{

//do something here, or move the button b2 here

}

Below a panel with a basic timer.

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

public class myJPanelTimer extends JPanel implements ActionListener
{
Timer tim;
int limit = 0;
int delay = 0;
int i = 0;
JButton start, b1;
public myJPanelTimer ()
{
super ();
//——————————————————
// Create components: Jpanel, JButton
//——————————————————
start = new JButton(“start”);
b1 = new JButton(“counter”);

start.addActionListener(this);
add(start);
add(b1);
//——TIMER ——————————————-
delay = 1000; //milliseconds
tim = new Timer(delay, this);// the word ‘this’ here means that the action listener
// that the timer needs is this very same class ‘myJPanelTimer
// This way the action performed method is called every time the timer ticks.
//——TIMER ——————————————-
}

//——————————————————————-
// actionPerformed
public void actionPerformed(ActionEvent event)
{

Object obj = event.getSource();
String choice = event.getActionCommand();
if (obj == start)
{
tim.start();
}
//============================================================
// every tick of the timer clock will call
if (obj == tim)
{
i = i+1;
b1.setText(“counting … “+i);
}

}
}