1 / 24

Applets

Applets. Java API. Applet Examples. Java Programs: Application programs Applet programs Applets are designed to run on Web pages. Here are a couple of applet, from JSDK examples. (These can be found in j2sdk1.4/demo/applets.) Clock Molecule 1 Molecule 2 Molecule 3 Tic-Tac-Toe.

mattox mattox
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 Java API

  2. Applet Examples • Java Programs: • Application programs • Applet programs • Applets are designed to run on Web pages. Here are a couple of applet, from JSDK examples. (These can be found in j2sdk1.4/demo/applets.) • Clock • Molecule 1 • Molecule 2 • Molecule 3 • Tic-Tac-Toe

  3. Applet Restrictions • Applets have restrictions for security reasons. • Cannot read or write files on the user's computer, to prevent damaging files and spreading viruses. • Cannot run any program on the user's system. Otherwise, the local system could be destroyed or modified. • Cannot connect the user's computer to any other computer, except to the server where the applets are hosted. Without this restriction, an applet may connect the user's computer to another computer without the user's knowledge.

  4. <applet> Tag in HTML Page <html> <head> <title>Applet Demo</title> </head> <body><h1>An Applet Demo</h1> <applet code="Sample.class“ codebase=“code/applets” width="200" height="200> </applet> </body> </html>

  5. Applet Class • You create an applet by extending java.applet.Applet class. Applet has no main(), since the program is executed automatically by the browser. • Applet contains several methods, which can be overridden by the programmer. • public void init() • public void stop() • public void destroy() • public void paint()

  6. Applet Class (cont.) • public void init() -- called when the applet is loaded, as the enclosing Web page is opened. This method might include creating objects that the applet needs--e.g., creating an interface, setting fonts and loading images, and reading applet parameters from the web page. Like a constructor in application. • public void stop()--called when the user leaves the page containing the applet. The method may be overridden by a code, for example, that would stop any processing to conserve resources. In such a case, a corresponding code must be included in the start() method to restart the processing.

  7. Applet Class (cont.) • public void destroy()--called when the browser exits normally. The stop() method is always called before the this method in called. • public void paint(Graphcs g)--called whenever the applet displays something on the screen, such as text, line, color, or image. The method is called automatically whenever the window containing an applet is redrawn, for example, after visiting a different page. • Ordinarily, the methods init() and the paint() are overridden. Others can be left alone, in which case, nothing specific will be done when they are invoked by the browser.

  8. Example: Applet Welcome • Image of an applet interface

  9. Welcome.java Source Code import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; public class Welcome extends Applet { String msg = "Welcome to the world “ + “of Applets."; public void paint(Graphics g) { setBackground(Color.cyan); g.drawOval(5, 50, 200, 40); g.drawString(msg, 15, 75); g.drawLine(5, 100, 205, 100); }}

  10. Welcome.java Source Code • Note: • Only paint(Graphics g) method needs to be over-ridden. • Graphics g is the handle to the graphics environment for drawing figures and texts in the applet. • setBackground(Color.cyan) sets background color of the applet—think of it as a frame

  11. Welcome.html <html> <head> <title>Welcome</title> </head> <body> <h1>Welcome Message</h1> <hr> <applet code="Welcome.class“ codebase=“subfolder/” //optional width=220 height=120> </applet> </body> </html> View Applet

  12. Interactive Applet Welcome2 • Applet Welcome2 prompts the user to click a button, causing an input panel to pop up and to display a welcome message to a text field. • It incorporates event detection and event handler in the same way that you have already seen with application programs. • Run Welcome2 • Code Welcome2.java

  13. Welcome2 (cont.) • In the source code, note: • Interface has three objects, but on btnStart is associated with an event. • In init() method: • Objects are created • They are arranged in a panel • Button is associated with event listener • Panel is inserted into the applet • In actionPerformed() method: • yourName = JOptionPane.showInputDialog pops

  14. In Welcome example the background color (cyan) was hard-coded in the applet code. It would be more convenient if the color can be changed during its use in the Web page. The color information can be received by the applet as a parameter from the Web page. Applet with Parameters

  15. Applet with Parameters (cont.) <html> <head> <title>Welcome</title> </head> <body> <h1>Applet with Parameter</h1> <applet code="Welcome3.class" width=300 height=100> <param name="bgcolor" value="green"> </applet></body> </html>

  16. Applet with Parameters (cont.) • Using the same Welcome3 applet: • Web Page 1 • Web Page 2 • Web page 3 • Welcome3.java Source Code

  17. Applet with Paramers (cont.) • Note: • String color = getParameter("bgcolor");captures the value of parameter “bgcolor” from the HTML page. • if (color.equals("green")) setBackground(Color.green); else if . . .Not color == “green”, because String variable is a reference.

  18. Applet to Display Graph • Next example uses an applet (Bargraph) to display a bar graph. • Values of bar heights are captured from the Web page.

  19. Applet to Display a Graph (cont.) <html> <head> <title>Graph Test</title></head><body> <applet code="BarGraph.class" width=300 height=200> <param name="title" value="Quiz Scores"> <param name="num0" value="85"> <param name="num1" value="90"> <param name="num2" value="73"> <param name="num3" value="100"> <param name="num4" value="82"> <param name="num5" value="68"> </applet> </body> </html>

  20. Applet to Display a Graph (cont.) • barGraph.HTML • BarGraph.java Source Code • BAR_WIDTH, MAX_HEIGHT, & GAP are constants that specify the bars and the spacing between them. • title = getParameter("title");captures the value of the “title” parameter. • value = getParameter("num" + count);In the HTML page, parameters are named “num0”, “num1”, etc., so that the applet code can use a loop to form those names.

  21. Applet to Display a Graph (cont.) • Values of parameters are stored in array numList[]. • In the paint() method, • g.setColor(Color.BLUE); Once a color is set, it remains the same for subsequent drawings until it is changed. • Both Color.BLUE & Color.blue are OK. • fillRectangle() uses the values in numList[] to draw rectangles.

  22. With Different Parameters <html> <head> <title>Graph Test</title></head><body> <applet code="BarGraph.class" width=300 height=200> <param name="title" value="Quiz Scores"> <param name="num0" value=“20"> <param name="num1" value=“40"> <param name="num2" value=“60"> <param name="num3" value=“80"> <param name="num4" value=“100"> <param name="num5" value=“120"> </applet> </body> </html> Run barGraph2.html

  23. Converting Application to Applet • To show the relationship between an application and an applet, the next example shows how to convert a previous application—ConverDistance.java—to an applet. • ApplicationConvertDistance.javaSource Code

  24. Converting Application to Applet • Extend Applet, instead of Frameclass DistanceConverter extends Applet implements ActionListener {instead ofclass DistanceConverter extends JFrame implements ActionListener { • Applet has no constructor. Method init() takes its place. • Most of code in constructor is appropriate in init(), exept for super(), setSize(),setDefaultCloseOperation() , and setVisitble(true) . • Applet ConvertDistance.java Source Code

More Related

代做工资流水公司佛山签证工资流水多少钱中山入职银行流水费用苏州代做银行流水单深圳查企业对私流水湛江日常消费流水制作珠海银行流水账多少钱合肥查收入证明舟山企业贷流水制作青岛入职银行流水公司个人银行流水办理金华签证银行流水 多少钱扬州开银行流水账单荆州工资流水账单代开盐城企业流水打印图片青岛车贷流水样本郑州薪资流水查询汕头代做个人银行流水宜昌银行流水账代做临沂入职流水开具南京签证流水打印温州对公账户流水代做南京车贷工资流水 图片曲靖银行流水修改模板南阳工资流水费用揭阳代办贷款工资流水肇庆代开在职证明宁波车贷工资流水 打印上海打车贷银行流水离职证明图片重庆企业对公流水样本香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声卫健委通报少年有偿捐血浆16次猝死汪小菲曝离婚始末何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言男子被猫抓伤后确诊“猫抓病”周杰伦一审败诉网易中国拥有亿元资产的家庭达13.3万户315晚会后胖东来又人满为患了高校汽车撞人致3死16伤 司机系学生张家界的山上“长”满了韩国人?张立群任西安交通大学校长手机成瘾是影响睡眠质量重要因素网友洛杉矶偶遇贾玲“重生之我在北大当嫡校长”单亲妈妈陷入热恋 14岁儿子报警倪萍分享减重40斤方法杨倩无缘巴黎奥运考生莫言也上北大硕士复试名单了许家印被限制高消费奥巴马现身唐宁街 黑色着装引猜测专访95后高颜值猪保姆男孩8年未见母亲被告知被遗忘七年后宇文玥被薅头发捞上岸郑州一火锅店爆改成麻辣烫店西双版纳热带植物园回应蜉蝣大爆发沉迷短剧的人就像掉进了杀猪盘当地回应沈阳致3死车祸车主疑毒驾开除党籍5年后 原水城县长再被查凯特王妃现身!外出购物视频曝光初中生遭15人围殴自卫刺伤3人判无罪事业单位女子向同事水杯投不明物质男子被流浪猫绊倒 投喂者赔24万外国人感慨凌晨的中国很安全路边卖淀粉肠阿姨主动出示声明书胖东来员工每周单休无小长假王树国卸任西安交大校长 师生送别小米汽车超级工厂正式揭幕黑马情侣提车了妈妈回应孩子在校撞护栏坠楼校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变老人退休金被冒领16年 金额超20万西藏招商引资投资者子女可当地高考特朗普无法缴纳4.54亿美元罚金浙江一高校内汽车冲撞行人 多人受伤

代做工资流水公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化