AppletsEvent HandlingThreads and more in Java.ppt
《AppletsEvent HandlingThreads and more in Java.ppt》由会员分享,可在线阅读,更多相关《AppletsEvent HandlingThreads and more in Java.ppt(72页珍藏版)》请在麦多课文档分享上搜索。
1、Applets Event Handling Threads and more in Java,Sami Khuri Mathematics & Computer Science Department,Creating the “First” Applet,Create a Java source file: First.java Compile First.java. The Java compiler creates the Java bytecode file First.class in the same directory. Create an HTML file First.htm
2、l, for example, that includes First.class. Run the applet in a Java-enabled browser or a Java applet viewing program.,java.lang.Object | +-java.awt.Component | +-java.awt.Container | +-java.awt.Panel | +-java.applet.Applet AWT: Abstract Windowing Toolkit,Applets,Methods defined in the class Applet:
3、init(): Applet is being loaded by browser. Used for one-time initialization. start(): Browser entered page containing applet stop(): Browser leaves page containing applet destroy(): Applet is discarded by browser. paint() is used for drawing on applets. It is a method in the class Component.,Java-en
4、abled Browser,When a Java-enabled browser encounters an tag, it: reserves a display area of the specified width and height for the applet loads the bytecodes for the specified Applet subclass creates an instance of the subclass calls the instances init() and start() methods.,HyperText Markup Languag
5、e,An HTML documents contains tags that specify formatting instructions.An HTML tag is enclosed in angle brackets: example and The width and height (in pixels) of the area in which the applet is displayed is included in the html file.,Inheriting from above classes,Some methods in Component: add(), re
6、move() and setLayout(): controls the positions & sizes of components. Applets inherit the drawing and event handling methods from AWT Component class to produce user interfaces. Drawing: images, control of color and font. UI components: buttons, etc Event handling: detecting & responding to mouse dr
7、agging, button pushing, key pressing,Component, Container, and Panel,From AWT Container, applets get methods to hold components & use layout managers. Panels and applets can only be displayed on other graphical surfaces. A panel must be added to another container in order to be displayed. A componen
8、t is added to a container by using add() from the Container class.,Security Restrictions,Restrictions imposed on applets loaded over the network. The applet cannot: Dynamically load native code libraries. Read from or write to the local file system. Make network connections to any computer except to
9、 the one from which it obtained the code. Read certain system properties. Start a printing job.,More Applet Capabilities,Applets can make network connections to the host from which they came. Applets that are loaded from the local file system (from a directory in the users CLASSPATH) have none of th
10、e restrictions that applets loaded over the network do. Applets can load data files (show images) and play sounds.,Displaying Images,The code base, returned by the Applet getCodeBase() method, is a URL that specifies the directory from which the applets classes were loaded. The document base, return
11、ed by the Applet getDocumentBase() method, specifies the directory of the HTML page that contains the applet.,Example on Displaying Images,Images must be in GIF or JPEG format. Example: Image file yawn.gif is in directory “images”. To create an image object “nekopicture” that contains “yawn.gif”: Im
12、age nekopicture = new Image nekopicture = getImage(getCodeBase(), “images/yawn.gif“);,Playing Sounds,The AudioClip interface in the Applet class provides basic support for playing sounds. Sound format: 8 bit, 8000 Hz, one-channel, Sun “.au“ files. Methods in AudioClip that need to be implemented in
13、the applet: loop(): starts playing the clip repeatedly play() & stop() to play & stop the clip.,Inheritance tree of applets & frames,Object,Component,Container,Window,Frame,Panel,Applet,Label,Canvas,Scrollbar,Choice,List,Button,Checkbox,TextComponent,Event Handling,With event-driven programming, eve
14、nts are detected by a program and handled appropriately Events: moving the mouse clicking the button pressing a key sliding the scrollbar thumb choosing an item from a menu,Three Steps of Event Handling,Prepare to accept events import package java.awt.event Start listening for events include appropr
15、iate methods Respond to events implement appropriate abstract method,1. Prepare to accept events,Import package java.awt.event Applet manifests its desire to accept events by promising to “implement” certain methods Example: “ActionListener” for Button events “AdjustmentListener” for Scrollbar event
16、s,2. Start listening for events,To make the applet “listen” to a particular event, include the appropriate “addxxxListener”. Examples: addActionListener(this) shows that the applet is interested in listening to events generated by the pushing of a certain button.,2. Start listening for events (cont)
17、,Example addAdjustmentListener(this) shows that the applet is interested in listening to events generated by the sliding of a certain scroll bar thumb. “this” refers to the applet itself - “me” in English,3. Respond to events,The appropriate abstract methods are implemented. Example: actionPerformed
18、() is automatically called whenever the user clicks the button. Thus, implement actionPerformed() to respond to the button event.,3. Respond to events (cont),Example: adjustmentValueChanged() is automatically invoked whenever the user slides the scroll bar thumb. So adjustmentValueChanged() needs to
19、 be implemented. In actionPerformed(ActionEvent evt), ActionEvent is a class in java.awt.event.,north,center,south,leftMsg,rightMsg,centerValue,ranger,statement,pan2,pan3,pan1,StatBar,Threads,A lightweight sequential flow of control that shares an address space and resources with other threads. A th
20、read, unlike processes (heavyweight), has a low context switching time. Threads are indispensable for sockets, image holding, and animation.,Threads in Java,Traditionally, threads are implemented at the system level, separate from the programming language. Java is a language and a runtime system and
21、 threads are integrated in both. The keyword synchronize is used to make a block of code accessible to at most one thread at a time.,Purpose of Threads,Making a User Interface more responsive. When one method can use the partial output of another without waiting for the first one to finish. Example:
22、 image-loading and image-displaying methods. Any type of application that lends itself to concurrency.,How to create threads,There are two ways of creating threads in Java: 1) Extend the “Thread” classWe can instantiate the class Thread as many times as desired to achieve multi-threading. 2) Impleme
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- APPLETSEVENTHANDLINGTHREADSANDMOREINJAVAPPT
