Lesson 36: The calculator – Java Applets. 1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you.

Slides:



Advertisements
Similar presentations
Drawing in a frame – Java GUI
Advertisements

Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Programming and Problem Solving With Java Copyright 1999, James M. Slack Applets What is an Applet? Applet Parameters Graphics in Applets Other Applet.
Graphics Programming With Applets Feb 23, Applets There are three different types of executable java code. –Standalone application, which has main()
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Happy Tax Day! l Project 8 due on Thursday.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
Applets, Graphical User Interfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads.
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
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.
PHY281Introduction to JavaSlide 1 Introduction to Java In this section we will learn how how to use Java and write our first Java Applet:  The Java Development.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
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.
Java Workshop for Teachers May 6, 2005 A Brief Look at the Java Programming Language.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 13 l HTML l Applets Applets and HTML.
19-Sep-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Applets Maj Joel Young
Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Intro to Java 2 By Geb Thomas Based on the Java TutorialJava Tutorial.
Applets.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
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.
GUI programming Graphical user interface-based programming.
POS 406 Java Technology And Beginning Java Code
Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets.
Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 13 l HTML l Applets Applets and HTML.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
The Drawing program – Java Applets
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
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.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
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.
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.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Lecture 1. Introduction to Programming and Java MIT- AITI 2003.
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 GUI programming Graphical user interface-based programming Chapter G1 (pages )
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 Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
IT11 Agenda for Feb Golden Rule Reminder. 2. PowerPoint demonstration on the structure of Java Applet programs. Source Files HTML Files Class Files.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
Lesson 33: Layout management and drawing – Java 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.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Design Applet based java program to find the area and perimeter of a rectangle given its length and width. Java GUI.
Multiple buttons and action calls
Java Applet What is a Java Applet? How is applet compiled?
Java Applets.
Ellen Walker Hiram College
Graphical user interface-based programming
UNIT-5.
Java Applets.
The calculator – Java GUI
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

Lesson 36: The calculator – Java Applets

