Any thoughts on this i can get it to run but cant pass code
Any thoughts on this, i can get it to run but can\'t pass code around and the button doesnt move around.
i didnt pass the student, or the app.java.
What will you learn
Combined use of Timer and the tracking of user interactions
Deliverables
app.java
myJFrame.java
myJPanel.java
and other necessary Java files
Contents
The objective of the lab is to create a game in which the player has to click on a moving student-button to score.
The student button has to move constantly (using the timer)
The application has to keep the score
The actual score has to be shown.
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:
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
Keep the score on a separate button, every click on the student button increases the score by 1.
When these two things are working change the image of the student button when it is clicked
When 3 is implemented, 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, make the movement smooth instead of jumping from one place to another place very far away
Makes the student-button run faster when the mouse approaches it
When 5 and 6 are implemented, then do whatever else you want to add to it
_______________________________________________________________________________________
what i have so far:
-------------------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class myJPanelTimer extends JPanel implements ActionListener
{
myJPanelTimer p2;
myJPanelstd p1;
JButton b1,jb1;
Timer tim;
int limit = 0;
int delay = 0;
int i = 0;
JButton start;
public myJPanelTimer()
{
super ();
jb1 = new JButton(\"a real student-button should be here, not me, a simple JButton ...\");
add(jb1);
//jb1.setBounds(new Rectangle(100,520,100,75));
//------------------------------------------------------
// 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 setPanel2(myJPanelstd p3){
p1 = p3; }
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;
Random t = new Random();
jb1.setLocation(t.nextInt(getWidth()-25),t.nextInt(getHeight()-25));
b1.setText(\"counting ... \"+i);
}
}
}
---------------------------------
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class myJPanelstd extends JPanel
{
myJPanelTimer p2;
myJPanelstd p1;
JButton jb1;
public myJPanelstd()
{
super();
setBackground(Color.pink);
// setLayout(null);
JButton b1, b2, b3;
b1 =new JButton(\"fake timer\");
b2 = new JButton(\"click me\");
b3 = new JButton(\"score\");
add(b1);
add(b2);
add(b3);
b1.setBounds(new Rectangle(50,150,100,75));
// p2 = new myJPanelTimer();
//-------------------------------------------------------
// add buttons to JPanel
//-------------------------------------------------------
p2 = new myJPanelTimer();
add(p2);
p2.setPanel2(p1);
// Move the rat randomly, subtract 75, so that the rat should not meet the edges
//lb.setLocation(r.nextInt(getWidth()-75),r.nextInt(getHeight()-75));
}
public void setPanel1(myJPanelstd panel1){
p1 = panel1;
}
}
-----------------------------
import java.awt.*;
import javax.swing.*;
public class myJFrame extends JFrame
{
myJPanelstd mjp;
public myJFrame ()
{
super (\"My First Frame\");
//------------------------------------------------------
// Create components
mjp = new myJPanelstd();
myJPanelTimer p2 = new myJPanelTimer();
myJPanelstd p1 = new myJPanelstd();
//------------------------------------------------------
// Choose a Layout for JFrame and
// add Jpanel to JFrame according to layout
getContentPane().setLayout(new BorderLayout());
getContentPane().add(mjp,\"Center\");
//------------------------------------------------------
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize (640, 480);
setVisible(true);
}
}
Solution
Here is a code where u can make the button move constantly for the student.Hope this will be helpful for you:
this is my xml
this is the code




