CSC 1214: Object-Oriented Programming

Similar documents
PROGRAMMING LANGUAGE 2

Chapter 4. Swing 18 marks

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Introduction. Introduction

Graphical User Interfaces. Comp 152

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Chapter 13. Applets and HTML. HTML Applets. Chapter 13 Java: an Introduction to Computer Science & Programming - Walter Savitch 1

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

Java. GUI building with the AWT

MIT AITI Swing Event Model Lecture 17

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Part 3: Graphical User Interface (GUI) & Java Applets

Container getcontentpane( ) The add( ) method of Container can be used to add a component to a content pane. Its form is shown here: void add(comp)

Introduction to the JAVA UI classes Advanced HCI IAT351

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

G51PRG: Introduction to Programming Second semester Applets and graphics

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Chapter 12 Advanced GUIs and Graphics

Chapter 17 Creating User Interfaces

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

ASSIGNMENT NO 14. Objectives: To learn and demonstrated use of applet and swing components

SINGLE EVENT HANDLING

Graphics User Defined Forms, Part I

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Agenda. Container and Component

Packages: Putting Classes Together

Part I: Learn Common Graphics Components

Programmierpraktikum

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Original GUIs. IntroGUI 1

Window Interfaces Using Swing Objects

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Swing. By Iqtidar Ali

AP CS Unit 11: Graphics and Events

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Swing Programming Example Number 2

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Introduction to Computer Science I

Day before tests of Java Final test. IDM institution of Bandarawela. Project for department of education

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Java Applet & its life Cycle. By Iqtidar Ali

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Programming graphics

Graphical User Interfaces

Lecture 5: Java Graphics

CHAPTER 2. Java Overview

Java - Applets. public class Buttons extends Applet implements ActionListener

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Swing from A to Z Some Simple Components. Preface

Graphical Applications

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

First Name: AITI 2004: Exam 2 July 19, 2004

Graphical User Interface (GUI)

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Graphic User Interfaces. - GUI concepts - Swing - AWT

Chapter #1. Program to demonstrate applet life cycle

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

H212 Introduction to Software Systems Honors

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 9: Writing Java Applets. Introduction

Window Interfaces Using Swing Objects

CS 209 Programming in Java #13 Multimedia

CoSc Lab # 5 (The Controller)

UNIT-2: CLASSES, INHERITANCE, EXCEPTIONS, APPLETS. To define a class, use the class keyword and the name of the class:

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

JAVA NOTES GRAPHICAL USER INTERFACES

Java Applet Basics. Life cycle of an applet:

Example: CharCheck. That s It??! What do you imagine happens after main() finishes?

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

Graphical User Interfaces (GUIs)

COMP Assignment #10 (Due: Monday, March 11:30pm)

Java Programming Lecture 6

We are on the GUI fast track path

CSC 1214: Object-Oriented Programming

COMPSCI 230. Software Design and Construction. Swing

Mr. Nilesh Vishwasrao Patil. Advanced Java Programming: (17625)

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

User interfaces and Swing

Building Graphical User Interfaces. GUI Principles

Programming Language Concepts: Lecture 8

Information Technology for Industrial Engineers 15 November ISE 582: Information Technology for Industrial Engineers

Lecture 3: Java Graphics & Events

Lecture 9. Lecture

First Name: AITI 2004: Exam 2 July 19, 2004

Graphical User Interface (GUI) and Object- Oriented Design (OOD)

Graphical User Interface (GUI)

Transcription:

CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment: http://muele.mak.ac.ug office: block A, level 3, department of computer science alt. office: institute of open, distance, and elearning, room 2 Applets, Applications, and Swing Kizito (Makerere University) CSC 1214 April, 2018 1 / 18

Overview 1 Applets Introduction Applet Fundamentals 2 Swing The JApplet class Swing Constructors Swing Applet Example 3 GUI Applications Kizito (Makerere University) CSC 1214 April, 2018 2 / 18

Applets Introduction Applets Introduction Previous examples have been Java applications The other type of Java program is the Applet Applets are small applications that are accessed on an Internet server and run as part of a Web document They are used to provide interactive features to web applications that cannot be provided by HTML alone They differ from applications in several key areas They can capture mouse input and also have controls like buttons or check boxes They are also used to create online game collections They are usually written in Java but other languages such as Jython, JRuby, Scala or Eiffel may be used as well Kizito (Makerere University) CSC 1214 April, 2018 3 / 18

