Thread: christmas class
View Single Post
  #1 (permalink)  
Old 12-15-2008, 08:24 PM
f_the_cook's Avatar
f_the_cook f_the_cook is offline
Member
 
Join Date: May 2008
Posts: 21
Rep Power: 0
f_the_cook is on a distinguished road
Default christmas class
hello yet again i need some help with my code if you would. what im trying to do is make the 12 days of Christmas in an applet while playing the music. the problem im having is i need it when i run it to have it say on the first day etc. and then make a new line and start with on the second day. ill show you the code i have so far.

Code:
import java.awt.*;
import javax.swing.*;
public class Christmas 
{

    public static String ChristmasRecursion(int day,int tempDay,int lastDay) 
    {
    	int giftDay=tempDay;
    	int secondDay=day;
    	String result="";
    	String[] dayLyrics = new String[]    	
    		{"first","second","third","fourth","fifth","sixth","seventh","eigth","nineth","tenth","eleventh","twelfth"};
    		
    		
    		String [] gifts=new String[]
    		{"","a partirige in a pear tree!\n\n","turtle doves\n", "french hens\n" , "calling birds\n", "golden rings\n" ,"geese a-laying\n","swans a-swimming\n"
    			,"maids a-milking\n","ladies dancing\n","lords a-leaping\n","pipers piping\n","drummers drumming\n" };
    		
    		
    	String myTrue="my true love gave to me\n ";
    	
    	if ((day==11)|| (day==lastDay))
    	{
    		result+=( "On the "+dayLyrics[day]+" day of christmas "+myTrue+ "\n");
    			//gifts[day]);
    		
    		while (tempDay!=0)
    		{
    			if ((tempDay==1)&&(secondDay!=0))
    			{
    				result+="And ";
    			}
    			if (day!=0)
    			{
    				result+=((day+1)+ " ");
    			}
    			result+=gifts[tempDay]+"\n";
    			tempDay--;
    			day--;
    			
    		}
    		tempDay=giftDay;
    		day=secondDay;
    		result+="\n\n\n\n\n\n\n";
    	}
    	else
    	{
    		result+=( "\n\n\nOn the "+dayLyrics[day]+" day of christmas "+myTrue+ "\n");
    		
    		while (tempDay!=0)
    		{
    			if ((tempDay==1)&&(secondDay!=0))
    			{
    				result+="And ";
    			}
    			if(day!=0)
    			{
    				result+=((day+1)+" ");
    			}
    			result+=gifts[tempDay]+"\n";
    			tempDay--;
    			day--;
    		}
    		tempDay=giftDay;
    		day=secondDay;
    		
    		
    		result+=ChristmasRecursion((day+1),(tempDay+1),lastDay);
    	}	
    			
    	return result;	
    	
    }
    
    
    
}

thats the first part here is the second.

Code:
import java.awt.*;
import javax.swing.*;
import java.applet.AudioClip;
import java.applet.*;
import java.io.*;
import sun.audio.*;
import java.util.StringTokenizer;
import java.net.*;

public class fdoce extends Applet 
{
	static AudioClip sound;
	static ImageIcon christmasBackground;
	static ImageIcon ChristmasBackgroundCutout;
	public String _sound;
	public String CB;
	public String CBC;

    public fdoce() 
    {  
    	      GraphicsEnvironment env =  GraphicsEnvironment.getLocalGraphicsEnvironment();
    		env.getAvailableFontFamilyNames();
    		setFont (new Font("Agency", Font.PLAIN, 12));
    }
    synchronized public void intit()
    {
    	_sound=((_sound=getParameter("sound"))!=null) ? _sound: "";
    	sound=getAudioClip(getCodeBase(), _sound);
    	sound.loop();
    }
    public void paint(Graphics g)
    {
    	g.setColor(Color.WHITE);
    	int length;
    	int chosenDay=0;
    	int tempDay=1;
    	int height=50;
    	int width=50;
    	int numLines=0;
    	boolean commas=true;
    	boolean drumming=false;
    	String title=("12 days of XMAS!\n\n");
    	String lyrics=(Christmas.ChristmasRecursion(0,1,11));
    	StringTokenizer st = new StringTokenizer(lyrics);
    	
    	CB=((_sound=getParameter("image"))!=null)? CB :"christmasBackground.jpg";
    //	CBC=((_sound=getParameter("imageCutout"))!=null)? CBC:"christmasBackgroundCutOut.jpg";
    	christmasBackground=new ImageIcon(CB);
    //	ChristmasBackgroundCutout=new ImageIcon(CBC);
    	
    	g.drawImage(christmasBackground.getImage(),0,0,null);
    	g.drawString(title,10,15);
    	while (st.hasMoreTokens())
    	{
    		String w = st.nextToken();
    		length=w.length();
    		
    		if (w.equalsIgnoreCase("drumming"))
    		{
    			width+=5;
    			drumming=true;
    		}
    		if(drumming==true&& w.equalsIgnoreCase(","))
    		{
    			width+=15;
    			drumming=false;
    		}
    		g.setFont(new Font("Agency", Font.PLAIN, 12));
    			g.drawString(w,width,height);
    			g.setColor(Color.WHITE);
    //			g.drawImage(ChristmasBackgroundCutout.getImage(),725,500,null);
    			g.setColor(Color.WHITE);
    			g.setFont(new Font("Agency", Font.PLAIN, 12));
    			if(w.equalsIgnoreCase(":")||w.equalsIgnoreCase(",")||w.equalsIgnoreCase("!"))
    			{
    				commas=true;
    			}
    			if(commas==false)
    			{
    				g.drawString(w,725,550);
    			}
    			width+=((int)length*5.5+20);
    			if(w.equalsIgnoreCase(":")||w.equalsIgnoreCase(",")||w.equalsIgnoreCase("!"))
    			{
    				numLines++;
    				width=50;
    				height+=15;
    				if(numLines>=37)
    				{
    					width=330;
    				}
    				if(numLines>=74)
    				{
    					width=630;
    				}
    				if(w.equalsIgnoreCase("!"))
    				{
    					g.drawLine(width,height-13,width+275,height-13);
    				}
    			}
    			if(numLines==37||numLines==74)
    			{
    				height=50;
    			}
    			try
				{
					Thread.sleep(400);
				}
				catch(InterruptedException e)
				{
					
				}
				commas=false;	
    			
    			
    	}
    }
    
    
}
your help is greatly appreciated.


thank you.
Reply With Quote