Java Unit 5: Applets and Graphics Putting Applets into Web Pages
Classes and HTMLs. • Two things are involved in the process of putting applets onto web pages • The .class files of the applet • The html file of the page • Remember: any Java source file that compiles correctly will produce a .class file.
Finding the .class files • Compiled .class files for Java source code should be found: • C:/UserName/workspace/NameOfProject/bin • It may also be found in • C:/UserName/Android/workspace/NameOfProject/bin
Creating the HTML • The HTML file should be created inside the same folder as the .class files for the applet. • This is to reduce complexity in defining the path to the applet class in the HTML file. • You could start creating an HTML file in Notepad by: • Starting a new text file, and adding your code from there • When it comes time to save, remove the *.txt extension, and add ‘.html’ to the end of the file instead. (Ex: MyHtmlPage.html)
HTML Code <html><applet code=“MyJavaApplet.class” width=“500” height=“500”></applet></html>
HTML Code • HTML is a markup language, using a system of tags to define elements of a document. • <html> is a tag that denotes a start of an html file, while </html> denotes the end. • Likewise, <applet> and </applet> are used in the same way.
HTML Code • You’ll notice inside the first applet tag, there are attributes assigned inside it (code, width, height) • ‘code’ is the directory where the HTML file would be able to find the main .class entry point for the applet. • ‘width’ and ‘height’ define the size as it appears on the web page. • Note that the values assigned to these attributes are defined in double quotes.
A couple of other notes • If you have to change a line in the source code of the Java files and recompile, you should replace the .class file in the html directory with the new one. • When running Java applets, the user must have the Java Runtime Environment installed. • For security or other reasons, some systems may not run Java content through web pages.
Passing Security • With the newest version of Java, unsigned applets are not allowed to run on any machine. • This means that if we try running our applet through an html page we created, the applet will not run at all, because it is considered a security risk. • Therefore, we’ll need to create an exception for this.
Passing Security • Open the Java Control Panel • On Windows start, search for “Configure Java” • Java\jre7\bin\javacpl.exe • Go to the Security tab, and click the “Edit Site List…” button. • In the blank space on the dialog, type in the file or html location of the html file you want to run. • Ex: http://math.uaa.alaska.edu/~android/java/unit5/unit5applet • Click Add, then OK on both dialogs. You should be able to run that applet now.