10
Java Unit 5: Applets and Graphics Putting Applets into Web Pages

Putting Applets into Web Pages. Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ The html

Embed Size (px)

Citation preview

Page 1: Putting Applets into Web Pages.  Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ The html

Java Unit 5: Applets and Graphics

Putting Applets into Web Pages

Page 2: Putting Applets into Web Pages.  Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ The html

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.

Classes and HTMLs.

Page 3: Putting Applets into Web Pages.  Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ The html

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

Finding the .class files

Page 4: Putting Applets into Web Pages.  Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ 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)

Creating the HTML

Page 5: Putting Applets into Web Pages.  Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ The html

<html><applet code=“MyJavaApplet.class” width=“500” height=“500”></applet></html>

HTML Code

Page 6: Putting Applets into Web Pages.  Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ The html

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

Page 7: Putting Applets into Web Pages.  Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ The html

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.

HTML Code

Page 8: Putting Applets into Web Pages.  Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ The html

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.

A couple of other notes

Page 9: Putting Applets into Web Pages.  Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ The html

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

Page 10: Putting Applets into Web Pages.  Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ The html

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.

Passing Security