Results 1 to 7 of 7
  1. #1
    giganews35 is offline Member
    Join Date
    Sep 2008
    Location
    Aurora IL
    Posts
    5
    Rep Power
    0

    Default Help with my first program...newbie

    Hey guys, as you can see I am kind of a newb with coding in Java, so I would like to ask for your help to help me learn and advance, obviously finishing my fist program.

    Issues I am having:
    Reaction time measure when ENTER key is pressed.
    stage() method in ADT(greenlight) output green light

    Please be easy on me I am a newb, so if you see something wrong which most likely the enitre program is not well put together. Let me know! I am not asking for you guys to write the program for me either I want to learn how to code but I will most likely ask very basic questions.

    Here is what I wrote so far:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.imageio.*;
    import java.io.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    
    public class bracket extends JFrame implements ActionListener, KeyListener  
    //main class
    {
    	
    	private Image strip; //image name using with IO
    	private JButton fred, joe; //button names
    	private JPanel panel;//panel name
    	private drag L,R; //objects accessing ADT
    	
    	
    	  public static void main (String[] A)
    	  
    	  {
    		    bracket frame = new bracket();
    		    frame.setSize(800,600);
    		    frame.view();
    		    frame.show();
    		    
    	  }
    
    	  
    	  
    	  private void view() 
    	  {
    		    setDefaultCloseOperation (EXIT_ON_CLOSE);
    		    Container window = getContentPane ();
    		    window.setLayout(new FlowLayout ());
    		    panel = new JPanel();
    		    panel.setPreferredSize (new Dimension (776,445));
    		    panel.setBackground(Color.white);
    		    window.add(panel);
    		    fred = new JButton ("Reset");
    		    window.add(fred);
    		    fred.addActionListener(this);
    		    joe = new JButton ("Stage");
    		    window.add(joe);
    		    joe.addActionListener(this);
    		    
    		    //ADT initial constructor, sets delay
    		    //Object R uses parameters from Object L by adding an x 
    		    //ordinate in the ADT because the object stays the same just 
    		    //needs to be over one way 
    		    L=new drag(panel,0,0,20,0);
    		    //left side of the xmas tree (int i,int o, int j)
    		    R=new drag(panel,54,35,30,55);
    			//right side of the xmas tree (int i,int o, int j)
    		    
    		    
    		    
    		    //reads image so it can be used using the draw method
    		    try
    		    {
    				  System.err.println("Now Loading...");
    				  
    				    File background = new File("F:/Workplace/strip.jpg");
    				    strip = ImageIO.read(background);
    				    
    				  System.err.println("Loaded Successfully");  
    		    }
    			  catch (Exception e)
    			{
    				  //prints an error message if file can not be found/loaded
    				  System.err.println("Unable to locate file(s)");
    			}
    
    	  }
    	  
    	  public void actionPerformed (ActionEvent event)
    	  
    	  {
    		  //draws background
    		  Graphics g = panel.getGraphics();  
    		  //draws the IO read file
    		  g.drawImage(strip,1,1,this);
    		  //button function
    		  
    		   Object C = event.getSource();
    		   if (C == joe)
    		   {
    		
    		   L.tick();
    		   R.tick();
    		   
    		   }
    		   
    		  /* Object B = event.getSource();
    		   if (B == fred) 
    		   {
    			
    		   } */ 
    	
    		  
    	
    	  }
    	
    	  public void keyTyped(KeyEvent e) {
    	        //
    	    }
    	 
    	    public void keyPressed(KeyEvent e) {
    	        if (e.getKeyCode() == KeyEvent.VK_ENTER)
    	        {
    	        L.stage();
            	R.stage();
    	        System.currentTimeMillis();
    	        	
    	        }
    	       
    	    }
    	 
    	    public void keyReleased(KeyEvent e) {
    	        // 
    	    }
    }
    //use three timers one for stage and yellow lights, one for green and one for red. On green use key pressed to start 
    //System.currentTimeMillis(), figure out how to measure and display the reaction time
    //If VK_ENTER is pressed before the yellow clears, activate timer for Red light to appear
    Java Code:
    Adt
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    
    
    public class drag implements ActionListener
    {
    	private JPanel panel;
    	private int x,y,q;
    	private Timer countdown, greenlight;
    	
    
    
    		public drag(JPanel panel2, int i, int o, int j, int i2)
    		{			
    			panel=panel2;
    			x=i;
    			y=i2;
    			q=0;//go counter
    			
    		
    			countdown= new Timer(j, this); //timer
    			greenlight= new Timer(o,this); //stage light timer
    			
    
    		}
    			
    		
    
    		public void tick() //timer to be used in main class
    		{	
    			countdown.start(); //start method
    			
    		}
    		public void stage()//method to start timer in main class
    		{
    			stagetimer.start();// start method
    			
    		}
    		
    		
    		
    		public void actionPerformed (ActionEvent event)
    		{
    		if(event.getSource()==countdown){
    			Graphics k = panel.getGraphics(); 
    			k.setColor(Color.yellow);
    			
    			// integer q as a counter with if statements drawing my lights
    			q=q+10;
    			
    			if(q>50)
    			{
    				
    				k.fillOval(345+y,120,12,12);
    			
    				k.fillOval(360+y,120,12,12);
    			
    			}
    			
    				
    			if(q>150)
    			{
    				k.fillOval(345+y,150,12,12);
    				k.fillOval(360+y,150,12,12);
    					
    			}
    			
    		  	if (q>350)
    		  	{
    			k.fillOval(349+x,170,25,25);
    		  	}
    		  	
    		  	if (q>450)
    		  	{
    		    k.fillOval(349+x,203,25,25);
    		  	}
    		  	
    		  	if (q>550)
    		  	{
    		    k.fillOval(349+x,234,25,25);
    		        
    		    q=0; //resets counter to zero 
    			countdown.stop(); //stops the timer right after the last set of lights
    			
    		  	}
    		 
       
    		 if(event.getSource()==greenlight)
    	{
    		
    		Graphics k1 = panel.getGraphics(); 
    		q=0;
    		k1.setColor(Color.green);
    		if(q>1)
    		{
    	    
    		k1.fillOval(349+x,266,25,25);
    		
    		q=0;
    		greenlight.stop();
    	    }
       
    	}
    		}
    		}
    		
    }
    Last edited by giganews35; 12-01-2008 at 02:49 AM.

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Please use code tags (edit your post) and explain your exact problems/post full error messages.

    Makes it a much easier for me and every1 else to read.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Yeah, I agree. You really need to rephrase your question so that it's coherent and answerable.

  4. #4
    giganews35 is offline Member
    Join Date
    Sep 2008
    Location
    Aurora IL
    Posts
    5
    Rep Power
    0

    Default

    Ok the program as it sits right now has no errors.

    What I am doing is making a drag racing "game" program that measures your reaction time based off a drag x-mas tree.

    What it does is it displays my background with a drag racing christmas tree.

    The stage lights and yellow lights are controlled by a(countdown) timer. And I would like to control the green light as well as the red light. I have a delay set with (left,right)L,R ADT objects in the main class.

    ( Not sure what to do I tried using the KeyEvent, KeyListener to make the green light appear when the ENTER key was pressed) I do not know how to go about doing that especially using it in the abstract data type.

    Now I also need to measure the reaction time starting from the last yellow light until the (ENTER)key is pressed. And if ENTER is pressed to soon the Red light needs to appear. I believe this needs to be done using the System.currentTimeMillis().

    And eventually display the reaction time for both the sides. (Right or left side doesn't matter would be the computers reaction time, green light always goes on, reaction time measure based on computer speed from yellow to green light) Player 1 based on how long it take to press ENTER after the last yellow light clears.
    Last edited by giganews35; 12-01-2008 at 04:09 AM.

  5. #5
    giganews35 is offline Member
    Join Date
    Sep 2008
    Location
    Aurora IL
    Posts
    5
    Rep Power
    0

    Default

    I hope that simplified this a little more...

  6. #6
    giganews35 is offline Member
    Join Date
    Sep 2008
    Location
    Aurora IL
    Posts
    5
    Rep Power
    0

    Default

    Ok I used buttons instead of KeyEvents, and so far I can control the left side and the right side is automatic.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.imageio.*;
    import java.io.*;
    //import java.awt.event.KeyEvent;
    //import java.awt.event.KeyListener;
    
    
    public class bracket extends JFrame implements ActionListener 
    //main class
    {
    	
    	private Image strip; //image name using with IO
    	private JButton fred, joe, jay; //button names
    	
    	private JPanel panel;//panel name
    	private drag L,R; //objects accessing ADT
    	
    	
    	  public static void main (String[] A)
    	  
    	  {
    		    bracket frame = new bracket();
    		    frame.setSize(800,600);
    		    frame.view();
    		    frame.show();
    		    
    	  }
    
    	  
    	  
    	  private void view() 
    	  {
    		    setDefaultCloseOperation (EXIT_ON_CLOSE);
    		    Container window = getContentPane ();
    		    window.setLayout(new FlowLayout ());
    		    panel = new JPanel();
    		    panel.setPreferredSize (new Dimension (776,445));
    		    panel.setBackground(Color.white);
    		    window.add(panel);
    		    jay = new JButton ("GO 1");
    		    window.add(jay);
    		    jay.addActionListener(this);
    		    fred = new JButton ("Reset");
    		    window.add(fred);
    		    fred.addActionListener(this);
    		    joe = new JButton ("Stage");
    		    window.add(joe);
    		    joe.addActionListener(this);
    		    
    		    //ADT initial constructor, sets delay
    		    //Object R uses parameters from Object L by adding an x 
    		    //ordinate in the ADT because the object stays the same just 
    		    //needs to be over one way 
    		    L=new drag(panel,0,10,30,0);
    		    //left side of the xmas tree (int i,int o, int j)
    		    R=new drag(panel,54,20,40,55);
    			//right side of the xmas tree (int i,int o, int j)
    		    
    		    
    		    
    		    
    		    //reads image so it can be used using the draw method
    		    try
    		    {
    				  System.err.println("Now Loading...");
    				  
    				    File background = new File("F:/Workplace/strip.jpg");
    				    strip = ImageIO.read(background);
    				    
    				  System.err.println("Loaded Successfully");  
    		    }
    			  catch (Exception e)
    			{
    				  //prints an error message if file can not be found/loaded
    				  System.err.println("Unable to locate file(s)");
    			}
    
    	  }
    	  
    	  public void actionPerformed (ActionEvent event)
    	  
    	  {
    		  //draws background
    		  Graphics g = panel.getGraphics();  
    		  //draws the IO read file
    		  g.drawImage(strip,1,1,this);
    		  //button function
    		  
    		   Object C = event.getSource();
    		   if (C == joe)
    		   {
    		
    		   L.tick();
    		   R.tick();
    		 
    		   
    		   
    		   }
    		 
    		   
    		   Object A = event.getSource();
    		   if (A == jay) 
    		   {	
    			   L.stage();
    			   
    			  	
    		   }  
    			 
    	  }
    	
    	 /* public void keyTyped(KeyEvent e) {
    	        //
    	    }
    	 
    	    public void keyPressed(KeyEvent e) {
    	        if (e.getKeyCode() == KeyEvent.VK_ENTER)
    	        {
    	        L.stage();
            	R.stage();
    	        System.currentTimeMillis();
    	        	
    	        }
    	       
    	    }
    	 
    	    public void keyReleased(KeyEvent e) {
    	        // 
    	    }*/
    }
    //use three timers one for stage and yellow lights, one for green and one for red. On green use key pressed to start 
    //System.currentTimeMillis(), figure out how to measure and display the reaction time
    //If VK_ENTER is pressed before the yellow clears, activate timer for Red light to appear
    
    
    ADT
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    
    
    
    public class drag implements ActionListener
    {
    	private JPanel panel;
    	private int x,y,q;
    	private Timer countdown, greenlight;
    	
    
    
    		public drag(JPanel panel2, int i, int o, int j, int i2)
    		{			
    			panel=panel2;
    			x=i;
    			y=i2;
    			q=0;//go counter
    			
    		
    			countdown= new Timer(j, this); //timer
    			greenlight= new Timer(o,this); //stage light timer
    			
    
    		}
    			
    		
    
    		public void tick() //timer to be used in main class
    		{	
    			countdown.start(); //start method
    			
    		}
    		public void stage()//main class starts timer 
    		{
    			greenlight.start();// start method
    			
    		}
    		
    		
    		
    		public void actionPerformed (ActionEvent event)
    		{
    		if(event.getSource()==countdown){
    			Graphics k = panel.getGraphics(); 
    			k.setColor(Color.yellow);
    			
    			// integer q as a counter with if statements drawing my lights
    			q=q+7;
    			
    			if(q>50)
    			{
    				
    				k.fillOval(345+y,120,12,12);
    			
    				k.fillOval(360+y,120,12,12);
    			
    			}
    			
    				
    			if(q>150)
    			{
    				k.fillOval(345+y,150,12,12);
    				k.fillOval(360+y,150,12,12);
    					
    			}
    			
    		  	if (q>450)
    		  	{
    			k.fillOval(349+x,170,25,25);
    		  	}
    		  	
    		  	if (q>550)
    		  	{
    		    k.fillOval(349+x,203,25,25);
    		  	}
    		  	
    		  	if (q>650)
    		  	{
    		    k.fillOval(349+x,234,25,25);
    		  	}
    		  	if(q>750)
    		  	{
    		  		k.setColor(Color.green);
    		  		k.fillOval(403,266,25,25);
    		    q=0; //resets counter to zero 
    			countdown.stop(); //stops the timer right after the last set of lights
    			
    			
    			
    			 		  	}
    		  	}
    		 
       
    		 if(event.getSource()==greenlight)
    	{
    		
    		Graphics k1 = panel.getGraphics(); 
    		
    		k1.setColor(Color.green);
    		
    		q=q+1;
    		
    		if(q>1)
    		{
    	    
    		k1.fillOval(349,266,25,25);
    		
    		q=0;
    		greenlight.stop();
    	    }
       
    	}
    		}
    		
    
    
    
    
    
    
    		
    		
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm sorry but I'm still not fully clear on what you are trying to achieve here, though this may be me. Any chance you could explain it again as if I were 12 yr old?

    Also, you would be wise to read the Sun tutorials on how to draw with Swing. It's not how your doing it in that you shouldn't be getting a Graphics object via getGraphics, but instead you should draw within the paintComponent method of a JComponent such as a JPanel. The tutorials will explain it to you.

Similar Threads

  1. Need Help With VERY Simple Program -- Newbie!!!
    By somethingfast in forum New To Java
    Replies: 17
    Last Post: 11-25-2008, 08:38 AM
  2. :) newbie...........
    By Somitesh Chakraborty in forum Introductions
    Replies: 1
    Last Post: 08-19-2008, 10:00 AM
  3. newbie newbie newbie
    By krislogy in forum New To Java
    Replies: 9
    Last Post: 08-15-2008, 01:28 AM
  4. newbie needs help...
    By vicky08 in forum New To Java
    Replies: 2
    Last Post: 03-31-2008, 05:26 PM
  5. Newbie
    By CSnoob87 in forum Introductions
    Replies: 2
    Last Post: 02-18-2008, 09:49 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
  •