Applets Introduction Applets A Simple Applet import java.awt.*; import java.applet.*; public class SimpleApplet extends Applet { public void paint(graphics g) { g.drawstring("a Simple Applet", 20, 20); Output The look depends on your OS and Window Manager Kizito (Makerere University) CSC 1214 April, 2018 4 / 18

Applets Applet Fundamentals Applets Fundamentals The import statement java.awt.* imports the Abstract Windowing Toolkit (AWT) classes The class Applet is contained in the package java.applet Every applet must be a subclass of Applet paint() is a method defined by the AWT, has one parameter of type Graphics, and is called each time the applet must re-display its output The Graphics class defines a method, drawstring() with the following general form: void drawstring(string message, int x, int y) Applet code is compiled in the same way as programs However, running the applet involves a different process There are two ways: 1 Execute the applet within a Web browser 2 Use an applet viewer (e.g., appletviewer) Kizito (Makerere University) CSC 1214 April, 2018 5 / 18

Applets Applet Fundamentals Applet Fundamentals Using a Web browser Write a short HTML file containing the appropriate APPLET tag Example: <applet code="simpleapplet" width=200 height=60> </applet> Then execute your browser and load this file, which causes SimpleApplet to be executed The location of the code option should be relative to the HTML file s. In this case, SimpleApplet.class resides in the same folder as the HTML (usually.html) The width and height statements specify the dimensions of the display area used by the applet. (The tag contains several other options though) Kizito (Makerere University) CSC 1214 April, 2018 6 / 18

Applets Applet Fundamentals Using an applet viewer Option 1 Execute the HTML file (say RunApp.html) as follows: :> appletviewer RunApp.html Option 2 Include a comment at the head of your Java source file containing the APPLET tag and start the viewer with the Java source file 1 import java.awt.*; import java.applet.*; /* <applet code="simpleapplet" width=200 height=60> </applet> */ public class SimpleApplet extends Applet { public void paint(graphics g) { g.drawstring("a Simple Applet", 20, 20); 2 :> appletviewer SimpleApplet.java In summary 1 Edit a Java source file 2 Compile your program 3 Execute the applet viewer (specifying the name of your applet s source file) Kizito (Makerere University) CSC 1214 April, 2018 7 / 18

Applets Applet Fundamentals Using an applet viewer Option 1 Execute the HTML file (say RunApp.html) as follows: :> appletviewer RunApp.html Option 2 Include a comment at the head of your Java source file containing the APPLET tag and start the viewer with the Java source file 1 import java.awt.*; import java.applet.*; /* <applet code="simpleapplet" width=200 height=60> </applet> */ public class SimpleApplet extends Applet { public void paint(graphics g) { g.drawstring("a Simple Applet", 20, 20); 2 :> appletviewer SimpleApplet.java In summary 1 Edit a Java source file 2 Compile your program 3 Execute the applet viewer (specifying the name of your applet s source file) Kizito (Makerere University) CSC 1214 April, 2018 7 / 18

Applets Applet Fundamentals Applet Fundamentals Sumamry Applets do not need a main() method Instead the applet class must inherit Applet from java.applet or JApplet from javax.swing (in the case of a Swing applet) Applets must be run under an applet viewer or a Java-compatible browser User I/O is not accomplished with Java s stream I/O classes. Instead, applets use the interface provided by the AWT Kizito (Makerere University) CSC 1214 April, 2018 8 / 18

Swing Swing A supercharged and flexible alternative to AWT Swing is a set of classes provides more powerful and flexible components than are possible with the AWT It is a set of user interface components that is implemented entirely in Java In additional to the familiar components (e.g., buttons, check boxes, labels), Swing supplies several additions (including tabbed panes, scroll panes, trees, and tables) Even the familiar components such as buttons have more compatibilities in Swing Kizito (Makerere University) CSC 1214 April, 2018 9 / 18

Swing The JApplet class Swing The JApplet class Extends Applet and fundamental to Swing Applets that use Swing must be subclasses of JApplet JApplet is rich with functionality that is not found in Applet For example, JApplet supports various panes, such as the content pane, the glass pane, and the root pane The content pane can be obtained via the method: Container getcontentpane() The add() method of Container can be used to add a component to a content pane: void add(comp) Kizito (Makerere University) CSC 1214 April, 2018 10 / 18

Swing Swing Constructors Swing Icons, Labels, and Buttons Constructors Icons ImageIcon(String filename) ImageIcon(URL url) Labels JLabel(Icon i) Label(String s) JLabel(String s, Icon i, int align) Buttons JButton(Icon i) JButton(String s) JButton(String s, Icon i) Kizito (Makerere University) CSC 1214 April, 2018 11 / 18

Swing Swing Constructors Text Fields JTextField() JTextField(int cols) Swing Text Fields and Check Boxes JTextField(String s, int cols) JTextField(String s) Check Boxes JCheckBox(Icon i) JCheckBox(Icon i, boolean state) JCheckBox(String s) JCheckBox(String s, boolean state) JCheckBox(String s, Icon i) JCheckBox(String s, Icon i, boolean state) Kizito (Makerere University) CSC 1214 April, 2018 12 / 18

Swing Swing Constructors Swing Radio Buttons and Combo Boxes Radio Buttons JRadioButton(Icon i) JRadioButton(Icon i, boolean state) JRadioButton(String s) JRadioButton(String s, boolean state) JRadioButton(String s, Icon i) JRadioButton(String s, Icon i, boolean state) Combo Boxes JComboBox() JComboBox(Vector v) Kizito (Makerere University) CSC 1214 April, 2018 13 / 18

Swing Swing Constructors Swing Tabbed Panes, Scroll Panes, Trees and Tables Tabbed and Scroll Panes JTabbedPane() JScrollPane(Component comp) JScrollPane(int vsb, int hsb) JScrollPane(Component c, int vsb, int hsb) Trees and Tables JTree(Hashtable ht) JTree(Object obj[]) JTree(TreeNode tn) JTree(Vector v) JTable(Object data[][], Object colheads[]) Kizito (Makerere University) CSC 1214 April, 2018 14 / 18

Swing Swing Applet Example Swing JTextField Example import java.awt.*; import javax.swing.*; /* <applet code="jtextfielddemo" width=300 height=50> </applet> */ public class JTextFieldDemo extends JApplet { JTextField jtf; KDE public void init() { // Get content pane Container contentpane = getcontentpane(); contentpane.setlayout(new FlowLayout()); Win 8 // Add text field to content pane jtf = new JTextField(15); contentpane.add(jtf); Kizito (Makerere University) CSC 1214 April, 2018 15 / 18

GUI Applications GUI Applications We can also write GUI applications without extending Applet or JApplet We still make use of AWT and Swing We need to have a way of responding to actions/events (like mouse clicks) when they happen, so we need java.awt.event.* We shall use GUI containers like frames and panels A frame is displayed as a separate window with a title bar it can be repositioned and resized on the screen as needed A panel is a container that cannot be displayed on its own but is used to organize other components A panel must be added to another container to be displayed Recall our AsciiCounter assignment... Kizito (Makerere University) CSC 1214 April, 2018 16 / 18

GUI Applications GUI Applications AsciiCounter.java class AsciiCounter implements Counter { char value = a ; // or a? GUI public void reset() { value = a ; public void increment() { value = (char) (((int)value + 1) % 128); public void decrement() { value = (char) (((int)value - 1)<0? 127: --value); public String valueasstring () { return " "+ value +" ("+ ((int) value) +")" ; Kizito (Makerere University) CSC 1214 April, 2018 17 / 18

GUI Applications GUI Applications AsciiCounterUI.java 1. import java.awt.*; 31. pane.add(valuelbl); 2. import java.awt.event.*; 32. pane.add(inclbl); 3. import javax.swing.*; 33. pane.add(txtbx); 4. 34. pane.add(upbtn); 5. public class AsciiCounterUI 35. pane.add(downbtn); 6. implements ActionListener { 36. pane.add(resetbtn); 7. private Container pane; 37. 8. private JTextField txtbx; 38. upbtn.addactionlistener(this); 9. private JButton upbtn, downbtn, resetbtn; 39. downbtn.addactionlistener(this); 10. private JLabel vallbl, valuelbl, inclbl; 40. resetbtn.addactionlistener(this); 11. private JFrame frame; 41. frame.setvisible(true); 12. private AsciiCounter counter; 42. 13. 14. public AsciiCounterUI(AsciiCounter c) { 43. 44. public void actionperformed (ActionEvent e) { 15. counter = c; 45. JButton b = (JButton) e.getsource(); 16. frame = new JFrame("AsciiCounter"); 46. int inc = Integer.parseInt(txtBx.getText()); 17. frame.setsize(300,150); 47. if (b == resetbtn) frame.setdefaultcloseoperation(jframe.exit ON CLOSE); 48. counter.reset(); 19. pane = frame.getcontentpane(); 49. else 20. pane.setlayout(new GridLayout(4,2)); 50. for (int i=0; i<inc; i++) 21. 51. if (b == upbtn) counter.increment(); 22. vallbl = new JLabel("Counter value: "); 52. else counter.decrement(); 23. valuelbl = new JLabel("a"); 53. valuelbl.settext(counter.valueasstring()); 24. inclbl = new JLabel("Up/Down by: "); 54. 25. upbtn = new JButton("Up"); 55. 26. downbtn = new JButton("Down"); 56. public static void main(string args[]) { 27. resetbtn = new JButton("Reset"); 57. AsciiCounter c = new AsciiCounter(); 28. txtbx = new JTextField("1", 5); 58. AsciiCounterUI cui = new AsciiCounterUI(c); 29. 59. 30. pane.add(vallbl); 60. Kizito (Makerere University) CSC 1214 April, 2018 18 / 18