Introduction to Java Applet Programs Section 1.3Intro. to Java Applet Programs: a Greeter Applet Section 3.7Graphical/Internet Java: Applet: An Einstein.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
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.
Corresponds with Chapter 12
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
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 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
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.
Introduction to Java Applet Programs Section 1.3Intro. to Java Applet Programs: a Greeter Applet Section 3.7Graphical/Internet Java: Applet: An Einstein.
GUI programming Graphical user interface-based programming.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
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.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Managers Arranges and lays out the GUI components on a container.
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.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
University of Limerick1 Software Architecture Java Layout Managers.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Computer Science 209 GUIs Model/View/Controller Layouts.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 14 : Swing II King Fahd University of Petroleum & Minerals College of Computer Science.
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.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
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.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 7 A First Look at GUI Applications Layout Managers.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Java Applet What is a Java Applet? How is applet compiled?
Object-Orientated Analysis, Design and Programming
Graphical User Interfaces
Graphical User Interfaces -- GUIs
Swing JComponents.
GUIs Model/View/Controller Layouts
Modern Programming Language Java
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
Graphical user interface-based programming
GUI building with the AWT
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Introduction to Java Applet Programs Section 1.3Intro. to Java Applet Programs: a Greeter Applet Section 3.7Graphical/Internet Java: Applet: An Einstein Calculator Sections 4.5 & 5.4 Graphical/Internet Java: Old MacDonald... Applet 1

Swing Containers 2 Top-Level: ______________________ They create an area of the screen called a _________ or _________ where the graphical output is to take place. import javax.swing.*; class FrameExample { public static void main(String [] args) { JFrame frame = new JFrame("A JFrame"); frame.show(); }

3 Panes: Each of these frames contains layers of containers called _________ (or _________ ). The most important of these is the _____________________. The content pane is what is used to group and position the widgets that actually do the _____________ (i.e., drawing) on the screen, or perform some other action.

4 The content pane can be accessed via the ___________________ method. For example: frame.getContentPane().add(JButton("Press"); JButton creates a button. Constructors can create an unlabeled button, one with text on it, or an icon on it,or both. addActionListener() is used to add a listener to the button that listens for events (e.g., mouse press)

5 Action Listeners: They are used for certain widgets that ________ ______________ ― e.g., buttons. java.awt.event.ActionListener is an interface that demands only one method be implemented: public void actionPerformed(ActionEvent event); The event passed to this method can be safely ignored. For a button, this method will be called when the button is pressed.

6 Layouts: How the components are organized on the content pane ― size and how they are arranged ― is determined by the container's ______________________________. Containers inherit a setLayout() method from the Container class for setting the layout. But several of them have a default layout. ______ provides several standard ones; each of them is a class in the java.awt package.

7 Layout: The simplest AWT layout manager. It's the default layout manager for JPanel. FlowLayout lays out components side-by- side starting near the top of the content pane, working downward as each row is filled. Constructor: Just the default one FlowLayout()

8 Layout: GridLayout lays out components in a grid. Constructor: GridLayout(int rows, int cols) –Parameters are the number of rows and columns in the grid. –Either (but not both) may be 0 to represent "infinity." –Components are added to the content pane row-by-row in the specified grid pattern.

9 Layout: It's the default layout for JApplet and JFrame. BorderLayout divides the content pane into sections: –north, south, east, west, center –Center is the largest; extra space goes here –Some sections may be ignored. Constructor: Default: BorderLayout() Add a component to the a specific region of the content pane by adding a second argument: BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.EAST, BorderLayout.CENTER

Applets vs. Applications 10 Applications are _______________ programs –executed with Java interpreter An applet is a small program that: –can be placed on a _____________ –can be executed by a ________________ or with Java's ______________ –gives web pages "dynamic content" See Greeter2.java and Greeter2.html in §1.3

11 import javax.swing.*; // JApplet, JLabel import java.util.*; // Date public class Greeter extends JApplet { public void init() { Date currentDate = new Date(); String today = currentDate.toString(); JLabel greeting = new JLabel( "Today, " + today + ", we begin our study of applets!" ); getContentPane().add(greeting); }

12 Notes about Applets : Syntax public class ClassName extends JAapplet –Extends JApplet (or Applet ) –Must be public Body –There is no main() method in an applet –JApplet class provides other methods instead –First method executed is the init() method that is not declared static

13 Statements –Declaration statements for Date are no different than in applications. –Label declaration JLabel greeting = new JLabel ( … ) for freestanding strings of text (not part of a button or menu) –Content pane: portion of window where label is added: getContentPane().add(greeting)

Example: Building a Temperature-Conversion Applet (similar to Einstein Applet in §3.7) 14 Problem Scenario: Write a program to read a temperature in Celsius, compute and display the equivalent Fahrenheit temperature. However, instead of the text-based solution or GUI-application from before, use an applet.

15 /* TemperatureApplet.java converts Celsius * temperatures to Fahrenheit. * Author: L. Nyhoff * Date: Nov. 29, 2002 */ import java.awt.*; // BorderLayout import javax.swing.*; // JApplet, JTextArea, JOptionPane public class TemperatureApplet extends JApplet { JTextArea introArea, inputArea, outputArea; public void init() { introArea = new JTextArea("Celsius-to-Fahrenheit Converter"); inputArea = new JTextArea("\n\n\nCelsius\nTemperature:\n"); outputArea = new JTextArea("\n\n\nFahrenheit\nTemperature:\n"); getContentPane().add(introArea, BorderLayout.NORTH); getContentPane().add(inputArea, BorderLayout.CENTER); getContentPane().add(outputArea, BorderLayout.EAST);

16 String TITLE = "Celsius-to-Fahrenheit Conversion"; String celsiusString = JOptionPane.showInputDialog( null, "Please enter the temperature in Celsius: ", TITLE, OptionPane.QUESTION_MESSAGE); inputArea.append(celsiusString); double celsius = Double.parseDouble(celsiusString); double fahrenheit = ((9.0/5.0)*celsius) + 32; outputArea.append(Double.toString(fahrenheit)); } }

