1 / 24

Computer Science 209

Computer Science 209. Applets. Applications and Applets. A Java application runs on a stand-alone computer and may connect to other computers via sockets A Java applet downloads from a Web server to a Web browser and runs in that browser Applets have full functionality, except they cannot

Download Presentation

Computer Science 209

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. Computer Science 209 Applets

  2. Applications and Applets • A Java application runs on a stand-alone computer and may connect to other computers via sockets • A Java applet downloads from a Web server to a Web browser and runs in that browser • Applets have full functionality, except they cannot • access files on the client’s computer • write to files on the server’s computer • make other network connections

  3. Runtime Setup Host computer JVM MyApp.class File storage Connections to server are read-only Client computer Web Browser MyPage.html JVM MyApplet.class Server computer

  4. Development: Applet Viewer Host computer JVM MyApp.class File storage Read/write access to host file system Host computer Applet Viewer MyPage.html JVM MyApplet.class File storage

  5. Development: Applet Viewer

  6. Development: Local Host Host computer JVM MyApp.class File storage Client and server are on the host computer Read access to host libraries Host computer Web Browser MyPage.html JVM MyApplet.class File storage

  7. Development: Local Host

  8. The Applet HTML Tag General form: <applet code="AnyApplet.class" width=anInt height=anInt> WIDTH and HEIGHT give the number of pixels in the dimensions of the applet’s “window” on the page Example: <applet code="MainView4.class" width=200 height=200>

  9. A Web Page for the Card Tester <html> <--! Author: Ken Lambert --> <head> <title>Card Tester</title> </head> <body> <applet code="MainView4.class" width=200 height=200></applet> </body> </html> The title and size are specified in the Web page, not the Java code

  10. Changes to a Java Program • The main method is omitted, so there is no separate application class • The main view class extends JApplet instead of JFrame • The main view’s constructor is renamed to public void init()

  11. The JApplet Class Hierarchy Package Note that JApplet is a container class, and is a subclass of Panel Thus, its default layout manager is FlowLayout

  12. The Card Tester Application public class MainView3 extends JFrame{ public MainView3(){ final Deck deck = new Deck(); final CardPanel panel = new CardPanel(); JButtonbutton = new JButton("Deal"); button.addActionListener(blah blah); Container c = getContentPane(); c.add(panel, BorderLayout.CENTER); c.add(button, BorderLayout.SOUTH); } }

  13. The Card Tester Applet public class MainView4 extends JApplet{ public void init(){ final Deck deck = new Deck(); final CardPanel panel = new CardPanel(); JButtonbutton = new JButton("Deal"); button.addActionListener(blah blah); Container c = getContentPane(); setLayout(new BorderLayout()); c.add(panel, BorderLayout.CENTER); c.add(button, BorderLayout.SOUTH); } }

  14. Problem: Loading Card Images • Card images load from the local disk when using the applet viewer or when running the browser and server on the same computer • Card images do not load in the usual manner when the applet is loaded from a remote server

  15. Loading Images in the Usual Manner private Icon getImageFromFile(int rank, Suit suit){ String fileName = "DECK/"; fileName+= rank; fileName+= Character.toUpperCase(suit.toString().charAt(0)); fileName+= ".GIF"; return new ImageIcon(getClass().getResource(fileName)); } private Icon getBackFromFile(){ String fileName = "DECK/CARDBACK.GIF"; return new ImageIcon(getClass().getResource(fileName)); } Works only for applications and applets run locally

  16. Loading Images from a Remote Server new ImageIcon(getClass().getResource(fileName)); new ImageIcon(getImage(getDocumentBase(), fileName)); An applet must be able to call back to the server from which it originates for any data files The methods getImage and getDocumentBase must be run on the applet object to accomplish this

  17. Two Different Strategies new ImageIcon(view.getClass().getResource(fileName)); new ImageIcon(view.getImage(view.getDocumentBase(), fileName)); The first view is a JFrame in an application The second view is a JApplet in an applet There might be other main views!

  18. An Image Loader Strategy import javax.swing.Icon; public interface ImageLoader{ public Icon getImageFromFile(int rank, Suit suit); public Icon getBackFromFile(); } The main view will define an image loader class that realizes a particular strategy for loading images

  19. import javax.swing.*; public class AppletImageLoader implements ImageLoader{ private JAppletview; public AppletImageLoader(JAppletview){ this.view= view; } public Icon getImageFromFile(int rank, Suit suit){ String fileName = "DECK/"; fileName+= rank; fileName+= Character.toUpperCase(suit.toString().charAt(0)); fileName+= ".GIF"; return new ImageIcon(view.getImage(view.getDocumentBase(), fileName)); } public Icon getBackFromFile(){ String fileName = "DECK/CARDBACK.GIF"; return new ImageIcon(view.getImage( view.getDocumentBase(), fileName)); } }

  20. Using the Image Loader Strategy • The main view instantiates its image loader and passes it to the Card class • The Card class maintains the image loader as a class variable • The image loader’s methods are run when images are loaded

  21. The Card Tester Applet public class MainView4 extends JApplet{ public void init(){ final Deck deck = new Deck(); final CardPanel panel = new CardPanel(); JButtonbutton = new JButton("Deal"); button.addActionListener(blah blah); Container c = getContentPane(); setLayout(new BorderLayout()); c.add(panel, BorderLayout.CENTER); c.add(button, BorderLayout.SOUTH); } }

  22. The Card Tester Applet public class MainView4 extends JApplet{ public void init(){ final ImageLoader loader = new AppletImageLoader(this); Card.setImageLoader(loader); final Deck deck = new Deck(); final CardPanel panel = new CardPanel(); JButtonbutton = new JButton("Deal"); button.addActionListener(blah blah); Container c = getContentPane(); setLayout(new BorderLayout()); c.add(panel, BorderLayout.CENTER); c.add(button, BorderLayout.SOUTH); } }

  23. Changes to the Card Class public class Card implements Comparable<Card>{ private Suit suit; private int rank; private booleanfaceUp; private Icon image; private static Icon CARD_BACK; private static ImageLoader loader; public Card(Suit suit, int rank){ this.suit= suit; this.rank= rank; faceUp= false; image = loader.getImageFromFile(rank, suit); if (CARD_BACK == null) CARD_BACK = loader.getBackFromFile(); } public static void setImageLoader(ImageLoaderloader){ Card.loader= loader; } // Other methods, except for getImageFromFile and getBackFromFile

More Related