Java Applet. Introductions Applet is java program that can be embedded into HTML pages Java applets runs on the java enables web browsers such as mozila.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Graphics Programming With Applets Feb 23, Applets There are three different types of executable java code. –Standalone application, which has main()
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Program Design With Methods And Graphics / Chapter 4 1 Abstract Window Toolkit.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Introduction to Java GUI Creating Graphical User Interfaces © copyright Bobby Hoggard / material may not be redistributed without permission.
Java Swing © Walter Milner 2005: Slide 1 Java Swing Walter Milner.
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.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
GUI programming Graphical user interface-based programming.
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Swing [part.1] Taken from Water Milner Eriq Muhammad Adams J |
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
ITEC 109 Lecture 27 GUI. GUIs Review Sounds –Arrays hold sample values –Creating a keyboard –Sound effects Homework 3 –The big two –Due after break –Lab.
Java Swing Tutorial. Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Review_6 AWT, Swing, ActionListener, and Graphics.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
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.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Java Swing. Note - this presentation.. often needs to refer to source code which is too big to put on a slide So the source code is in a separate Word.
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.
A Quick Java Swing Tutorial
Java Applet.
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
UNIT-5.
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Java Applet

Introductions Applet is java program that can be embedded into HTML pages Java applets runs on the java enables web browsers such as mozila and internet explorer. Applet is designed to run remotely on the client browser, so there are some restrictions on it. Applet can't access system resources on the local computer. Applets are used to make the web site more dynamic and entertaining.

Advantages of Applet * Applets are cross platform and can run on Windows, Mac OS and Linux platform * Applets can work all the version of Java Plugin * Applets runs in a sandbox, so the user does not need to trust the code, so it can work without security approval * Applets are supported by most web browsers * Applets are cached in most web browsers, so will be quick to load when returning to a web page * User can also have full access to the machine if user allows

Disadvantages of Java Applet * Java plug-in is required to run applet * Java applet requires JVM so first time it takes significant startup time * If applet is not already cached in the machine, it will be downloaded from internet and will take time * Its difficult to desing and build good user interface in applets compared to HTML technology

The Applet class To create an applet, you must import the Applet class This class is in the java.applet package The Applet class contains code that works with a browser to create a display window Capitalization matters! applet and Applet are different names

Importing the Applet class Here is the directive that you need: import java.applet.Applet; import is a keyword java.applet is the name of the package A dot (. ) separates the package from the class Applet is the name of the class There is a semicolon ( ; ) at the end

Applet versus Application Applets are small programs while applications are larger programs. Applets don't have the main method while in an application execution starts with the main method. Applets can run in our browser's window or in an appletviewer. To run the applet in an appletviewer will be an advantage for debugging. Applets are designed for the client site programming purpose while the applications don't have such type of criteria.

The Life cycle of An Applet init(): This method is called to initialized an applet start(): This method is called after the initialization of the applet. stop(): This method can be called multiple times in the life cycle of an Applet. destroy(): This method is called only once in the life cycle of the applet when applet is destroyed.

