1 / 36

System development with Java

System development with Java. Instructors: Rina Zviel-Girshin Lecture 10. Overview. Applet API GUI. 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.

Download Presentation

System development with Java

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. System development with Java Instructors: Rina Zviel-Girshin Lecture 10 Rina Zviel-Girshin @ARC

  2. Overview • Applet API • GUI Rina Zviel-Girshin @ARC

  3. 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. Rina Zviel-Girshin @ARC

  4. 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. Rina Zviel-Girshin @ARC

  5. 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. Rina Zviel-Girshin @ARC

  6. 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 Rina Zviel-Girshin @ARC

  7. 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. Rina Zviel-Girshin @ARC

  8. 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); } } Rina Zviel-Girshin @ARC

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

  10. 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); } } Rina Zviel-Girshin @ARC

  11. 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. Rina Zviel-Girshin @ARC

  12. 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. • <applet name=“rina”..> Rina Zviel-Girshin @ARC

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

  14. 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 <applet codebase=url code=“Name.class" width=600 height=100> <param name=“name" value=“value"> </applet> Rina Zviel-Girshin @ARC

  15. 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. Rina Zviel-Girshin @ARC

  16. 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. Rina Zviel-Girshin @ARC

  17. 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. Rina Zviel-Girshin @ARC

  18. 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); } } Rina Zviel-Girshin @ARC

  19. Result Rina Zviel-Girshin @ARC

  20. 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. Rina Zviel-Girshin @ARC

  21. 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() Rina Zviel-Girshin @ARC

  22. 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 Rina Zviel-Girshin @ARC

  23. 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")); Rina Zviel-Girshin @ARC

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

  25. Result Rina Zviel-Girshin @ARC

  26. 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. Rina Zviel-Girshin @ARC

  27. 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() Rina Zviel-Girshin @ARC

  28. 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. Rina Zviel-Girshin @ARC

  29. 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); } } Rina Zviel-Girshin @ARC

  30. 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. Rina Zviel-Girshin @ARC

  31. 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); } Rina Zviel-Girshin @ARC

  32. 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) {} } Rina Zviel-Girshin @ARC

  33. 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 Rina Zviel-Girshin @ARC

  34. 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); } Rina Zviel-Girshin @ARC

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

  36. Any Questions? Rina Zviel-Girshin @ARC

More Related