Question

Any thoughts on this, i can get it to run but can't pass code around and...

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);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backgroundblank" >
                  <Button
                    android:id="@+id/Button01"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/black1" />
</RelativeLayout>

this is the code

public class tested extends Activity {

Button buttonblack;
int score=0;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.tested);


    buttonblack = (Button)findViewById(R.id.black1);
    buttonblack.setOnClickListener(new View.OnClickListener() {

              public void onClick(View v){
              //score+10(i dont know how to make score +10 if the button clicked)
              //if the button clicked
              //Do some logic here
                                         }                
            });

if (score = 100){
         //the speed of move are increase more fast
                }

}
Add a comment
Know the answer?
Add Answer to:
Any thoughts on this, i can get it to run but can't pass code around and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with...

    Basic button tracking Deliverables Updated files for app6.java myJFrame6.java myJPanel6.java student.java Contents You can start with the files available here. public class app6 { public static void main(String args[]) {     myJFrame6 mjf = new myJFrame6(); } } import java.awt.*; import javax.swing.*; import java.awt.event.*; public class myJFrame6 extends JFrame {    myJPanel6 p6;    public myJFrame6 ()    {        super ("My First Frame"); //------------------------------------------------------ // Create components: Jpanel, JLabel and JTextField        p6 = new myJPanel6(); //------------------------------------------------------...

  • Debug this java and post the corrected code. /* Creates a simple JPanel with a single...

    Debug this java and post the corrected code. /* Creates a simple JPanel with a single "quit" JButton * that ends the program when clicked. * There are 3 errors, one won't prevent compile, you have to read comments. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class QuitIt extends JFrame {     public QuitIt() {         startIt(); //Create everything and start the listener     }     public final void startIt() {        //Create the JPanel        JPanel myPanel =...

  • Can someone modify my code so that I do not get this error: Exception in thread...

    Can someone modify my code so that I do not get this error: Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1043) at java.awt.Container.add(Container.java:363) at RatePanel.<init>(RatePanel.java:64) at CurrencyConverter.main(CurrencyConverter.java:16) This code should get the amount of money in US dollars from user and then let them select which currency they are trying to convert to and then in the textfield print the amount //********************************************************************* //File name: RatePanel.java //Name: Brittany Hines //Purpose: Panel for a program that convers different currencyNamerencies to use dollars //*********************************************************************...

  • Can you help me to link two frames in Java Swing and then if you click...

    Can you help me to link two frames in Java Swing and then if you click "Proceed button" it will link to the Second Frame...Thank you :) First frame code: //First import javax.swing.*; import java.awt.event.*; import java.io.*; public class Sample extends JFrame implements ActionListener { JMenuBar mb; JMenu file; JMenuItem open; JTextArea ta; JButton proceed= new JButton("Proceed"); Sample() { open = new JMenuItem("Open File"); open.addActionListener(this); file = new JMenu("File"); file.add(open); mb = new JMenuBar(); mb.setBounds(0, 0, 400, 20); mb.add(file); ta...

  • This is the java GUI program. I want to add an icon to a button, but...

    This is the java GUI program. I want to add an icon to a button, but it shows nothing. Can you fix it for me please? import java.awt.BorderLayout; import java.awt.Container; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; class Icon extends JFrame{ public static void main(String args[]){ Icon frame = new Icon("title"); frame.setVisible(true); } Icon(String title){ setTitle(title); setBounds(100, 100, 300, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); ImageIcon icon1 = new ImageIcon("https://static.thenounproject.com/png/610387-200.png"); JButton button1 = new JButton(icon1); p.add(button1); Container contentPane...

  • Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10...

    Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is pushed and then goes off.   Put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the number of seconds as it counts up from 0 to 10. THIS IS A JAVA PROGRAM. The image above is what it should look like. // Demonstrates mnemonics and tool tips. //********************************************************************...

  • In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

    In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • please help me debug this Create a GUI for an application that lets the user calculate...

    please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....

  • Deliverables app.java, myJFrame.java, myJPanel.java and student.java as requested below. (Use the student.java class that you already...

    Deliverables app.java, myJFrame.java, myJPanel.java and student.java as requested below. (Use the student.java class that you already have from previous labs) Contents Based on the graphics you learned this week Create an instance (an object, i.e., st1) of student in myJPanel Display his/her basic information Display for 10 times what he/she is up to (using the method whatIsUp() that your student class already has) -------------------------------------------------------------------------------------------------- \\app.java public class app { public static void main(String args[]) { myJFrame mjf = new myJFrame();...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT