Applets An applet is similar to a Java program, but it runs within a Web-browser on a client...

7
Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client system in a Java applet The browser gives the applet a piece of the browser window, and Java controls that region The structure of an applet is different than an application There is an init method instead of a main() method An applet inherits (extends) a Applet class. This means all the methods in the applet class are available to the applet you create Some methods that are available include init(), paint(), update(), setSize(), and others.

Transcript of Applets An applet is similar to a Java program, but it runs within a Web-browser on a client...

Page 1: Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.

Applets• An applet is similar to a Java program, but it runs within a

Web-browser on a client machine.

• For security, you cannot change anything on a client system in a Java applet

• The browser gives the applet a piece of the browser window, and Java controls that region

• The structure of an applet is different than an application– There is an init method instead of a main() method– An applet inherits (extends) a Applet class. This means all the

methods in the applet class are available to the applet you create

– Some methods that are available include init(), paint(), update(), setSize(), and others.

Page 2: Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.

Graphics class object

• Methods– g.drawArc(x, y, width, height, startAngle, endAngle);– g.drawLine(x1, y, x2, y2);– g.drawOval(x, y, width, height);– g.drawRect(x, y, width, height);– g.fillArc(x, y, width, height);– g.fillOval(x, y, width, height);– g.setColor(color);

• Notes– x, x1, x2 = distance from the left, y, y1, y2 = distance from the top– width = how wide to draw, height = how high to draw– color = the color to use for drawing

Page 3: Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.

The Color Class• Instantiate a color

String s = “ 0000ff”; int i = Integer.parseInt( s.trim(), 16);Color c = new Color(i);

• Set the current color for drawinggraphics.setColor(c);

• Fill a rectangle using the current colorGraphics.fillRect(0,0,800,800);

Page 4: Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.

A Rectangle applet

import java.awt.*;import javax.swing.*;

public class Picture extends JApplet{ public void init() { setSize(new Dimension(800,800)); }

public void paint(Graphics g) { g.setColor(new Color(256*255));g.fillRect(0,0,799,799);g.setColor(new Color(0));g.fillRect(50, 100, 100, 200); }

}

We override JApplet init and paint methods, this is polymorphism

1. Import gets Java features that can be used if we ask for them2. A Dimension object defines a width and height3. setSize defines a size for the applet window4. extends is a reserved word for inheriting from the Applet class

Page 5: Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.

Testing and running an Applet

• In Jgrasp, simply click on the picture of the apple

• Create a web-page with the applet<html><head><title>My Applet</title></head><body><applet code="MyPicture.class"

width="800" height="800"></applet></body></html>

Page 6: Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.

Applet parameters• HTML: Add the following immediately after your applet tag, but

before </applet> <param name=“background” value=“00ff00” /><param name=“foreground” value= “ff0000” />

• The applet: Retrieve parameters from the HTMLString background = getParam(“background”);String foreground = getParam(“foreground”);

• Your init() method (Formatting in your applet to use colors)int i = Integer.parseInt( background.trim(), 16);Color c = new Color(i);

• Your paint(Graphics g) method: setColor(c);g.fillRect(0, 0, 800, 800);

Page 7: Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.

Review• What is an applet?• What security concerns relate to an applet?• What is inheritance?• What is polymorphism?• How do you test your applet in Jgrasp?• How do you use html parameters in an applet?• What is a parameter name?• What does the init() method do?• What does the paint() method do?• How are colors represented in the computer?• What is the Graphics class? How do you use it in an applet?