1. Creating Your First Applet HelloWorldApp is an example of a Java application, a standalone program. Now you will create a Java applet called HelloWorld, which also displays the greeting "Hello world!". Unlike HelloWorldApp, however, the applet runs in a Java-enabled Web browser such as HotJava, Netscape Navigator, or Microsoft Internet Explorer. import java.applet.*; import java.awt.*; /** * The HelloWorld class implements an applet that * simply displays "Hello World!". */ public class HelloWorld extends Applet { public void paint(Graphics g) { // Display "Hello World!" g.drawString("Hello world!", 50, 25); } Review

Compile the applet source File. Compiling the applet java code At the prompt, type the following command and press Return: javac HelloWorld.java The compiler should generate a Java bytecode file, HelloWorld.class. Review

Using the applet viewer to see the applet Run the Program. Although you can view your applets using a Web browser, you may find it easier to test your applets using the simple appletviewer application that comes with the JavaTM Platform. To view the HelloWorld applet using appletviewer, enter at the prompt: Review

The applet HTML file Review

The Java Application calculator

// MiniCalc.java - demo gridlayout import java.awt.*; import javax.swing.*; class MiniCalc{ public static void main (String[] args){ JFrame frame = new JFrame("MiniCalc"); Container pane = frame.getContentPane(); //Creating the major components JTextField firstNumber= new JTextField(20); JTextField secondNumber= new JTextField(20); JTextField result= new JTextField(20); JButton addButton = new JButton("Add"); JButton subButton = new JButton("Subtract"); pane.setLayout(new GridLayout(4,2)); pane.add(new JLabel("Enter a number")); pane.add(firstNumber); pane.add(new JLabel("Enter a number")); pane.add(secondNumber); pane.add(new JLabel("Result")); pane.add(result); pane.add(addButton); pane.add(subButton); DoMath listener = new DoMath(firstNumber, secondNumber, result); subButton.addActionListener(listener); addButton.addActionListener(listener); frame.pack(); frame.show(); } Review

// DoMath.java import javax.swing.*; import java.awt.event.*; class DoMath implements ActionListener{ DoMath(JTextField first, JTextField second, JTextField result){ inputOne = first; inputTwo = second; output = result; } public void actionPerformed(ActionEvent e){ double first, second; first = Double.parseDouble(inputOne.getText().trim()); second = Double.parseDouble(inputTwo.getText().trim()); if (e.getActionCommand().equals("Add")) output.setText(String.valueOf(first+second)); else output.setText(String.valueOf(first-second)); } private JTextField inputOne, inputTwo, output; } Review

Running the application Review

The Java Applet calculator

In this example we will rewrite the web calculator so that we can place it on the web. The benefit is that this application can run anywhere on any operating system that is Java enabeled.

The // MiniCalcApplet.java - /* <applet code=MiniCalcApplet.class" width=200 height =100>*/ import java.awt.*; import javax.swing.*; public class MiniCalcApplet extends JApplet{ public void init (){ Container pane = getContentPane(); JTextField firstNumber= new JTextField(20); JTextField secondNumber= new JTextField(20); JTextField result= new JTextField(20); JButton addButton = new JButton("Add"); JButton subButton = new JButton("Subtract"); // There will be 4 rows of two components each pane.setLayout(new GridLayout(4,2)); pane.add(new JLabel("Enter a number")); pane.add(firstNumber); pane.add(new JLabel("Enter a number")); pane.add(secondNumber); pane.add(new JLabel("Result")); pane.add(result); pane.add(addButton); pane.add(subButton); DoMath listener = new DoMath(firstNumber, secondNumber, result); subButton.addActionListener(listener); addButton.addActionListener(listener); } // MiniCalc.java - demo gridlayout import java.awt.*; import javax.swing.*; class MiniCalc{ public static void main (String[] args){ JFrame frame = new JFrame("MiniCalc"); Container pane = frame.getContentPane(); JTextField firstNumber= new JTextField(20); JTextField secondNumber= new JTextField(20); JTextField result= new JTextField(20); JButton addButton = new JButton("Add"); JButton subButton = new JButton("Subtract"); pane.setLayout(new GridLayout(4,2)); pane.add(new JLabel("Enter a number")); pane.add(firstNumber); pane.add(new JLabel("Enter a number")); pane.add(secondNumber); pane.add(new JLabel("Result")); pane.add(result); pane.add(addButton); pane.add(subButton); DoMath listener = new DoMath(firstNumber, secondNumber, result); subButton.addActionListener(listener); addButton.addActionListener(listener); frame.pack(); frame.show(); } We don’t need frames in an applet

// DoMath.java import javax.swing.*; import java.awt.event.*; class DoMath implements ActionListener{ DoMath(JTextField first, JTextField second, JTextField result){ inputOne = first; inputTwo = second; output = result; } public void actionPerformed(ActionEvent e){ double first, second; first = Double.parseDouble(inputOne.getText().trim()); second = Double.parseDouble(inputTwo.getText().trim()); if (e.getActionCommand().equals("Add")) output.setText(String.valueOf(first+second)); else output.setText(String.valueOf(first-second)); } private JTextField inputOne, inputTwo, output; } This Listener class remains unchanged

A Calculator using a Java Applet and the DoMath class Here is Berg’s very own web calculator The HMTL page to embedd the JavaApplet

The filesCompiling Running in a browser

Running in the applet viewer Sometimes it is easier to debug an applet using the applet viewer (we don’t see the HTML web page) The web page

Observations: 1.The applet use another host to display information, so we do not need the Jframe component. 2.The container is automatically associated with the viewer since we extended our class and therefore based it on the Japplet. 3.The pack() and the show() methods are not needed. 4.The layout manager still works on the container. 5.The listener classes are still the same and do not need to be recompiled. 6.An applet can be embedded in any webpage and used as applications on the page. The applet will run on any operating system that is java enabeled.