java applet. introductions applet is java program that can be embedded into html pages java applets...

40
Java Applet

Upload: eileen-noreen-skinner

Post on 14-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

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

Java Applet

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

Introductions

Applet is java program that can be embedded into HTML pagesJava 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.

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

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

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

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

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

The Applet class

To create an applet, you must import the Applet classThis class is in the java.applet packageThe Applet class contains code that works with a browser to create a display windowCapitalization matters! applet and Applet are different names

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

Importing the Applet class

Here is the directive that you need: import java.applet.Applet;import is a keywordjava.applet is the name of the packageA dot ( . ) separates the package from the classApplet is the name of the classThere is a semicolon ( ; ) at the end

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

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.

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

The Life cycle of An Applet

init(): This method is called to initialized an appletstart(): 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.

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

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

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

Applet Tag

[<] APPLET [CODEBASE = codebaseURL] CODE = appletFile [ALT = alternateText] [NAME = appletInstanceName] WIDTH = pixels HEIGHT = pixels [ALIGN = alignment] [VSPACE = pixels] [HSPACE = pixels] > [< PARAM NAME = appletParameter1 VALUE = value >] [< PARAM NAME = appletParameter2 VALUE = value >] . . [alternateHTML] [</]APPLET[>]

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

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

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

HTML code

<HTML><HEAD></HEAD><BODY><APPLET ALIGN="CENTER" CODE="FirstApplet.class" WIDTH="800" HEIGHT="500"></APPLET></BODY></HTML>

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

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

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

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 colorg.fillColor(Color.color_name);Graphics.fillRect() :to fill the color inside the rectangle by specified colorg.fillRect(int X_coordinate, int Y_coordinate, int Wdth, int height)

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

Passing Parameter in Java Applet

The param tag(<parma name="" value=""></param>) is used to pass the parameters to an applet

To access values String strParameter = this.getParameter("Message");

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

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)

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

Display image

paint(Graphics g) has usedMediaTracker is a utility class that tracks the status of a number of media objectsimg - 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.

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

Java Swing Tutorial

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

Java Swing

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

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

IntroductionsSwing 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

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

Java Swing Class Hierarchy

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

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.

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

JFrame

Open frame place the icon on the title bar.Methods are as follows:frame.setIconImage(Toolkit.getDefaultToolkit().getImage("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.

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

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

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

Menus

JMenuBar

JMenu

JMenuItem

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

checkBox

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

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

Text Areaimport 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);TextFieldJTextField t = new JTextField("Text field 3", 8);

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

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

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

Layout Managers

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

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

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

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

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

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

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

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

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

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

Swing has a lot of classes

controls

User I/O widgets eg JButton

containers

things that hold other thingseg JFRame

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

Containers

general purpose containers - panel scroll pane split pane tabbed pane tool bar

top level containers - JFrame JApplet JDialog

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

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

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

Tooltip and border

myPanel.setOpaque(true); myPanel.setToolTipText("I'm a JPanel");myPanel.setBorder(BorderFactory.createLineBorder(Color.white)); frame.setContentPane(myPanel); ..

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

JSplitPane

setLayout(null);

//Create a split pane

JSplitPane myPane = new JSplitPane();

myPane.setOpaque(true);

frame.setContentPane(myPane);

frame.setVisible(true);

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

JSplitPane with JPanels

//Create a split paneJSplitPane myPane = new JSplitPane();myPane.setOpaque(true);myPane.setDividerLocation(150);// make two panelsJPanel 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 splitmyPane.setRightComponent(right); myPane.setLeftComponent(left);

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

Exercise