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

Slides:



Advertisements
Similar presentations
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Advertisements

LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
Applets The objectives of this chapter are: To describe applets and their purpose To discuss embedding applets in HTML pages.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
1 Applets. 2 Motivations When browsing the Web, you frequently see the graphical user interface and animation developed using Java. These programs are.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
Java Applets. Lecture Objectives Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
ISE 582: Information Technology for Industrial Engineering Instructor: Elaine Chew University of Southern California Department of Industrial and Systems.
ISE 582: Web Technology for Industrial Engineering University of Southern California DJE Dept of Industrial and Systems Engineering Lecture 9 JAVA Cup.
Applets. What is an Applet?  According to Sun “An applet is a small program that is intended not to be run on its own, but rather to be embedded inside.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Applets, HTML and GUI’s Recitation – 04/11/2008 CS 180 Department of Computer Science, Purdue University.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
19-Sep-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Applets Maj Joel Young
JAVA APPLETS By: Ms. Humaira Siddiqui. Java and the Internet Java is considered to be a language well suited to be used in the internet. In contrast with.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Li Tak Sing COMPS311F. Applets An applet is any small application that performs one specific task, sometimes running in the context of a larger program,
GUI programming Graphical user interface-based programming.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
Applets Yong Choi School of Business CSU, Bakersfield.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
1 Applets. 2 Design of Applets 3 Sun wrote Java to be executable within a hosting application browser The applications are applets. An applet is downloaded.
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Computer Science 209 GUIs Model/View/Controller Layouts.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
Creating a Java Application and Applet
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 17 : Applets King Fahd University of Petroleum & Minerals College of Computer Science.
Applet: An applet is a java program that is transmitted over the network from the server to client & executed within clients browser. Applets are used.
Java Applets.
GUIs Model/View/Controller Layouts
Computer Science 209 The Singleton Pattern.
30 Java Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
Graphical user interface-based programming
Applets.
UNIT-5.
Java applets 1/3/2019.
Computer Science 209 Images and GUIs.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
G6DICP - Lecture 27 Java Applets.
Presentation 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 JVMMyApp.class Client computer Web Browser MyApplet.class JVM Server computer MyPage.html Connections to server are read-only

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

Development: Applet Viewer

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

Development: Local Host

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:

A Web Page for the Card Tester Card Tester 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 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); }

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

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

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

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

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

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

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