Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120e)

CIS 120 Final Exam 16 December Name (printed): Pennkey (login id):

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

CIS 120 Final Exam 15 December 2017 SOLUTIONS

H212 Introduction to Software Systems Honors

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

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

Programming Languages and Techniques (CIS120)

G51PRG: Introduction to Programming Second semester Applets and 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!

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphical User Interface (GUI)

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

Jonathan Aldrich Charlie Garrod

Introduction to the JAVA UI classes Advanced HCI IAT351

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Lecture 5: Java Graphics

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

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

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

Programming Languages and Techniques (CIS120)

Packages: Putting Classes Together

COMPSCI 230. Software Design and Construction. Swing

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

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

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

Graphical User Interfaces 2

Programming Languages and Techniques (CIS120)

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

Object-oriented programming in Java (2)

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

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

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

CSC207 Week 4. Larry Zhang

Introduction to concurrency and GUIs

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

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

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 :) )

Lecture 9. Lecture

CS 251 Intermediate Programming GUIs: Components and Layout

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

Lecture 19 GUI Events

Using Several Components

Systems Programming Graphical User Interfaces

Programmierpraktikum

AP CS Unit 11: Graphics and Events

CSE 331 Software Design & Implementation

Lecture 3: Java Graphics & Events

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

MIT AITI Swing Event Model Lecture 17

User interfaces and Swing

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

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

Phase 2: Due 11/28/18: Additional code to add, move and remove vertices in response to user mouse clicks on the screen.

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

GUI Software Architecture

Java: Graphical User Interfaces (GUI)

UI Software Organization

CS11 Java. Fall Lecture 4

Java Programming Lecture 6

Programming Languages and Techniques (CIS120)

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

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

Class 16: The Swing Event Model

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

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

Graphical User Interface (GUI)

Phase 2: Due 05/02/18 The Gui now stores and displays a graph in response to user mouse clicks on the screen.

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

Programming Language Concepts: Lecture 8

CSE 331 Software Design & Implementation

FirstSwingFrame.java Page 1 of 1

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

(Incomplete) History of GUIs

CS415 Human Computer Interaction

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

CS11 Java. Fall Lecture 3

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

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Announcements. 1. Forms to return today after class:

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Graphical User Interfaces 2

CS Exam 1 Review Suggestions

Java Swing Introduction

State Application Using MVC

Midterm assessment - MAKEUP Fall 2010

DM503 Programming B. Peter Schneider-Kamp.

CSCI 136 Written Exam #2 Fundamentals of Computer Science II Spring 2012

Part I: Learn Common Graphics Components

Transcription:

Programming Languages and Techniques (CIS120) Lecture 32 April 9, 2018 Swing I: Drawing and Event Handling Chapter 29

HW8: Spellchecker Available on the web site Due: Tuesday! Announcements Parsing, working with I/O, more practice with collections Final exam: May 2, 3-5PM If you have two finals at the same time, you can postpone one If you have 3 finals on one day, you may postpone the middle one If either case applies to you, add your name to the form on the website Must discuss all other exam conflicts with course instructors

Game project Game Design Proposal Milestone Due: (12 points) Friday April 13 th at NOON!!!! (Should take about 1 hour) Submit on GRADESCOPE TAs will give you feedback over the weekend Final Program Due: (88 points) Wednesday, April 25 th at 11:59pm Submit zipfile online, submission only checks if your code compiles Eclipse is STRONGLY recommended for this project May distribute your game (after the deadline) if you do not use any of our code Grade based on demo with your TA during reading days Grading rubric on the assignment website Recommendation: don t be too ambitious. NO LATE SUBMISSIONS PERMITTED

HW9: Game Project

Swing Java's GUI library

Quiz Have you ever used the Swing library to build a Java app before? 1. Nope 2. No, but I've used a different GUI library in Java 3. Yes, but I didn't really understand how it worked 4. Yes, I'm an expert

Why study GUIs (yet again)? Most common example of eventbased programming Heavy and effective use of OO inheritance Case study in library organization and some advanced Java features Ideas applicable everywhere: Web apps Mobile apps Desktop apps Fun!

