1 / 7

Applets

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

debra
Download Presentation

Applets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 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.

  2. 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

  3. 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 drawing graphics.setColor(c); • Fill a rectangle using the current color Graphics.fillRect(0,0,800,800);

  4. A Rectangle applet We override JApplet init and paint methods, this is polymorphism 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); } } Import gets Java features that can be used if we ask for them A Dimension object defines a width and height setSize defines a size for the applet window extends is a reserved word for inheriting from the Applet class

  5. 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>

  6. 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 HTML String 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);

  7. 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?

More Related