PDA

View Full Version : Made an applet that runs - need HTML file..




mcmacmcmac
Nov 22, 2007, 05:56 PM
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!



rev316
Nov 22, 2007, 06:08 PM
Just use the HTML applet tags along with the compiled .class file?

mcmacmcmac
Nov 22, 2007, 06:11 PM
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

toddburch
Nov 22, 2007, 06:38 PM
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.


<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

mcmacmcmac
Nov 22, 2007, 08:02 PM
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>
<applet code = “GradeBookApplet.class” width = “300” height = “500”>
</applet>
</html>

mcmacmcmac
Nov 22, 2007, 08:05 PM
Here's the code if it matters at all..


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);
}
}

}

Aranince
Nov 23, 2007, 12:06 AM
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>
<applet code = “GradeBookApplet.class” width = “300” height = “500”>
</applet>
</html>

You need to have body tags


<html>

<head>
<title>This is my Java app</title
</head>

<body>

<applet code = “GradeBookApplet.class” width = “300” height = “500”>
</applet>

</body>
</html>