17 Applet can be loaded as part of a web page and executed using Java's appletviewer (or by a browser): TemperatureApplet <applet CODEBASE="" CODE="TemperatureApplet.class" WIDTH = 300 HEIGHT = 200> The source. To run the applet: appletviewer TemperatureApplet.html

18 "Just Messing Around..." /* TemperatureApplet1.java converts Celsius * temperatures to Fahrenheit. * Author: L. Nyhoff * Date: Nov. 29, 2002 */ import java.awt.*; // BorderLayout import javax.swing.*; // JApplet, JLabel, JTextArea, JOptionPane public class TemperatureApplet1 extends JApplet { JLabel thermLabel, coldLabel, hotLabel, comfortLabel; JTextArea introArea, inputArea, outputArea; public void init() { thermLabel = new JLabel(new ImageIcon("thermometer.jpg")); hotLabel = new JLabel(new ImageIcon("tempHot.gif")); coldLabel = new JLabel(new ImageIcon("tempCold.gif")); comfortLabel = new JLabel(new ImageIcon("tempNice.gif"));

19 introArea = new JTextArea("Celsius-to-Fahrenheit Converter"); Font boldFont = new Font("Arial", Font.BOLD, 16); introArea.setFont(boldFont); inputArea = new JTextArea("\n\nCelsius\nTemperature:\n"); outputArea = new JTextArea("\n\nFahrenheit\nTemperature:\n"); getContentPane().add(introArea, BorderLayout.NORTH); getContentPane().add(thermLabel, BorderLayout.WEST); getContentPane().add(inputArea, BorderLayout.CENTER); getContentPane().add(outputArea, BorderLayout.EAST); String TITLE = "Celsius-to-Fahrenheit Conversion"; String celsiusString = JOptionPane.showInputDialog( null, "Please enter the temperature in Celsius: ", TITLE, OptionPane.QUESTION_MESSAGE); inputArea.append(celsiusString); double celsius = Double.parseDouble(celsiusString); double fahrenheit = ((9.0/5.0)*celsius) + 32; outputArea.append(Double.toString(fahrenheit)); if (fahrenheit = 90) getContentPane().add(hotLabel, BorderLayout.SOUTH); else getContentPane().add(comfortLabel, BorderLayout.SOUTH); } }

