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

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

Window Interfaces Using Swing Objects

Introduction to the JAVA UI classes Advanced HCI IAT351

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

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!

Window Interfaces Using Swing Objects

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

CS 251 Intermediate Programming GUIs: Components and Layout

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

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

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

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Lecture 5: Java Graphics

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

PIC 20A GUI with swing

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

Java Programming Lecture 6

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Chapter 12 Advanced GUIs and Graphics

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

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

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

Graphical User Interfaces. Comp 152

Laying Out Components. What is Widget Layout?

Systems Programming Graphical User Interfaces

2110: GUIS: Graphical User Interfaces

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Unit 6: Graphical User Interface

H212 Introduction to Software Systems Honors

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

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

Today. cisc3120-fall2012-parsons-lectiii.3 2

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

Programming graphics

CompSci 230 S Programming Techniques. Basic GUI Components

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

Basicsof. JavaGUI and SWING

Sri Vidya College of Engineering & Technology

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

CS11 Java. Fall Lecture 4

Part I: Learn Common Graphics Components

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

Graphical User Interfaces (GUIs)

Java. GUI building with the AWT

Swing. By Iqtidar Ali

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

Dr. Hikmat A. M. AbdelJaber

INTRODUCTION TO (GUIS)

Agenda. Container and Component

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

Introduction. Introduction

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Containers and Components

No SVN checkout today. Object-Oriented Design

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

Programmierpraktikum

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Lecture 3: Java Graphics & Events

Graphical User Interfaces in Java - SWING

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

DM503 Programming B. Peter Schneider-Kamp.

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

CHAPTER 2. Java Overview

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Resizing a Window. COSC 3461: Module 5B. What is Widget Layout? Size State Transitions. What is Widget Layout? Hierarchical Widget Layout.

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Assignment 2. Application Development

Chapter 12 GUI Basics

Lecture 18 Java Graphics and GUIs

Graphical interfaces & event-driven programming

Widgets. Widgets Widget Toolkits. User Interface Widget

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

Chapter 7: A First Look at GUI Applications

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

CS11 Java. Fall Lecture 3

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

