Can Anyone Help? (Java Assignment)......

Discussion in 'Computer Science & Culture' started by TruthSeeker, Oct 16, 2004.

Thread Status:
Not open for further replies.
  1. TruthSeeker Fancy Virtual Reality Monkey Valued Senior Member

    Messages:
    15,162
    I don't get this. It was working just fine, but now it completely stopped! The problem is that the program is not listening to the events anymore, and I have no idea why. Is there something wrong with the actionPerformed and adjustmentValueChanged methods? Please help....

    Please Register or Log in to view the hidden image!




    /*
    import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;


    public class Lab4 extends Applet implements ActionListener, AdjustmentListener{

    //Global Declarations
    TextField name, last, time;
    String firstName, lastName;
    Scrollbar slider;
    int secsin, sliderValue;
    double f, celcius;


    //Initializing Input
    public void init(){
    Label names= new Label ("Name: ");
    add (names);
    name= new TextField(" ",15);
    add(name);
    name.addActionListener(this);

    last= new TextField(" ",15);
    add(last);
    last.addActionListener(this);

    time= new TextField(" ",15);
    add(time);
    time.addActionListener(this);

    slider= new Scrollbar(Scrollbar.VERTICAL, 0,1,0,100);
    add(slider);
    slider.addAdjustmentListener(this);


    }

    //Repainting Output
    public void actionPerformed(ActionEvent e){
    firstName= name.getText();
    lastName= last.getText();
    secsin= Integer.parseInt(time.getText());

    repaint();
    }


    public void adjustmentValueChanged(AdjustmentEvent e){ sliderValue= slider.getValue();
    repaint();

    }


    //Paint Method
    public void paint (Graphics g){
    //g.drawString("Name: "+firstName+lastName, 100,100);

    calculation (g, 300, 100, secsin);

    g.drawString("Temperature: "+sliderValue+ " celcius", 100,325);
    g.drawString("Temperature: "+f+" fahrenheith", 100,300);

    g.setColor(Color.red);
    drawTermometer(g, 100,500);

    g.setColor(Color.black);
    convertToFahrenheit(sliderValue);

    colorInformation(g, 100,100,Color.blue);

    }


    //Private Methods
    private double convertToFahrenheit(double celsius){
    double f= (celcius*9/5) + 32;
    return f;
    }


    private void colorInformation (Graphics g, int x, int y, Color c){

    g.setColor(c);
    g.drawString(firstName+" "+lastName, x,y);
    g.drawString("Comp132",x,y+15);
    g.drawString("Section 1",x,y+30);

    }



    private void calculation (Graphics g, int x, int y, int sec){

    int seconds, //Converting time
    hours,
    remainder,
    minutes;

    final int CONV_TO_HR=3600,
    CONV_TO_MIN=60,
    CONV_TO_SEC=60;

    seconds = sec;
    hours = seconds/CONV_TO_HR;
    remainder = seconds%CONV_TO_HR;
    minutes = remainder/CONV_TO_MIN;
    remainder = remainder%CONV_TO_MIN;
    seconds = remainder%CONV_TO_SEC;

    g.drawString("Hours: "+ hours+" hours",x,y);
    g.drawString("Minutes: "+minutes+" min",x,y+25);
    g.drawString("Seconds: "+seconds+" sec",x,y+50);
    }

    private void drawTermometer(Graphics g, int x, int y){

    g.drawRect(x,y,30,100);
    g.fillOval(x-10,y+85,50,50);
    g.fillRect(x,y+100-sliderValue,30,sliderValue);
    g.drawString("100 Celcius",x+35,y);
    g.drawString("50 Celcius", x+35,y+50);
    g.drawString("0 Celcius", x+40,y+100);

    }

    }
    */

    PS: Comments (/*) added just in case java works here......
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Stryder Keeper of "good" ideas. Valued Senior Member

    Messages:
    13,105
    Usually things that occur during programming that cause programs to stop are:
    Not placing a ; at the end of a line if your interpreter uses them.
    Not closing the brackets { } around a Function.

    Then of course the less obvious like data not being entered into a variable before the program attempts to use the variable to extract the current data etc.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. TruthSeeker Fancy Virtual Reality Monkey Valued Senior Member

    Messages:
    15,162
    I checked. It's all fine. Besides, if it wasn't, the compiler would give me error messages......

    All variables are being assigned....


    Have you checked the code? I posted it right above. I cannot find anything wrong with it....

    Please Register or Log in to view the hidden image!

     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. TruthSeeker Fancy Virtual Reality Monkey Valued Senior Member

    Messages:
    15,162
    Huuuuuuuummm... my applet viewer give me the following message...:
    "Warning: Can't read AppletViewer properties file: C:\Documents and Settings\C0______\.hotjava\properties Using defaults"

    Is that a problem......?
     
  8. Voodoo Child Registered Senior Member

    Messages:
    1,296
    The program appears to be correct which suggests it's a problem with applet viewer. I ran it fine in Jbuilder. Perhaps you should display it in an HTML file, since the browser plug-in isn't likely to have the same problem.

    BTW, parseInt will throw an Exception if you type in a non-numeric number.

    you should catch this with something like:

    try{
    secsin= Integer.parseInt(time.getText());
    }

    catch(NumberFormatException ex)
    {
    JOptionPane.showMessageDialog(null, "A number, retard")
    }

    Good luck.
     
  9. mercurio 9th dan seppuku sensei Registered Senior Member

    Messages:
    325
    I think its because you named the applet Applet.

    Reserved words and all that jazz.
     
  10. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    AHA! After a bit of frustration I found where the problem is...

    THE DAMN SPACES!!!

    ....Yeah, apparently java doesn't like parsing a space to an int. If you get rid of the spaces in the TextField's it should work fine.
     
  11. TruthSeeker Fancy Virtual Reality Monkey Valued Senior Member

    Messages:
    15,162
    I think you are absolutely right. When I wrote it, it was running fine. It was just when I came back to it the next day that it screwed up. The applet must be broken or something.....

    And about the "try" and "catch"... I'm not quite there yet.... I'm just a beginer, ya know?

    Please Register or Log in to view the hidden image!



    Thanks, btw

    Please Register or Log in to view the hidden image!

     
  12. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    tsk tsk tsk...mutiple posts...*glares*
    It worked fine for me without the try catch if you didn't include the space in the time string. Maybe that was just my computer working perferctly as it usually does...you have to keep them in line and show them who's boss every once in a while, you know?

    HAHAHAHA!

    Please Register or Log in to view the hidden image!

     
Thread Status:
Not open for further replies.

Share This Page