User interfaces and Swing

Similar documents
Graphic User Interfaces. - GUI concepts - Swing - AWT

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

G51PRG: Introduction to Programming Second semester Applets and graphics

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

GUI in Java TalentHome Solutions

Introduction to the JAVA UI classes Advanced HCI IAT351

H212 Introduction to Software Systems Honors

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

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!

Computer Science 210: Data Structures. Intro to Java Graphics

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

2IS45 Programming

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

Java. GUI building with the AWT

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

DM503 Programming B. Peter Schneider-Kamp.

Systems Programming Graphical User Interfaces

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

CS2110. GUIS: Listening to Events

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

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

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Programming graphics

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

CHAPTER 2. Java Overview

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

Introduction. Introduction

Chapter 1 GUI Applications

Graphical User Interfaces (GUIs)

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

CS 2113 Software Engineering

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } }

Unit 7: Event driven programming

Dr. Hikmat A. M. AbdelJaber

CSIS 10A Practice Final Exam Solutions

AP CS Unit 11: Graphics and Events

Chapter 14: Applets and More

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

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

CS2110. GUIS: Listening to Events

Graphical User Interfaces 2

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

CS 251 Intermediate Programming GUIs: Components and Layout

Packages: Putting Classes Together

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions

Window Interfaces Using Swing Objects

Java - Applets. public class Buttons extends Applet implements ActionListener

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Chapter 14: Applets and More

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

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

Using Several Components

encompass a group of features for building Graphical User Interfaces (GUI).

FirstSwingFrame.java Page 1 of 1

Java: Graphical User Interfaces (GUI)

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

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

PIC 20A GUI with swing

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

Graphical User Interfaces. Comp 152

Graphical User Interface (GUI)

CS193k, Stanford Handout #3. Swing 1

Chapter 12 Advanced GUIs and Graphics

Agenda. Container and Component

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

Java Coordinate System

AP CS Unit 12: Drawing and Mouse Events

Question 1. Show the steps that are involved in sorting the string SORTME using the quicksort algorithm given below.

Programmierpraktikum

PESIT Bangalore South Campus

Java Applet Basics. Life cycle of an applet:

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2

Class 16: The Swing Event Model

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

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

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Part I: Learn Common Graphics Components

Programming Language Concepts: Lecture 8

Graphical User Interface (GUI)

Java Programming Lecture 6

Window Interfaces Using Swing Objects

Graphical User Interfaces. Swing. Jose Jesus García Rueda

THE UNIVERSITY OF AUCKLAND

Exam: Applet, Graphics, Events: Mouse, Key, and Focus

Programming Languages and Techniques (CIS120)

Lecture 5: Java Graphics

(Incomplete) History of GUIs

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

Lecture 9. Lecture

H212 Introduction to Software Systems Honors

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

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

javax.swing Swing Timer

Transcription:

User interfaces and Swing Overview, applets, drawing, action listening, layout managers. APIs: java.awt.*, javax.swing.*, classes names start with a J. Java Lectures 1