Terminology overview GUI (OCaml) Swing Graphics Context Gctx.gctx Graphics Widget type Widget.widget JComponent Basic Widgets button label checkbox JButton JLabel JCheckBox Container Widgets hpair, vpair JPanel, Layouts Events event ActionEvent MouseEvent KeyEvent Event Listener mouse_listener mouseclick_listener (any function of type event -> unit) ActionListener MouseListener KeyListener

Swing practicalities Java library for GUI development javax.swing.* Built on existing library: AWT java.awt.* When there are two versions of something, use Swing s. (e.g., java.awt.button vs. javax.swing.jbutton) Portable The Jxxx version is usually the one you want, rather than xxx. Communicates with underlying OS's native window system Same Java program looks appropriately different when run on PC, Linux, and Mac

Simple Drawing DrawingCanvas.java DrawingCanvasMain.java

Fractal Drawing Demo

Recursive function for drawing private static void fractal(graphics gc, int x, int y, double angle, double len) { if (len > 1) { double af = (angle * Math.PI) / 180.0; int nx = x + (int)(len * Math.cos(af)); int ny = y + (int)(len * Math.sin(af)); gc.drawline(x, y, nx, ny); fractal(gc, nx, ny, angle + 20, len - 8); fractal(gc, nx, ny, angle - 10, len - 8);

How do we draw a picture? In the OCaml GUI HW, we created widgets whose repaint function used the graphics context to draw an image let w_draw : widget = { repaint = (fun (gc:gctx) -> fractal (with_color gc green) 200 450 270 80) ; size = (fun () -> (200,200)); handle = (fun () -> ()) In Swing, we extend from class JComponent

Fundamental class: JComponent Analogue to widget type from GUI project (Terminology: widget == JComponent) Subclasses override methods paintcomponent (like repaint, displays the component) getpreferredsize (like size, calculates the size of the component) Events handled by listeners (don't need to use overriding ) Much more functionality available minimum/maximum size font foreground/background color borders what is visible many more

Simple Drawing Component public class DrawingCanvas extends JComponent { public void paintcomponent(graphics gc) { super.paintcomponent(gc); // set the pen color gc.setcolor(color.green); // draw a fractal tree fractal (gc, 200, 450, 270, 80); // get the size of the drawing panel public Dimension getpreferredsize() { return new Dimension(200,200); How to display this component?

JFrame Represents a top-level window Displayed directly by OS (looks different on Mac, PC, etc.) Contains JComponents Can be moved, resized, iconified, closed public void run() { JFrame frame = new JFrame("Tree"); // set the content of the window to be the drawing frame.getcontentpane().add(new DrawingCanvas()); // make sure the application exits when the frame closes frame.setdefaultcloseoperation(jframe.exit_on_close); // resize the frame based on the size of the panel frame.pack(); // show the frame frame.setvisible(true);

User Interaction

Start Simple: Lightswitch Task: Program an application that displays a button. When the button is pressed, it toggles a lightbulb on and off. Key idea: use a ButtonListener to toggle the state of the "lightbulb"

OnOffDemo The Lightswitch GUI program in Swing.

Display the Lightbulb class LightBulb extends JComponent { private boolean ison = false; public void flip() { ison =!ison; public void paintcomponent(graphics gc) { if (ison) { else { gc.setcolor(color.yellow); gc.setcolor(color.black); gc.fillrect(0, 0, 100, 100); public Dimension getpreferredsize() { return new Dimension(100,100); Remember the private state of the lightbulb Draw the Light bulb here using the graphics context Set the size of the window

Main Class public class OnOff implements Runnable { public void run() { JFrame frame = new JFrame("On/Off Switch"); JPanel panel = new JPanel(); frame.getcontentpane().add(panel); LightBulb bulb = new LightBulb(); panel.add(bulb); JButton button = new JButton("On/Off"); panel.add(button); button.addactionlistener(new ButtonListener(bulb)); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setvisible(true); public static void main(string[] args) { SwingUtilities.invokeLater(new OnOff()); Open frame and make a panel Create bulb and button Start the (Swing) application

Making the Button DO something class ButtonListener implements ActionListener { private LightBulb bulb; public ButtonListener (LightBulb b) { bulb = b; @Override public void actionperformed(actionevent e) { bulb.flip(); bulb.repaint();