import javax.swing.*; import java.awt.*; public class FlowLayoutExample { public static void main(String[] args) { JFrame f = new JFrame("FlowLayout"); f.getContentPane().setLayout(new FlowLayout()); for (int i = 1; i <= 5; i++) { JButton b = new JButton("" + i); f.getContentPane().add(b); } f.setVisible(true); } 20

import javax.swing.*; import java.awt.*; public class GridLayoutExample { public static void main(String[] args) { JFrame f = new JFrame("GridLayout"); f.getContentPane().setLayout(new GridLayout(2, 3)); for (int i = 1; i <= 6; i++) { JButton b = new JButton("" + i); f.getContentPane().add(b); } f.setVisible(true); } 21

import javax.swing.*; import java.awt.*; import ann.easyio.*; public class BorderLayoutExample { public static void main(String[] args) { Keyboard k = new Keyboard(); JFrame f = new JFrame("BorderLayout"); f.getContentPane().setLayout(new BorderLayout()); JButton b = new JButton("NORTH"); f.getContentPane().add(b, BorderLayout.NORTH); f.show(); k.getChar(); b = new JButton("SOUTH"); f.getContentPane().add(b, BorderLayout.SOUTH); f.show(); k.getChar(); } 22

b = new JButton("CENTER"); f.getContentPane().add(b, BorderLayout.CENTER); f.show(); k.getChar(); b = new JButton("WEST"); f.getContentPane().add(b, BorderLayout.WEST); f.show(); k.getChar(); b = new JButton("EAST"); f.getContentPane().add(b, BorderLayout.EAST); f.show(); } 23

Graphics on GUIs & Applets (§ 6.5) 24 We must, of course, first create a frame (window) on which to paint (draw): ___________ for applets ______________________(an extension of ___________) from the ann.gui package is convenient for GUI apps, because it handles window-closing events

25 The recommended practice is to design a "canvas" on which to paint that is a subclass of ___________, class Canvas extends JPanel {... } and then make this the content pane of the basic frame: __________ panel = new Canvas(...); __________________________ ;

26 Our canvas typically has two attributes — height and width — and the constructor sets their values and usually the background color (to something other than the default gray) class Canvas extends JPanel { public Canvas(int width, int height) { setSize(width, height); myHeight = height; myWidth = width; setBackground(Color.white); } // at least one other method... }

27 One other method that must be added is: public void __________________________ { // statements to paint the component } The object pen of type Graphics (from AWT) is what does the actual painting with methods like (see p. 309): drawArc(), drawLine(), drawOval(), drawRect(), drawString(), fillArc(), fillOval(), fillRect(), translate()

Example: Dutch Flag Applet (§ 6.5) 28 Problem:Build an applet to paint an image of the Dutch flag:

29 /** DutchFlag2.java is an applet that draws a Dutch flag * Output: A graphic representing the flag of the Netherlands. */ import javax.swing.*; // JApplet, JPanel import java.awt.*; // Graphics, Color,... public class DutchFlag2 extends JApplet { private static final int FLAG_WIDTH = 350; private static final int FLAG_HEIGHT = 200; private static final int TITLE_ROWS = 30; public DutchFlag2() { JPanel flagPanel = new FlagOfHolland(FLAG_WIDTH, FLAG_HEIGHT); setContentPane(flagPanel); }

30 public void init() { DutchFlag2 myPicture = new DutchFlag2(); myPicture.setSize(FLAG_WIDTH, FLAG_HEIGHT + TITLE_ROWS); myPicture.setVisible(true); } class FlagOfHolland extends JPanel { public FlagOfHolland(int width, int height) { setSize(width, height); myHeight = height; myWidth = width; // draw white field as background setBackground(Color.white); }

31 public void paintComponent(Graphics pen) { super.paintComponent(pen); myHeight = getHeight(); myWidth = getWidth(); int stripeHeight = myHeight / 3; // draw red stripe pen.setColor(Color.red); pen.fillRect(0, 0, myWidth, stripeHeight); // draw blue stripe pen.setColor(Color.blue); pen.fillRect(0, stripeHeight * 2, myWidth, myHeight); } int myHeight, myWidth; }

32