computer science 209 applets. applications and applets a java application runs on a stand-alone...
TRANSCRIPT
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– access files on the client’s computer– write to files on the server’s computer– make other network connections
Runtime Setup
File storage
Host computer
JVM MyApp.class
Client computer
Web Browser
MyApplet.classJVM Server computer
MyPage.html
Connections to server are read-only
Development: Applet Viewer
File storage
Host computer
JVM MyApp.class
Host computer
Applet Viewer
MyApplet.classJVM
MyPage.html
Read/write access to host file system
File storage
Development: Applet Viewer
Development: Local Host
File storage
Host computer
JVM MyApp.class
Host computer
Web Browser
MyApplet.classJVM
MyPage.html
Client and server are on the host computer
Read access to host libraries
File storage
Development: Local Host
The Applet HTML Tag
<applet code="AnyApplet.class" width=anInt height=anInt>
General form:
WIDTH and HEIGHT give the number of pixels in thedimensions of the applet’s “window” on the page
<applet code="MainView4.class" width=200 height=200>
Example:
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
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()
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
The Card Tester Applicationpublic 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); }}
The Card Tester Appletpublic 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); }}
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
Loading Images in the Usual Mannerprivate 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
Loading Images from a Remote Servernew 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));
Two Different Strategiesnew 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));
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
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)); }}
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
The Card Tester Appletpublic 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); }}
The Card Tester Appletpublic 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); }}
public class Card implements Comparable<Card>{
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