computer science 209 applets. applications and applets a java application runs on a stand-alone...

24
Computer Science 209 Applets

Upload: jevon-crawley

Post on 15-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

Computer Science 209

Applets

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

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

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

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

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

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

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

Development: Applet Viewer

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

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

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

Development: Local Host

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

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:

Page 10: 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 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

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

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

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

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

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

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); }}

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

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); }}

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

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

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

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

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

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));

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

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));

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

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

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

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)); }}

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

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

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

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); }}

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

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); }}

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

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