The Life cycle of An Applet import java.awt.*; import java.applet.*; class Myclass extends Applet{ public void init(){} Publicvoidstart(){} public void stop() {} public void destroy() {} public void paint(Graphics g) {}

Applet Tag [ [ ] [ ].. [alternateHTML] [ ]

Creating First Applet Example import java.applet.*; import java.awt.*; public class FirstApplet extends Applet{ public void paint(Graphics g){ g.drawString("Welcome in Java Applet.",40,20); }

HTML code

Drawing Shapes Example in java Graphics.drawLine() : to draw the line in the applet. drawLine(int X_from_coordinate, int Y_from_coordinate, int X_to_coordinate, int Y_to_coordinate); Graphics.drawString() : draws the given string as the parameter drawString(String string, int X_coordinate, int Y_coordinate); Graphics.drawOval() : draws the circle g.drawOval(int X_coordinate, int Y_coordinate, int Wdth, int height); Graphics.drawRect() : draws the rectangle. Here is the syntax of the drawRect() method : g.drawRect(int X_coordinate, int Y_coordinate, int Wdth, int height)

Drawing Shapes Example using color in java Graphics.setColor() : sets the color for the object by specified color. setColor(Color.color_name); Graphics.fillOval() : to fill the color inside the oval by specified color g.fillColor(Color.color_name); Graphics.fillRect() :to fill the color inside the rectangle by specified color g.fillRect(int X_coordinate, int Y_coordinate, int Wdth, int height)

Passing Parameter in Java Applet The param tag( ) is used to pass the parameters to an applet To access values String strParameter = this.getParameter("Message");

Event Listeners Example To handle the events generated by these buttons you add action listeners e.g. object_name.addActionListener(this);. When the action event occurs, that object's actionPerformed method is invoked. actionPerformed(ActionEvent e)

Display image paint(Graphics g) has used MediaTracker is a utility class that tracks the status of a number of media objects img - image name type of Image. x - lower X - Coordinate type of int. y - lower Y - Coordinate type of int. x1 - upper X - Coordinate type of int. y1 - upper Y - Coordinate type of int.

Java Swing Tutorial

Java Swing * A part of The JFC * Swing Java consists of Look and feel Accessibility Java 2D Drag and Drop, etc

Introductions Swing is one of the Graphical User Interface tool. Swing is used to develop the graphical user interface (GUI) in java. Swing components used for the building of GUI. Swing components are helpful for interactivity to Java applications. The components of Swing toolkit are given below: list controls buttons labels tree controls table controls

Java Swing Class Hierarchy

Swing Events event source // The Event source is the object. It generates Events. event object // The Event object encapsulates the condition changes in the event source. event listener // The Event listener is the object that requests to be notified Event source: Object is handling an event to the event listener.Event handling in Swing toolkit is very easy to handle and powerful. Objects are notified when a specific event occurs.

JFrame Open frame place the icon on the title bar.Methods are as follows: frame.setIconImage(Toolkit.getDefaultToolkit().getI mage("icon_confused.gif")); it’s using the Image class method named getImage().frame.getDefaultToolkit(): //This is the method of the Toolkit class which gets the default toolkit.

createAndShowGUI private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("Hi.."); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add a label. JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); //Display the window. frame.pack(); frame.setVisible(true); }

Menus JMenuBar JMenu JMenuItem

checkBox import javax.swing.*; JCheckBox chk = new JCheckBox("This is the Check Box"); frame.add(chk);

Text Area import javax.swing.JTextArea; // Create a text area with some initial text JTextArea textarea = new JTextArea("Initial Text"); int rows = 20; int cols = 30; textarea = new JTextArea("Initial Text", rows, cols); TextField JTextField t = new JTextField("Text field 3", 8);

JButton Button: Two types: JButton(), JButton(Name) JButton () is used for create blank JButton instance. JButton (Name) is used for create a JButton instance with the specified text. JButton Button1; Button1 = new JButton ("Black is White"); add (Button1);

Layout Managers Most Swing UIs utilise a LayoutManager to control positioning of items There is a choice of these which work in different ways Initially we do without one, and position items ourselves: frame.setLayout(null);

Absolute positioning JFrame frame = new JFrame("I am a JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(20,30,300,100); frame.setLayout(null); JButton butt=new JButton("Click me"); frame.getContentPane().add(butt); butt.setBounds(20, 20, 200,20); frame.setVisible(true);

FlowLayout JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("FlowLayout"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new FlowLayout()); JButton b1 = new JButton("Hello"); frame.getContentPane().add(b1); JButton b2 = new JButton("Two"); frame.getContentPane().add(b2); JTextField t1 = new JTextField("Text here"); frame.getContentPane().add(t1); frame.pack(); frame.setVisible(true);

BorderLayout JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Border"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton b1 = new JButton("At the top"); frame.getContentPane().add(b1,BorderLayout.PAGE_START ); JButton b2 = new JButton("Bottom"); frame.getContentPane().add(b2,BorderLayout.PAGE_END); JTextField t1 = new JTextField("Left"); frame.getContentPane().add(t1,BorderLayout.LINE_START); JTextField t2 = new JTextField("Right"); frame.getContentPane().add(t2,BorderLayout.LINE_END); JButton b3 = new JButton("Centre"); frame.getContentPane().add(b3,BorderLayout.CENTER ); frame.pack(); frame.setVisible(true);

Grid Layout JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Grid"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new GridLayout(4,3,5,5)); for (int i=0; i<10; i++) frame.getContentPane().add(new JButton(""+i)); frame.pack(); frame.setVisible(true);

Swing has a lot of classes controls User I/O widgets eg JButton containers things that hold other things eg JFRame

Containers general purpose containers - panel scroll pane split pane tabbed pane tool bar top level containers - JFrame JApplet JDialog

JPanel ( in createAndShowGUI) JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("I am a JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(20,30,300,100); frame.setLayout(null); //Create a panel JPanel myPanel = new JPanel(); myPanel.setBackground(new Color(255,3,25)); myPanel.setOpaque(true); //Make it the content pane. frame.setContentPane(myPanel); frame.setVisible(true);

Tooltip and border myPanel.setOpaque(true); myPanel.setToolTipText("I'm a JPanel"); myPanel.setBorder(BorderFactory.createLineBor der(Color.white)); frame.setContentPane(myPanel);..

JSplitPane setLayout(null); //Create a split pane JSplitPane myPane = new JSplitPane(); myPane.setOpaque(true); frame.setContentPane(myPane); frame.setVisible(true);

JSplitPane with JPanels //Create a split pane JSplitPane myPane = new JSplitPane(); myPane.setOpaque(true); myPane.setDividerLocation(150); // make two panels JPanel right = new JPanel(); right.setBackground(new Color(255,0,0)); JPanel left = new JPanel(); left.setBackground(new Color(0,255,0)); // set as left and right in split myPane.setRightComponent(right); myPane.setLeftComponent(left);

Exercise