Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 209 Applets. Applications and Applets A Java application runs on a stand-alone computer and may connect to other computers via sockets.

Similar presentations


Presentation on theme: "Computer Science 209 Applets. Applications and Applets A Java application runs on a stand-alone computer and may connect to other computers via sockets."— 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

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

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

6 Development: Applet Viewer

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

8 Development: Local Host

9 The Applet HTML Tag General form: WIDTH and HEIGHT give the number of pixels in the dimensions of the applet ’ s “ window ” on the page Example:

10 A Web Page for the Card Tester Card Tester The title and size are specified in the Web page, not the Java code

11 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()

12 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

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

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

15 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

16 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

17 Loading Images from a Remote Server new ImageIcon(getClass().getResource(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 new ImageIcon(getImage(getDocumentBase(), fileName));

18 Two Different Strategies new ImageIcon(view.getClass().getResource(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! new ImageIcon(view.getImage(view.getDocumentBase(), fileName));

19 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

20 import javax.swing.*; public class AppletImageLoader implements ImageLoader{ private JApplet view; public AppletImageLoader(JApplet view){ 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)); }

21 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

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

23 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(); JButton button = new JButton("Deal"); button.addActionListener(blah blah); Container c = getContentPane(); setLayout(new BorderLayout()); c.add(panel, BorderLayout.CENTER); c.add(button, BorderLayout.SOUTH); }

24 public class Card implements Comparable { private Suit suit; private int rank; private boolean faceUp; 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(ImageLoader loader){ Card.loader = loader; } // Other methods, except for getImageFromFile and getBackFromFile Changes to the Card Class


Download ppt "Computer Science 209 Applets. Applications and Applets A Java application runs on a stand-alone computer and may connect to other computers via sockets."

Similar presentations


Ads by Google