Made an applet that runs - need HTML file..

Discussion in 'Mac Programming' started by mcmacmcmac, Nov 22, 2007.

  1. macrumors member

    Joined:
    Oct 2, 2007
    #1
    Hi very new to programming here.. would really appreciate help with this

    I just made an applet in eclipse on osx, when I run as Java Applet everything is perfect. But my professor is asking for the html file along with the java file, and I think i should be able to run the applet in a browser.

    How can I find/create the html file and how can i get the applet to run in the browser.. thanks!
     
  2. macrumors regular

    Joined:
    Nov 7, 2004
    #2
    Just use the HTML applet tags along with the compiled .class file?
     
  3. thread starter macrumors member

    Joined:
    Oct 2, 2007
    #3
    rrrrrrright of course lol .. i don't think i stressed enough my ineptitude with this stuff. Though that sounds awfully simple i have no idea how to do it
     
  4. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #4
    See this thread. http://forums.macrumors.com/showthread.php?t=363516

    I've since taken that off my website, but here is the HTML I used for that example. You can use the commented applet tag to see how to use your local file system.

    Code:
    <html>
    <head>
    <title>HangMan</title>
    </head>
    <body>
    <!--applet codebase="file:///Users/toddburch/Documents/myjava/" code="HangMan" width="400" height="400" -->
    <applet codebase="http://www.burchwoodusa.com/cgi-bin/" code="HangMan" width="400" height="400">
    </applet> 
    </body>
    </html> 
    
    Todd
     
  5. thread starter macrumors member

    Joined:
    Oct 2, 2007
    #5
    i created this and tried opening the file in browser to launch applet but no luck.. i feel like this is really simple i'm just completely lost

    HTML:
    <html>
    <applet code = “GradeBookApplet.class” width = “300” height = “500”>
    </applet>
    </html>
     
  6. thread starter macrumors member

    Joined:
    Oct 2, 2007
    #6
    Here's the code if it matters at all..


    Code:
    import java.awt.Graphics;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;
    
    public class GradeBookApplet extends JApplet
    	{
    	
    		// initialization phase
    		private int total = 0;
    		private int gradeCounter = 0;
    		private double classAverage;
    		private int gradeFrequency [] = new int [11];
    		
    		public void init()
    			{
    				String gradeString = JOptionPane.showInputDialog("Enter a grade or -1 to quit");
    				
    				// convert grade to integer
    				int grade = Integer.parseInt(gradeString);
    				
    				// loop until sentinel value read from user
    				while ( grade != -1)
    				{
    					total += grade;
    					++gradeCounter;
    					
    					if (grade < 10)
    						++gradeFrequency[0];
    					else if (grade < 20)
    						++gradeFrequency[1];
    					else if (grade < 30)
    						++gradeFrequency[2];
    					else if (grade < 40)
    						++gradeFrequency[3];
    					else if (grade < 50)
    						++gradeFrequency[4];
    					else if (grade < 60)
    						++gradeFrequency[5];
    					else if (grade < 70)
    						++gradeFrequency[6];
    					else if (grade < 80)
    						++gradeFrequency[7];
    					else if (grade < 90)
    						++gradeFrequency[8];
    					else if (grade < 100)
    						++gradeFrequency[9];
    					else if (grade == 100)
    						++gradeFrequency[10];
    					
    					gradeString = JOptionPane.showInputDialog("Enter a grade or -1 to quit");
    					
    					// convert grade to integer
    					grade = Integer.parseInt(gradeString);
    				
    				}
    				
    				
    			}
    				
    				
    		public void paint (Graphics g)
    			{
    				// call superclass version of method paint
    				super.paint(g);
    				
    				// draw a string
    				g.drawString("GRADES DISTRIBUTION FOR THE CLASS:",25,25);
    				
    				
    				// for each array element output bar for chart
    				for (int counter = 0; counter < gradeFrequency.length; counter++)
    					{
    						int a = counter * 10;
    						int b = counter * 10 +9;
    						int y;
    						
    						if (counter == 0)
    							y = 50;
    						else
    							y = 50 + (counter *25);
    						
    						
    						// output bar label
    						if (counter==10)
    							g.drawString("100:",50,y);
    						else
    							g.drawString(a + " - " + b + ": ",25,y);
    						
    						if (gradeFrequency[counter] != 0)
    							{
    								int x = 80;
    								
    								for(int stars = 0; stars < gradeFrequency[counter];stars++)
    									{
    										g.drawString("*", x, y);
    										x = x+5;
    									}
    									
    								
    							}
    						
    						
    					}
    				
    				if (gradeCounter != 0)
    					{
    						//calculate average of all grades entered
    						classAverage = total/gradeCounter;
    						
    						
    						g.drawString("Total: " + total,25,350);
    						g.drawString("Number of grades: " + gradeCounter, 25, 375);
    						g.drawString("Average: " + classAverage, 25, 400);
    					}
    			}
    			
    	}
     
  7. macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #7
    You need to have body tags

    Code:
    <html>
    
    <head>
       <title>This is my Java app</title
    </head>
    
    <body>
    
    <applet code = “GradeBookApplet.class” width = “300” height = “500”>
    </applet>
    
    </body>
    </html>
     

Share This Page