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

Similar documents
Lecture 9. Lecture

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

Graphical User Interfaces (GUIs)

News and info. Array. Feedback. Lab 4 is due this week. It should be easy to change the size of the game grid.

Introduction to the JAVA UI classes Advanced HCI IAT351

H212 Introduction to Software Systems Honors

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

What is Widget Layout? COSC 3461 User Interfaces. Hierarchical Widget Layout. Resizing a Window. Module 5 Laying Out Components

Graphical User Interfaces. Comp 152

CS 251 Intermediate Programming GUIs: Components and Layout

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

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!

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

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

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

Graphical User Interface (GUI)

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

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

Part I: Learn Common Graphics Components

Laying Out Components. What is Widget Layout?

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

Window Interfaces Using Swing Objects

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

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

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

Java: Graphical User Interfaces (GUI)

User interfaces and Swing

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

Programming Languages and Techniques (CIS120)

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

Java. GUI building with the AWT

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

Programmierpraktikum

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

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

GUI Programming. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies

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

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

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Graphical User Interfaces

Window Interfaces Using Swing Objects

Introduction. Introduction

Class 14: Introduction to the Swing Toolkit

Graphical User Interface (GUI)

Agenda. Container and Component

Systems Programming Graphical User Interfaces

Lecture 5: Java Graphics

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

CS 2113 Software Engineering

1.00 Lecture 14. Lecture Preview

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

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

Java Programming Lecture 6

JAVA NOTES GRAPHICAL USER INTERFACES

Dr. Hikmat A. M. AbdelJaber

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

Graphical User Interface (GUI)

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

Containers and Components

Layout. Dynamic layout, Swing and general layout strategies

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

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

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

ITEC 120 4/14/11. Review. Need. Objectives. GUI basics JFrame JPanel JLabel, JButton, JTextField Layout managers. Lecture 38 GUI Interactivity

Unit 6: Graphical User Interface

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

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

Lecture 18 Java Graphics and GUIs

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

Swing from A to Z Some Simple Components. Preface

Final Examination Semester 2 / Year 2010

Basicsof. JavaGUI and SWING

1. Swing Note: Most of the stuff stolen from or from the jdk documentation. Most programs are modified or written by me. This section explains the

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

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

Building Java Programs Bonus Slides

CS11 Java. Fall Lecture 4

CSE 331 Software Design & Implementation

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

MIT AITI Swing Event Model Lecture 17

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

We are on the GUI fast track path

BASICS OF GRAPHICAL APPS

Graphical User Interfaces in Java - SWING

News! Feedback after Lecture 4! About Java and OOP!

CSC 1214: Object-Oriented Programming

2110: GUIS: Graphical User Interfaces

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

To gain experience using GUI components and listeners.

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

Lecture 3: Java Graphics & Events

Global Gomoku Lab 4 in D0010E

Using Several Components

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

EPITA Première Année Cycle Ingénieur. Atelier Java - J5

Transcription:

Lab 4 D0010E Object-Oriented Programming and Design Lecture 9 Lab 4: You will implement a game that can be played over the Internet. The networking part has already been written. Among other things, the lab will contain more advanced GUI programming than Lab 2. The model/view separation will play an important part! 2 Today s lecture GUI programming Components Listeners Layout Model-View-Controller pattern Observer pattern However, first a look at Object and potentially recursive functions. GUI programming in In Lab 2: Reaching new levels Using a listener to handle key events. Drawing simple shapes. Observable/Observer: Separation of model and view. Most of the structure of the GUI was provided. 3 4

GUI programming in general Top-level containers In the beginning, there was Abstract Windowing Toolkit (AWT). Swing was introduced in Java 1.2. Replaces (or rather extends) AWT. Consists of a wide range of different components you can use to build your GUI. Learn the principles, rather than every single class. JFrame, JDialog, and JApplet Each standalone application has at least one top-level container. (JFrame) Every top-level container has a content pane which contains other components. Together, they form a containment hierarchy. 5 6 Example 1: The empty window The empty window import javax.swing.*; public class GUItest { public static void main(string[] args) { JFrame frame = new JFrame("GUItest"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlocation(200,200); frame.setsize(300,150); frame.setvisible(true); } } 7 8

The empty window The empty window looks as a filled rectangular area but is (really) empty. Note the border and the menu bar on top bounding the frame. We can not produce (draw) graphics directly on containers, only on components. Remember: Components are stored in containers. To ease the programming of GUI:s, Java offers a variety of ready-made components. JPanel A JPanel is a component that acts as an empty rectangle. However, It is both a container and a component. 1) We can draw something on a JPanel. Like in lab 2 where LevelGUI drew rooms on a JPanel (a dynamic object of type Display). 2) We can add other components to it. To group components and simplify layout. 9 10 JPanel JPanel content = new JPanel(); frame.setcontentpane(content); This 1) creates a JPanel and 2) sets it as the contentpane of the container frame. If frame is The empty window, adding a JPanel makes it possible to draw graphics in the frame. Other ready-made components JButton JTextField JLabel 11 12

Extending a component LampExample private class Display extends JPanel { public Display(int x, int y) { setbackground(color.white); setpreferredsize(new Dimension(x,y)); } } 13 14 Observable The class representing the data (the model) should extend the class Observable, and will get several methods for free : setchanged() notifyobservers() addobserver(observer o) A dynamic object must call setchange/notifyobservers whenever it has changed. Its method addobserver is available to other objects so they can add observers. Note that the model is observable but not aware of its observers. 15 16

Observer An observer must implement the interface Observer, which means that it must provide an update method. The update method is called automatically whenever the observed object notifies its observers. 17 18 Example: LampGUIExample A window with a lamp and a button l09.gui.examples Main program JFrame Observable LampGUI LampGUIExample JPanel JButton <<interface>> ActionListener <<interface>> Observer LampController LampModel LampView MVC design pattern 19 20

The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again. Layouts What if we would like to arrange the components, instead of just adding them to our display panel? FlowLayoutExample Every container (frame, panel, and so on) has a layout manager that specifies how the components are placed The default layout manager for a JPanel is FlowLayout 21 22 More layout managers GridBagLayout 24

SpringLayout GridBagLayoutExample A layout manager that specifies relationships between the edges of components. Constraints that allow dynamic GUI:s. Example: SpringLayoutExample 4 components as shown below. 25 26 SpringLayout Short demo in Eclipse. NORTH WEST text EAST SOUTH 10 5 10 5 5 label 5 text 5 number 5 button 5 5 panel layout.putconstraint(springlayout.west, text, 5, SpringLayout.EAST, label); 27 28

A second demo in Eclipse. 29