Results 1 to 7 of 7
  1. #1
    young2link is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Help needed on reaction time programm

    Hello dear community,

    I'm new to programming and am rather stuck on this assignment.
    I need to make a program that shows a figure and after a random time changes its color. the user then has to click and the time that has passed should be measured and shown on screen.
    so far I have this:
    no matter what I do in my void startTest(){ it wont do anything. could you point out my mistakes so far and maybe give me some tips as to what I should do next?
    I know how to do all the layout stuff, but I have no idea how to work with actionlistener.



    Java Code:
    import javax.swing.*;                 //the right files are imported so all the utilities work
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.Border;
    import javax.swing.border.LineBorder;
    
    
    class ReactionExperiment implements MouseListener, ActionListener {
      
      Timer timer;
      Timer timer2;
      int random1 = (int)(1 + Math.random() * 2);
      boolean running = false;
      boolean pressedit = false;
      int clicks = 0;
      JFrame window = new JFrame("ReactionTimer");
      IntLabel reaction_1CountLabel = new IntLabel( "reaction 1");
      IntLabel reaction_2CountLabel = new IntLabel( "reaction 2");
      IntLabel reaction_3CountLabel = new IntLabel( "reaction 3");
      IntLabel reaction_4CountLabel = new IntLabel( "reaction 4");
      IntLabel reaction_5CountLabel = new IntLabel( "reaction 5");
      JButton startbutton = new JButton("start");
      JButton resetbutton = new JButton("reset");
      JPanel topPanel = new JPanel();                         
      JPanel centerPanel = new JPanel();
      JPanel countPanel = new JPanel();
      
      ReactionExperiment() {                  
            countPanel.add( startbutton );
            countPanel.add( resetbutton );
            countPanel.add( reaction_1CountLabel );
            countPanel.add( reaction_2CountLabel );
            countPanel.add( reaction_3CountLabel );
            countPanel.add( reaction_4CountLabel );
            countPanel.add( reaction_5CountLabel );
            window.add(topPanel, BorderLayout.NORTH);   
            window.add(centerPanel, BorderLayout.CENTER);
           // centerPanel.setBackground(Color.PINK);     
            window.add(countPanel, BorderLayout.SOUTH);
            window.setSize(600,400);
        }
      
        void start() {
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
            centerPanel.addMouseListener( this );
            window.setVisible( true );
        }
        
     public void mousePressed( MouseEvent  e) { 
              if(e.getButton() == MouseEvent.BUTTON1)
    {
      pressedit = true;
    }
        }
     public void buildGUI(){
       //code
     }
     
     void startTest(){
       timer.setInitialDelay(random1);
       if(pressedit == true){ 
     }
     
     public void nextTest(){
     }
     
    public void actionPerformed( ActionEvent e) {
      if (e.getSource() == startbutton){
         if(running == false){ 
           startTest();
              running = true;  
         }else{
             nextTest();
         }
      }
    }
      
    public class IntLabel extends JLabel {  //a subclass is made so I don't have to repeat the layout of every Label
    private String type;
    
    public IntLabel(String type){
    this.type = type;
    setBorder(BorderFactory.createLineBorder(Color.black)); //gives all labels a nice and simple black border
    update(0 );        
    } 
    public void update(int counter){      
    setText(type+":"+counter);            
    }
    }
    
    //empty methods of mouselistener
      public void mouseReleased( MouseEvent e) {}
      public void mouseClicked( MouseEvent e) {}
      
      public void mouseExited( MouseEvent e) {  }
      public void mouseEntered( MouseEvent e) {  }
      
      public static void main(String[] args) {
            new ReactionExperiment().start();
    }
    }
    Last edited by young2link; 10-14-2012 at 05:57 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Help needed on reaction time programm

    Hello and welcome! Please use [code][/code] when posting code so we can read!

    I don't see anything happening in your startTest method either - just an empty conditional. Did you expect this to do something?

  3. #3
    young2link is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help needed on reaction time programm

    I posted it blank because no matter what I filled in nothing worked. did I do something wrong on the outside of the code?

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Help needed on reaction time programm

    Make sure you put a System.out.println() in each place where you expect something to happen. I'm betting that either the actionListener is never firing, you're looking for the wrong action, or something similar. If you put a s.o.p. between these lines:

    Java Code:
    public void actionPerformed( ActionEvent e) {
      if (e.getSource() == startbutton){
    does anything happen?

  5. #5
    young2link is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help needed on reaction time programm

    It does not, but it should. Did I use the actionevent wrong?

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Help needed on reaction time programm

    I see that you implemented action listener, but I don't see you actually adding the instance listener (this) to anything.

    Java Code:
    window.addActionListener(this)
    Something like that!

  7. #7
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    12
    Rep Power
    0

    Default Re: Help needed on reaction time programm

    Any time you call a get/set method, you should reference the object. For instance, in your code you have:
    Java Code:
    this.type = type;
    setBorder(BorderFactory.createLineBorder(Color.black)); //gives all labels a nice and simple black border
    update(0 );       
    }
    public void update(int counter){     
    setText(type+":"+counter);
    You should have something like,
    Java Code:
    window.setborder(BorderFactory.createLineBorder(Color.black));
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

Similar Threads

  1. Replies: 1
    Last Post: 01-15-2012, 03:50 PM
  2. Err in programm
    By nassir in forum JCreator
    Replies: 5
    Last Post: 02-27-2010, 02:19 AM
  3. Marks programm
    By blackmoon in forum AWT / Swing
    Replies: 5
    Last Post: 12-08-2009, 11:06 PM
  4. web programm (.jsp)
    By renars1985 in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 04:23 PM
  5. web programm (.jsp)
    By renars1985 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-18-2007, 09:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •