Results 1 to 3 of 3
  1. #1
    AlejandroPe's Avatar
    AlejandroPe is offline Member
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Lightbulb [SOLVED] Cómo lograr la animacion de este Applet???

    Hello!! I´ve done this code for a countdown to the year 2010(my first applet by myself indeed :) ), the applet itself provides the information I really want but I'm missing something, the animation doesn´t work. I guess :confused: it has to do with the while(true) statement AND just before repaint(), I just don't know what action should my applet have in that while statement. Thanks for your help. Here's the code.

    Java Code:
    import java.awt.*;
    import java.util.*;
    
    public class RemainingTime extends java.applet.Applet implements Runnable {
    	Font f = new Font("TimesNewRoman", Font.BOLD,21);
    	Date datum;
    	int theDate;
    	int year;
    	int month;	
    	int day;
    	String dd;
    	String hr;  
    	Thread runner;
    	int theHours;
    	int theMinutes;	
    	int theSeconds;
    	public int subTotal;
    	int remainingDays;
           public void start() {
    	if (runner == null); {
    		runner = new Thread(this);
    		runner.start();
    		}
    	} 	  
    
       public void run() {
    	dd = getParameter("datum");	//får värdet i "datum" från htlm filen 
    	StringTokenizer tokens1, tokens2;
    	tokens1 = new StringTokenizer(dd, "/");	//Tokenizing stringen med / som avgränsare. 
    	year = Integer.parseInt(tokens1.nextToken());  //konvert varje token till integer. 
    	month = Integer.parseInt(tokens1.nextToken());
    	day = Integer.parseInt(tokens1.nextToken());  
    	
    	hr = getParameter("time");	//får ett värde (i "time") från htlm filen   
    	tokens2 = new StringTokenizer(hr, ":");
    	int hours = Integer.parseInt(tokens2.nextToken());
    	int minutes = Integer.parseInt(tokens2.nextToken());
    	int seconds = Integer.parseInt(tokens2.nextToken()); 	
    	
    	GregorianCalendar now = new GregorianCalendar();
    	int thisMonth = now.get(now.MONTH);
    	int thisDay = now.get(now.DAY_OF_MONTH);
    	int countDays = 0;
    	
    	for (int i = thisMonth+1; i <= 12; i++) { 
    	subTotal += countDays;      	/////////////////////////////////////////////////////////
    	      switch (month){
    	
    	 	case 1:		//jan
    		case 3:		//mars	
    		case 5:		//maj
    		case 7:		//juli
    		case 8:		//aug
    		case 10:		//okt
    		case 12:		//dec
    			countDays = 31;
    			break;
    		case 2: 	// feb 2009
    			countDays = 28;
    			break;
    		case 4:	//apr	
    		case 6:	//juni		
    		case 9:	//sept
    		case 11:	//nov
    			countDays = 30;
    			break;
    	   }
    	}
    	remainingDays = subTotal + thisDay;
    	int thisHour = now.get(now.HOUR);	
    	int thisMinute = now.get(now.MINUTE);
    	int thisSecond = now.get(now.SECOND);
    	int timeInSec = (thisHour*60*60) + (thisMinute*60) + thisSecond;
    	int finish = (hours*60*60) + (minutes*60) + seconds;
    	int totalRemSec;
    	if (timeInSec > finish) 
    		totalRemSec = 86400 - timeInSec + finish;
    	else
    		totalRemSec = finish - timeInSec;
     
    	int totalMin = totalRemSec / 60;
    	theHours = totalMin / 60;
    	theMinutes = totalMin % 60;
    	theSeconds = totalRemSec % 60;
    
    	Thread thisThread = Thread.currentThread();
    	while (runner == thisThread) {[COLOR="Red"]
    	while(true){
    		 repaint();
    		try {Thread.sleep(1000);}	
    		catch(InterruptedException e) {}[/COLOR]
    	}
     	}		
        }
    
         public void paint(Graphics s) {
    	
    	
    	s.setFont(f);
    	s.drawString("Remaining time to year 2010:", 5, 25);
    	s.drawString((String.valueOf(remainingDays)), 5, 50); 
    	s.drawString("days", 50, 50);
    	s.drawString((String.valueOf(theHours)), 5, 75);
    	s.drawString("hours", 50, 75);
    	s.drawString((String.valueOf(theMinutes)), 5, 100);
    	s.drawString("minutes", 50, 100);
    	s.drawString((String.valueOf(theSeconds)), 5, 125);
    	s.drawString("seconds", 50, 125);
         }
    }
    and the imput from the HTML

    XML Code:
    <HTML>
    <HEAD>
    <TITLE>Remaining time till year 2010
    </TITLE>
    </HEAD>
    <BODY>
    <APPLET CODE = "RemainingTime.class" WIDTH=400 HEIGHT=300>
    <PARAM NAME= datum VALUE="2009/12/31">
    <PARAM NAME= time VALUE="24:00:00"> 
    </APPLET>
    </BODY>
    </HTML>

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    repaint() calls the update method, as far as I know. However, I would suggest that you use a label of some form, and add setText(timeString) before the repaint(). I'm not sure what type of label to use, as Applets are not my strong point.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    AlejandroPe's Avatar
    AlejandroPe is offline Member
    Join Date
    Mar 2009
    Posts
    37
    Rep Power
    0

    Default Any other suggestions?

    :o I dont know how to use labels, im very new to Java prog.
    Can anyone tell me if there is another way to fix this or maybe how to use labels??

Similar Threads

  1. Calling another applet on click of button in one applet
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 1
    Last Post: 02-19-2009, 12:54 PM
  2. BFS using APPLET
    By bharanidharanit in forum Java Applets
    Replies: 3
    Last Post: 12-30-2008, 02:45 AM
  3. Applet
    By sonu in forum Java Applets
    Replies: 1
    Last Post: 11-04-2008, 10:31 AM
  4. Need with an applet
    By maggie_2 in forum Java Applets
    Replies: 6
    Last Post: 09-21-2008, 08:07 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 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
  •