1 / 23

Applets – Lecture 1, 2 & 3

Applets – Lecture 1, 2 & 3. Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI. What is an applet?. Applet : a Java program that can be inserted into a web page and run by loading that page in a browser

lara-kemp lara-kemp
Download Presentation

Applets – Lecture 1, 2 & 3

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 – Lecture 1, 2 & 3 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI

  2. What is an applet? • Applet: a Java program that can be inserted into a web page and run by loading that page in a browser • brings web pages to life with interactive content, multimedia, games, and more • the feature of Java that is primarily responsible for its initial popularity • users can run applets simply by visiting a web page that contains an applet program (if they have the Java runtime environment installed on their computer)

  3. Applet

  4. APPLET… • Loaded into a java-enabled program (Netscape, Applet Viewer) • Applet is not a stand-alone program (no main method), structured to run inside another program (browser) • JVM runs applications, but not applets. An applet can run as an application if it defines a main(). • Applets are usually event-driven.

  5. APPLET… • Does I/O through a GUI, only displays error messages with stdout. • Applets subclass java.applet • Security - applets barred from reading/writing files on the client, can’t load libraries, can’t make network connections (except to its host), can’t execute programs, etc...

  6. init() start() do some work stop() destroy() Applet life cycle • browser visits page containing an applet • browser calls init on that applet, once • browser calls start on that applet • browser goes away from that page • browser calls stop on that applet • browser comes back to that page • browser calls start again on that applet ... • browser shuts down • browser calls destroy on the applet, once

  7. APPLET • Applet lifecycle: (methods overriden from Applet class): init(), start(), paint(g),stop(), destroy() • Loading an applet • an instance of the applet’s controlling class (Applet subclass) created • applet initializes itself • applet starts running • Leaving and returning to applet page • when page is left, applet stops • upon page return, applet starts • Applet reloading/unloading • applet performs final cleanup, before reloading • Browser quitting - (applet stops, performs final cleanup)

  8. Applet Skeleton import java.awt.*; import java.applet.*; /* <applet code="AppletSkel" width=300 height=100> </applet> */ public class AppletSkel extends Applet { // Called first. public void init() { // initialization } /* Called second, after init(). Also called whenever the applet is restarted. */ public void start() { // start or resume execution } // Called when the applet is stopped. public void stop() { // suspends execution } /* Called when applet is terminated. This is the last method executed. */ public void destroy() { // perform shutdown activities } // Called when an applet's window must be restored. public void paint(Graphics g) { // redisplay contents of window } }

  9. Simple Applet Display Methods • To output a string to an applet, use drawString( ), which is a member of the Graphics class. Typically, it is called from within paint( ). It has the following general form:void drawString(String message, int x, int y)Here, message is the string to be output beginning at x,y. • In a Java window, the upper-left corner is location 0,0.

  10. Simple Applet Display Methods… • To set the background color of an applet’s window, use setBackground( ). • To set the foreground color (the color in which text is shown, for example), use setForeground( ). • These methods are defined by Component, and they have the following general forms:void setBackground(Color newColor)void setForeground(Color newColor) • A good place to set the foreground and background colors is in the init( ) method. • Of-course, you can change these colors as often as necessary during the execution of your applet.

  11. Simple Applet Program • Here is a very simple applet that sets the background color to cyan, the foreground color to red, and displays a message that illustrates the order in which the init( ), start( ), and paint( ) methods are called when an applet starts up. import java.awt.*; import java.applet.*; public class Sample extends Applet{ String msg; public void init() { setBackground(Color.cyan); setForeground(Color.red); msg = "Inside init( ) --"; } public void start() { msg += " Inside start( ) --"; } public void paint(Graphics g) { msg += " Inside paint( )."; g.drawString(msg, 10, 30); } }

  12. Simple Applet Program… • The applet generates the window shown here:

  13. HTML APPLET TAG (Element) <applet code="..." width=xxx height=xxx ...> ... </applet> • Required Attributes • CODE • Designates the filename of the Java class file to load • Filename interpreted with respect to directory of current HTML page (default) unless CODEBASE is supplied • WIDTH and HEIGHT • Specifies area the applet will occupy • Values can be given in pixels or as a percentage of the browser window width

  14. Web page with an applet • Put a page like this in your project's folder: <HTML> <HEAD> <TITLE>My Applet Page</TITLE> </HEAD> <BODY> <APPLET code="mypackage/MyApplet.class" width=400 height=300> </APPLET> </BODY> </HTML>

  15. Useful Applet Methods • getCodeBase, getDocumentBase • The URL of the: Applet file - getCodeBase HTML file - getDocumentBase • getParameter • Retrieves the value from the associated HTML PARAM element • getSize • Returns the Dimension (width, height) of the applet • getGraphics • Retrieves the current Graphics object for the applet • The Graphics object does not persist across paint invocations

  16. Useful Applet Methods… • showStatus • Displays a string in the status line at the bottom of the browser • getCursor, setCursor • Defines the Cursor for the mouse, for example, CROSSHAIR_CURSOR, HAND_CURSOR, WAIT_CURSOR • getBackground, setBackground • Gets/sets the background color of the applet • SystemColor class provides access to desktop colors • getForeground, setForeground • Gets/sets foreground color of applet (default color of drawing operations)

  17. Useful Applet Methods… • getAudioClip(URL url, String fileName) • play • Retrieves an audio file from a remote location and plays it • JDK 1.1 supports .au only. Java 2 also supports MIDI, .aiff and .wav

  18. Sample Playing Audio Clip Program import java.applet.Applet; import java.applet.AudioClip; public class NewApplet extends Applet { public void start(){ AudioClip acl; acl = this.getAudioClip(getDocumentBase(),"M1F1-Alaw-AFsp.au"); acl.play(); } }

  19. Useful Applet Methods… Loading images in applet • public Image getImage(URL url)Loads and returns the image file at the given URL. • public Image getImage(URL folder, String file)Loads the image in the given folder with the given name. Often used with getCodeBase().

  20. Requesting Repainting • repaint() method is defined by AWT. • Causes AWT runtime system to execute paint() method. • repaint() method has four forms: • void repaint()this method causes the entire window to be repainted. • void repaint(int left, int top, int width, int height)this method causes a specific region in a window to be repainted. • void repaint(long maxDelay)Here maxDelay specifies the maximum number of milliseconds that can elapse before calling repaint(). • void repaint(long maxDelay, int x, int y, int width, int height)

  21. Summary • Applet operations are restricted • Applet cannot read/write local files, call local programs, or connect to any host other than the one from which it was loaded • The init method • Called only when applet loaded, not each time executed • This is where you use getParameter to read PARAM data • The paint method • Called each time applet is displayed • Coordinates in drawing operations are wrt top-left corner • Drawing images • getImage(getCodeBase(), "imageFile") to “load” • drawImage(image, x, y, this) to draw

  22. Exercises • What is an applet? How does an applet differ from applications? • Explain with an example how we implement an applet into a webpage? • Explain how to set background area in applet? • What are the methods that controls an applet’s life cycle?

  23. Assignment • Write a program to load an image to your applet. • Write a program to draw a home in to your applet using Graphics methods.

More Related

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

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