Download presentation
Presentation is loading. Please wait.
Published byEvan Martin Modified over 9 years ago
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 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 them 2.A Dimension object defines a width and height 3.setSize defines a size for the applet window 4.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 My Applet <applet code="MyPicture.class" width="800" height="800">
6
Applet parameters HTML: Add the following immediately after your applet tag, but before 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?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.