Results 1 to 5 of 5
  1. #1
    roco10 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Need help with program

    I'm trying to make an simple applet that changes background according to the temperature that someone inputs. But for some reason, whenever I try to compile it, it is unable to locate the variable c, I've been stumped for a couple days

    Just trying to get the background to be blue when c <= 0, orange when c > 0 && c <100, and red when c >=100




    Java Code:
    /*
    <applet code="TemperatureConversion.class" width="200" height= "200">
    
    </applet>
    
    */
    
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class TemperatureConversion extends Applet implements ActionListener{
    
    	Label fl, cl;
    	TextField ft, ct;
    
    	public void init(){ 
    	
    		setLayout(new GridLayout(2,2));
    
    		fl = new Label("Farenheit");
    		cl = new Label("Celsius");
    		ft = new TextField(" ");
    		ct = new TextField(" ");
    
    		add(fl);
    		add(cl);
    		add(ft);
    		add(ct);
    
    		ft.addActionListener(this);
    		ct.addActionListener(this);
    		
    
    	}
    	public void actionPerformed(ActionEvent ae){
    
    
    	TextField temptext = (TextField)ae.getSource();
    	
    
    		if(temptext == ft){
    
    			String temp = ft.getText().trim();
    
    			int f = Integer.parseInt(temp);
    
    			int c =(int)((f-32)*(5.0/9));
    
    			ct.setText(" " + c);
    				
    		}else{
    
    			String temp = ct.getText().trim();
    
    			int f = Integer.parseInt(temp);
    
    			int c = (int)((f*(9.0/5)+32));
    
    			ft.setText(" " + c);
    	
    		} if(c <=  0){
    			r = 0;                               
    			g = 0;                                         
    			b = 255;
    		}else if((c  >  0) && ( c  < 100)){
    			r = 255;                       
    			g = 165;
    			b = 0;
    		}else{
    			r = 255;                           
    			g = 0;
    			b = 0;
    		}
    		setBackgroundColor(r,g,b);
    	}
    }
    Last edited by JosAH; 03-01-2012 at 06:14 AM. Reason: added [code] ... [/code] tags and removed [color] tags

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

    Default Re: Need help with program

    Your post is extremely hard to read. I suggest that you get rid of all fancy and unreadable colors and instead post your code using [code] [/code] tags with the [code] tag going above the code block and the [/code] tag going below.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help with program

    Those variable f and c are local to the if blocks and don't exist outside of them. Move the definition to the top of the method body so they will be visible in the entire body.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help with program

    Quote Originally Posted by Fubarable View Post
    Your post is extremely hard to read. I suggest that you get rid of all fancy and unreadable colors and instead post your code using [code] [/code] tags with the [code] tag going above the code block and the [/code] tag going below.
    I fixed that for the poster.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    roco10 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Need help with program

    Quote Originally Posted by JosAH View Post
    I fixed that for the poster.

    kind regards,

    Jos
    Thanks for doing that!!

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 01:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 07:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

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