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

    Lightbulb How can I get this to work properly -animation issue with an applet.

    I need someone to tell me what to write in the while(true) statement in order to have this animation working?

    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) {
    	while(true){
    		 repaint();
    		try {Thread.sleep(1000);}	
    		catch(InterruptedException e) {}
    	}
     	}		
        }
    
         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);
         }
    }


    information from the HTML file
    XML Code:
    <HTML>
    <HEAD>
    <TITLE>Remaining time to 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
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Nothing. Use a SwingTimer to control animations. The setup can be done in init(), rather than making it Runnable. You can parse a whole date at once using DateFormat.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

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

    Default

    but how to do that? I´m very new to Java and this is actually my first applet by myself. Thanks

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

Similar Threads

  1. Animation Applet
    By Unome in forum Java Applets
    Replies: 7
    Last Post: 10-17-2008, 07:34 AM
  2. Eclipse Documentation does not work properly.
    By krawetko in forum Eclipse
    Replies: 0
    Last Post: 10-05-2008, 10:06 AM
  3. Applet Vs JApplet ?? which is better for animation?
    By Mba7eth in forum New To Java
    Replies: 3
    Last Post: 09-09-2008, 04:55 PM
  4. Will this applet ever work?
    By willemjav in forum Java Applets
    Replies: 4
    Last Post: 04-20-2008, 05:40 PM
  5. why doesn't this short applet work?
    By kim85 in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 08:43 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
  •