1.1 GUI. JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { // XXX. init() main() public static

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

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

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Together, the appearance and how user interacts with the program are known as the program look and feel.

Chapter 6: Graphical User Interfaces

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

Graphical User Interface (GUI)

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

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation

CS193k, Stanford Handout #3. Swing 1

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

Java: Graphical User Interfaces (GUI)

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

Transcription:

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

Overview Aims To provide an overview of Swing and the AWT To show how to build simple GUIs in Java (more detail next week) Reading Core Java, vol. 1, chapters 7,8, and 9 http://download.oracle.com/javase/tutorial/uiswing/components/index.html http://download.oracle.com/javase/tutorial/2d/index.html 2

Introduction To create a Graphical User Interface We need to put pieces (components) together as part of an interface, e.g. buttons and menus. We need to respond to actions (events) initiated by the user or as part of the working of the system. button press menu selection redraw window event queue notify content change read contents update view process event model update content controller 3

The Java Abstract Window Toolkit (AWT) In Java 1.1, GUIs were constructed using classes in the AWT AWT uses native (peer-based) user-interface elements on the host platform, i.e. uses Windows components on Windows, X11 on Linux, etc. The AWT elements are called heavyweight components Disadvantages: Different peer-based routines on different platforms; Different look-and-feels; Different bugs on different platforms; Lowest common denominator approach to GUI construction. 4

Swing From Java 1.2, the Swing classes can be used for GUI construction. Only rely on two platform specific routines: Display a window; Paint on a window. Components, e.g. buttons and menus, are painted onto blank windows. Called lightweight components because they are written completely in Java. Choice of look-and-feel: Windows, Motif and Metal. Benefits: A better set of user interfaces; Much less platform-dependence; Consistent look-and-feel across platforms. Disadvantage is that drawing a GUI is slower as Java does more work (so native code does less). 5

Components and containers See API documentation for details: http://download.oracle.com/javase/8/docs/api/ AWT-based GUIs are built from containers such as Frame and components such as Buttons Swing-based GUIs are built from containers such as JFrame and components such as JButtons. Using JComponents means we can nest components, which gives greater flexibility when arranging interface components. The simplest GUI that we can build is to just display a single component on the screen 6

Components and containers See API documentation for details: http://download.oracle.com/javase/8/docs/api/ AWT-based GUIs are built from containers such as Frame and components such as Buttons Swing-based GUIs are built from containers such as JFrame and components such as JButtons. Using JComponents means we can nest components, which gives greater flexibility when arranging interface components. The simplest GUI that we can build is to just display a single component on the screen Object Component Container Window Frame JFrame JComponent AbstractButton JButton JMenuItem JMenu JToggleButton JCheckBox JRadioButton... JPanel JTextComponent JTextField JTextArea... AWT Swing 7

Example SimpleJFrame.java SimpleJFrame.java import javax.swing.*; public class SimpleJFrame extends JFrame { public SimpleJFrame() { settitle("a Simple Frame"); setsize(default_width, DEFAULT_HEIGHT); } public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; } public static void main (String[] args) { JFrame frm = new SimpleJFrame(); frm.setdefaultcloseoperation(jframe.exit_on_close); frm.setvisible(true); } 8

Notes on SimpleJFrame A main method is embedded to enable class testing. A top-level window is called a frame in Java. In the AWT the frame class is called Frame; in Swing it is called JFrame (which extends the AWT classes Window and Frame). setvisible( ) must be called before the frame is displayed. setdefaultcloseoperation(jframe.exit_on_close) causes the program to terminate when the window is closed. A JFrame is a container into which we can put other components, so can contain buttons, images, pull-down menus, etc. JFrame is an extension (via Window and Frame) of the generic (but not abstract) AWT superclass Container. See also SimpleFrame.java, which does the same thing, but with AWT rather than Swing. 9

Notes on SimpleJFrame SimpleJFrame is a subclass of JFrame which is given a size and a title. JFrame inherits settitle( ), setsize( ), and setvisible( ) (as well as other methods and fields) from its superclasses. 10

Content of a JFrame A JFrame has several layers: most are rather low level and used to implement the look and feel (menu bar, border, etc.). These layers are called panes. See http://download.oracle.com/javase/tutor ial/uiswing/components/toplevel.html A JFrame is intended to be a container for other objects including text, graphics (images, lines), and GUI controls (buttons, menus etc.) Usual approach is to draw on a panel (JPanel), which is then added to the ContentPane of the JFrame. 11

Content of a JFrame Panels are containers that extend JPanel (in the same way that SimpleFrame extends JFrame). The ContentPane is returned by the getcontentpane() method of JFrame: Container contentpane = myframe.getcontentpane(); JComponent c =... contentpane.add(c); Container is part of the AWT, so code using Container needs to import java.awt.* as well as javax.swing.* 12

SimpleJFrame2 public class SimpleJFrame2 extends JFrame { public SimpleJFrame2() { settitle("a simple frame with content"); setsize(400, 150); Container contentpane = this.getcontentpane(); } // create a drawing that is a MyPanel object // and add to content pane mydrawing = new MyPanel(); contentpane.add(mydrawing); private MyPanel mydrawing; } MyPanel extends JPanel 13

A simple panel public class MyPanel extends JPanel { public MyPanel() { setbackground(color.white); } public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawline(20,20, 120,70); } } To draw anything on a Panel we need to create a class that extends JPanel, and we need to provide a paintcomponent method paintcomponent is automatically called every time the window needs to be redrawn. super.paintcomponent is in JComponent. The Graphics object g that is passed to paintcomponent is an object that is part of the AWT (java.awt.graphics). It is automatically received, and provides a collection of attributes that allow us to display items on our JPanel. See http://download.oracle.com/javase/8/docs/api/java/awt/graphics.html for other methods including drawimage(), drawoval(), drawpolygon() etc. 14

Graphics2D Graphics2D extends Graphics to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout -- http://download.oracle.com/javase/8/docs/api/java/awt/graphics2d.html A Graphics2D object can be easily obtained by casting the Graphics object g that is part of JPanel. public void paintcomponent(graphics g) { super.paintcomponent(g); Graphics2D g2 = (Graphics2D) g; g2.setstroke(new BasicStroke(5)); g2.setpaint(color.red); g2.setfont(new Font("Papyrus", Font.PLAIN, 48)); g2.setrenderinghint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); String text = "Hello World!"; g2.drawstring(text, 40, 80); } Graphics2D provides many, many methods to have fun with, see http://docs.oracle.com/javase/tutorial/2d/index.html 15

Repainting When you would like to change what is being displayed: change a model and request the view to refresh, or invalidate the content of the frame. This will eventually cause a repaint method to be called. Your paint method (or paintcomponent) can be called at any time if your window is covered by another one and subsequently made visible again. 16

Adding interface components to JPanel In Swing, user interface components are added to the JFrame s content pane just like a drawing. JPanels act as containers for other components and are added to a JFrame JButton quitbutton = new JButton("Quit");... Container contentpane = frame.getcontentpane(); JPanel p = new JPanel(); JButton p.add(quitbutton); JPanel contentpane.add(p); Panels are one of the most useful interface components, since you can draw on them and they can act as containers for other components. JFrame 17

SimpleFrameWithQuitButton public class SimpleFrameWithQuitButton extends JFrame implements ActionListener { public SimpleFrameWithQuitButton() { settitle("a Simple Frame with quit button"); setsize(default_width, DEFAULT_HEIGHT); Container contentpane = this.getcontentpane(); JPanel p = new JPanel(); quitbutton = new JButton("Quit"); } quitbutton.addactionlistener(this); p.add(quitbutton); contentpane.add(p); public void actionperformed(actionevent event) { Object source = event.getsource(); // return source object of the event if (source == quitbutton) { System.exit(0); } } Components that do something (e.g. a Button), must be linked to an action. More on this next week. 18

Layout Managers There are also IDE tools that can do layout management,but you need to know how to do it by writing Java code. A layout manager controls arrangement of JComponents within a Container. Layout manager classes conform to the LayoutManager interface. The default layout manager for a JPanel is FlowLayout, with the JComponents aligned in a row in the centre of the JPanel. If the frame is resized, or more buttons are added, the buttons stay centred it is the layout manager that controls this dynamic behaviour. Container.setLayout(LayoutManager mgr); is used to change the layout manager. 19

Other layout managers BorderLayout see later GridLayout aligns components in a rectangular grid of equal-sized spaces. GridBagLayout aligns components vertically and horizontally, without requiring that the components be of the same size. BoxLayout puts components in a single row or column. CardLayout lets you implement an area that contains different components at different times. Also, using inheritance, it is possible to define your own custom layout managers 20

BorderLayout BorderLayout is the default layout manager for the content pane of a JFrame. Position to add components can be controlled: Container contentpane = getcontentpane(); JPanel p = new JPanel();... contentpane.add(p, BorderLayout.SOUTH); Default position for adding components is Center or BorderLayout.CENTER North Edge components are laid out first, then Center Components grow to fill up available space. To add multiple buttons to the South of the frame, we would first add them to a panel using FlowLayout and then add the panel to the South of the frame. West Center South East 21

More components Creating components and adding to a JPanel: JPanel p = new JPanel(); // create component // add to panel contentpane.add(p, BorderLayout.NORTH); Example components include: JLabel a line of text or/and an icon JTextField input a line of text JCheckBox for yes/no (on/off) choices JRadioButton a set of alternatives, only one of which may be on JMenu pull-down hierarchical menus Others see the Java API 22

Radio buttons? JRadioButton a set of alternatives, only one of which may be on Trivia: the Galvin Manufacturing Corporation first used Motorola for its brand of car radio in 1930 and later adopted it as the company name 23

Radio buttons? If you like that, look up computer icons that don t make sense any more 24

Example: GUIComponents1.java JButton JButton JPanel JFrame JRadioButton JPanel JPanel JCheckBox JRadioButton JRadioButton JCheckBox JPanel JLabel JTextField 25

Summary AWT is limited to a few primitive elements supported by all native user interfaces. Swing minimises the use of native methods portable, rich GUI. The JFrame s content pane is the container for other interface components A JPanel is a component that can act as a container, and it can be drawn on. LayoutManagers control where components are placed in a container. There are many kinds of component... Example code on course webpage to experiment with: http://www.dcs.shef.ac.uk/~adam/stuff/com6516/week07_gui_examples.zip 26