Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.

Similar presentations


Presentation on theme: "Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10."— Presentation transcript:

1 Rina Zviel-Girshin @ARC1 System development with Java Instructors: Rina Zviel-Girshin Lecture 10

2 Rina Zviel-Girshin @ARC2 Overview Applet API GUI

3 Rina Zviel-Girshin @ARC3 What applet can do? Load data files specified relative to the URL of the applet or the page in which it is running. Display short status strings. Play sounds. Find other applets running in the same page.

4 Rina Zviel-Girshin @ARC4 Finding and Loading Data Files Whenever an applet needs to load some data from a file it uses either the code base (getCodeBase()) or the document base to form the complete URL (getDocumentBase()). getCodeBase() method returns a URL that specifies the directory from which the applet's classes were loaded getDocumentBase() method returns the directory of the HTML page that contains the applet.

5 Rina Zviel-Girshin @ARC5 Image Java supports two types of images:.gif and.jpeg Images must first be loaded and then displayed. Images are loaded to the applet using getImage() method. getImage() method have two syntax forms: –Image my =getImage(url); –Image my = getImage(url, relativePath); Images are drawn using –drawImage( image, x, y, ImageObserver) where the last argument can be this.

6 Rina Zviel-Girshin @ARC6 Example import java.awt.*; import java.applet.*; public class DrawImage extends Applet { Image image;//Image instance public void init() {//image is in subdirectory of the applet code image = getImage(getCodeBase(), "images/cat.gif"); } public void paint(Graphics g) { // drawing the image in the center of the applet g.drawImage(image, (this.getSize().width-image.getWidth(this))/2, (this.getSize().height-image.getHeight(this))/2, this); } } // end of class

7 Rina Zviel-Girshin @ARC7 Status Strings Status string is a string that appears on the status line at the bottom of the applet viewer window. Applets display status lines with the showStatus() method. Syntax: showStatus(“String message”); You should never put crucial information in the status line because status line can be overwritten by other applets or by the browser.

8 Rina Zviel-Girshin @ARC8 Example import java.applet.*; import java.awt.*; public class PaintStatus extends Applet { int count = 0; public void paint(Graphics g) { g.drawString("hello",this.getSize().height/2, this.getSize().width/2-10); count += 1; showStatus("paint() called " + count + " time" + ((count > 1) ? "s":"")); } public void update(Graphics g) { paint(g); } }

9 Rina Zviel-Girshin @ARC9 Sounds To play a sound you use play() method of the Applet class. Syntax: –play(url) –play(url, relativePath) Main disadvantage: plays exactly once.

10 Rina Zviel-Girshin @ARC10 Example import java.awt.*; import java.applet.*; public class PlayAudio extends Applet { public void init() { play(getCodeBase(), "images/train.au"); } public void paint(Graphics g) { g.drawString("This is a simple audio applet", 20, 20); }

11 Rina Zviel-Girshin @ARC11 Inter-applet Communication Applets can find other applets and send messages to them- inter-applet communication. Many browsers require that: –the applets originate from the same server. –the applets originate from the same directory on the server (the same code base). The Java API requires that the applets be running on the same html page. An applet can find another applet either – by looking it up by name getApplet method –or by finding all the applets on the page getApplets method.

12 Rina Zviel-Girshin @ARC12 Finding by name getApplet method finds applet by its name. If method is successful it returns to the caller an Applet objects and then the caller can invoke methods on the object. getApplet(String name) - returns a reference to the applet called name used in the Name attribute in the applet tag. The name attribute should be added to applet tag.

13 Rina Zviel-Girshin @ARC13 Finding by name We will show some examples later.

14 Rina Zviel-Girshin @ARC14 What your applet has access to? getParameter(String)-if there are parameters as part of the applet's invocation appletContext –a hook into the browser –showDocument(URL) / showDocument(URL, target) –showStatus(String) –getApplet() /getApplets() HTML

15 Rina Zviel-Girshin @ARC15 GUI Components are graphical user interface (GUI) widgets like –labels, –menus, –windows, –buttons, –text fields, –applets, and more. In Java all components are subclasses of java.awt.Component.

16 Rina Zviel-Girshin @ARC16 Label The simplest component is java.awt. A Label object is a component for placing text in a container. A label displays a single line of read-only text. Syntax: Label lname=new Label(“string”); The text can be changed by the application, but a user cannot edit it directly.

17 Rina Zviel-Girshin @ARC17 Label After creation Label should be added to the container (Panel, Applet,..). Syntax: this.add(lname); Stages of adding a component: –Declare the component –Initialize the component –Add the component to the layout.

18 Rina Zviel-Girshin @ARC18 Example import java.applet.*; import java.awt.*; public class LabelExample extends Applet { public void init() { Label mylabel = new Label("This is a Label"); this.add(mylabel); }

19 Rina Zviel-Girshin @ARC19 Result

20 Rina Zviel-Girshin @ARC20 Components paint() There is no paint() method in the previous applet. But label gets drawn on the screen anyhow. How does this happen? Components know how to paint themselves. When a container like an applet is repainted it calls the paint() method for all the components it contains. java.awt.Label has its own paint() method which knows how to paint() itself.

21 Rina Zviel-Girshin @ARC21 Format You can format your components. You can set font, foreground color, background color, name… Resizes this component so that it has new width and height. Moves component to a new location. Syntax: (setAttribute) –setSize() –setFont() –setLocation()

22 Rina Zviel-Girshin @ARC22 Example import java.awt.*; import java.applet.*; public class LabelExample1 extends Applet { public void init() { Label mylabel = new Label("Formatted label!"); mylabel.setForeground(Color.blue); mylabel.setBackground(Color.red); this.add(mylabel); } }//end class

23 Rina Zviel-Girshin @ARC23 Buttons Button creation: Button bname=new Button(“String”); After creation button should be added to the applet. Button creation and addition can be done in one line: add(new Button(“String"));

24 Rina Zviel-Girshin @ARC24 Example import java.applet.*; import java.awt.*; public class ButtonExample extends Applet { public void init () { this.add(new Button("Button with no actions")); }

25 Rina Zviel-Girshin @ARC25 Result

26 Rina Zviel-Girshin @ARC26 Actions In last example button did not do anything when it was pressed. But buttons do things when we press them. So we need to specify what to do when button is pressed. When the mouse is clicked on a Button, the Button fires an ActionEvent. To be ready to respond to this event we must register an ActionListener with the Button.

27 Rina Zviel-Girshin @ARC27 Action Listener Syntax: Button b= new Button(“Button with action"); add(b); b.addActionListener(myActionListener); // assign the button a listener where myActionListener is a reference to an object which implements the java.awt.event.ActionListener interface. This interface has a single method: actionPerformed()

28 Rina Zviel-Girshin @ARC28 Action Listener Since ActionListener is an interface and not a class, it can be implemented in our applet. Applet signature will be: public class Name extends Applet implements ActionListener { … } Method actionPerformed() will be implemented inside the applet.

29 Rina Zviel-Girshin @ARC29 Example import java.awt.*; import java.applet.*; import java.awt.event.*; public class ButtonAL extends Applet implements ActionListener { public void init () { Button b = new Button("Change applet background"); this.add(b); b.addActionListener(this); } public void actionPerformed(ActionEvent e) { this.setBackground(Color.green); } }

30 Rina Zviel-Girshin @ARC30 Mouse Listener What about the mouse? MouseListener interface listen to the mouse events (press, release, click, enter, and exit) on a component. The listener object added to the component using addMouseListener method. When a mouse event occurs, the relevant method in the listener object is invoked, and the MouseEvent is passed to it.

31 Rina Zviel-Girshin @ARC31 Example import java.awt.*; import java.applet.*; import java.awt.event.*; public class MouseClick extends Applet implements MouseListener { int xpos, ypos; public void init() { addMouseListener(this); } public void paint(Graphics g) { g.drawString("("+xpos+","+ypos+")",xpos,ypos); }

32 Rina Zviel-Girshin @ARC32 Example //methods of MouseListener interface public void mouseClicked (MouseEvent me) { xpos = me.getX(); ypos = me.getY(); repaint(); } //empty methods public void mousePressed (MouseEvent me) {} public void mouseReleased (MouseEvent me) {} public void mouseEntered (MouseEvent me) {} public void mouseExited (MouseEvent me) {} }

33 Rina Zviel-Girshin @ARC33 MouseMotionListener The listener interface for receiving mouse motion events on a component: MouseMotionListener. A mouse motion event is generated when the mouse is moved or dragged. MouseMotionListener interface has 2 methods: –mouseDragged –mouseMoved

34 Rina Zviel-Girshin @ARC34 Example import java.awt.*; import java.applet.*; import java.awt.event.*; public class MouseMotion extends Applet implements MouseMotionListener { int xpos,ypos; public void init() { addMouseMotionListener(this); } public void paint(Graphics g) { g.drawString("("+xpos+","+ypos+")",xpos,ypos); }

35 Rina Zviel-Girshin @ARC35 Example //MouseMotionListener methods public void mouseMoved(MouseEvent me) { xpos = me.getX(); ypos = me.getY(); repaint(); } public void mouseDragged(MouseEvent me){} }

36 Rina Zviel-Girshin @ARC36 Any Questions?


Download ppt "Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10."

Similar presentations


Ads by Google