2 Applets public class Simple extends Applet { public void init() {... public void start() {... public void stop() {... public void destroy() {... init() to initialize the applet each time it's loaded (or reloaded). start() to start the applet's execution, such as when the applet's loaded or when the user revisits a page that contains the applet. stop() to stop the applet's execution, such as when the user leaves the applet's page or quits the browser. destroy() to perform a final cleanup in preparation for unloading.

3 Run an applet <HTML> <BODY> <APPLET CODE=SimpleApplet.class WIDTH=200 HEIGHT=100> </APPLET> </BODY> </HTML> file simpleapplet.html appletviewer simpleapplet.html

Applets and standalone applications The user interface is attached to a panel object nested for applets, in a top-level browser, for a swing application: in a top-level frame object. A frame object is a top-level window that provides a title, banner, and methods to manage the appearance and behavior of the window. Java Lectures 4

A simple window the whole is an instance of javax.swing.jframe. container of the window upper left corner, coordinates (200, 100) from the upper left corner of the screen. width: 300 pixels; height: 350 pixels. Java Lectures 5

6 Code of the simple window import javax.swing.jframe; class Window { public static void main(string[] args) { JFrame cadre = new JFrame("Empty"); cadre.setsize(300, 350); cadre.setlocation(200, 100); cadre.show();

7 A small application JButton JPanel JPanel JPanel JFrame

import javax.swing.*; import java.awt.event.*; import java.awt.*; class Container extends JPanel implements ActionListener { JButton show = new JButton("show"); JButton erase = new JButton("erase"); JPanel board = new JPanel(); Container() { setlayout(new BorderLayout(5, 5)); JPanel thebuttons = new Jpanel(); // for the buttons thebuttons.add(show); thebuttons.add(erase); add(thebuttons, BorderLayout.NORTH); board.setpreferredsize(new Dimension(200, 150)); board.setbackground(color.white); add(board, BorderLayout.CENTER); show.addactionlistener(this); erase.addactionlistener(this); Java Lectures... 8

public void actionperformed(actionevent e) { Object source = e.getsource(); if (source == show) { Graphics g = board.getgraphics(); g.setcolor(color.red); g.drawoval(50,20,100,100); g.dispose(); else if (source == erase) { board.repaint(); public static void main(string[] argv) { JFrame myframe = new JFrame(); myframe.setcontentpane(new Container()); myframe.pack(); myframe.show(); // or myframe.setvisible(true); Java Lectures 9

Drawing Every JComponent object contains: a background and a foreground color, public Color getforeground(); public void setforeground(color c); a font, public Font getfont(); public void setfont(font f); a method, public Graphics getgraphics(); Java Lectures 10

11 The Graphics class Magic pencil for a graphical component. It holds a graphic context: the painted component, a color for drawing: setcolor(color c); a font: setfont(font f); a "clipping" rectangle (render area), draw methods, drawrect(), drawline()...

12 Paint repaint() calls the paint() method, which calls the method paintcomponent(), then a method to repaint the borders, then the paint() methods of the subcomponents, down the containment hierarchy.

13 Button: Action listening The interface ActionListener contains only one method declaration public void actionperformed(actionevent e); How button-generated actions are passed to the actionperformed method? trace.addactionlistener(delegate); the delegate object must implement ActionListener, and thus has a method actionperformed.

14 Action listening means: when the button show is clicked, the method actionperformed of delegate is invoked. an ActionEvent object is created when the button is clicked, and passed to actionperformed. Event handling in the actionperformed method: for example, drawing.

15 Events Click a button, Chose an item in a menu, Push the button of the mouse/move the mouse while the pointer is on a component, Type "return" when working in a text area, Close the window of the application, Press any key....

Action listening and event handling Events are "perceived" by the computer passed to the Java program, treating the event, according to the type of event and the concerned component. ActionEvent objects are created, representing the event. Listeners are objects which treat some kind of events, implementing the right interface. they are linked to a certain source (component), they have methods to handle the events. Java Lectures 16

17 Other ActionListeners Many... An adapter class implements empty versions of all its interface's methods. use inner classes. listener1 Source listener2 listener3

18 The event-dispatching thread It does event handling and the different painting operations on the screen, It treats everything sequentially: while an event is treated, the UI is suspended.

19 Another example: Mouse clicks

20 The MouseListener interface This listener allows to treat 5 types of events, public void mousepressed(mouseevent evt); public void mousereleased(mouseevent evt); public void mouseentered(mouseevent evt); public void mouseexited(mouseevent evt); public void mouseclicked(mouseevent evt);

A class for the circles class GraphicCircle { int x,y, radius; GraphicCircle(int radius, int x, int y) { this.radius = radius; this.x = x; this.y = y; public void draw(graphics g) { g.filloval(x - radius, y - radius, 2*radius, 2*radius); Java Lectures 21

22 class TestClicks extends JPanel implements MouseListener { Vector memory = new Vector(); TestClicks() { setpreferredsize(new Dimension(250, 250)); addmouselistener(this); public void mouseclicked(mouseevent evt) { memory.add(new GraphicCircle(15, evt.getx(), evt.gety())); repaint(); public void mousepressed(mouseevent evt) { public void mousereleased(mouseevent evt) { public void mouseentered(mouseevent evt) { public void mouseexited(mouseevent evt) {...

23 public void paintcomponent(graphics g) { super.paintcomponent(g); for (int i = 0; i < memory.size(); i++) ((GraphicCircle)memory.get(i)).draw(g); public static void main(string[] args) { JFrame frame = new JFrame(); frame.setcontentpane(new TestClicks()); frame.setlocation(50, 50); frame.pack(); frame.setvisible(true);

Layout managers java.awt.flowlayout (default for JPanel): from left to right, starting new rows if necessary. Java Lectures 24

25 Layout managers (2) java.awt.borderlayout (default for content pane): five areas available to hold components, north, south, east, west, and center. All extra space is placed in the center area. javax.swing.boxlayout: puts components in a single row or column. It respects the components' requested maximum sizes, and also lets you align components.

26 Layout managers (3) javax.swing.cardlayout: implements an area that contains different components at different times. javax.swing.gridlayout: displays components in a grid. javax.swing.gridbaglayout: aligns components by placing them within a grid of cells, allowing some components to span more than one cell.

27 Some design solution Main advice: separate the user interface from the application. 2 classes, the user interface, the application, implementing the ActionListener.

28 class Delegate implements MouseListener { Jpanel panel; Delegate(JPanel _panel) { this.panel = _panel; public void mouseclicked(mouseevent evt) { panel.addcircle(new GraphicCircle(15, evt.getx(), evt.gety())); repaint(); public void mousepressed(mouseevent evt) { public void mousereleased(mouseevent evt) { public void mouseentered(mouseevent evt) { public void mouseexited(mouseevent evt) {

29 class TestClicks extends JPanel { Vector memory = new Vector(); TestClicks() { setpreferredsize(new Dimension(250, 250)); addmouselistener(new Delegate(this)); public void addcircle(graphiccircle c) { memory.add(c); public void paintcomponent(graphics g) { super.paintcomponent(g); for (int i = 0; i < memory.size(); i++) ((GraphicCircle)memory.get(i)).draw(g);...

30 public static void main(string[] args) { JFrame frame = new JFrame(); frame.setcontentpane(new TestClicks()); frame.setlocation(50, 50); frame.pack(); frame.setvisible(true);