Interactors. Brahm Capoor.

Similar documents
CS 106A, Lecture 23 Interactors and GCanvas

CS 106A, Lecture 24 Interactors and NameSurfer

Assignment 6 YEAH Hours. Ben Barnett and Avery Wang

Classes. How can we manage large programs?

YEAH session #6. 6 March 2014, 5:30p-6:30p Miles Seiver

YEAH Session #6. November 13, 2014, 6-7 PM Nick Troccoli

CS 106A, Lecture 25 Life After CS 106A, Part 1

Solutions to Section #7

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

Friday Four Square Today! Outside Gates at 4:15PM

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

CS 106A Winter 2017 Final Solutions

Example: Building a Java GUI

Swing. By Iqtidar Ali

Example: Building a Java GUI

CS106A Final Exam Review Session. Saturday Dec. 10, 2016 Nick Troccoli

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

CS 106A, Lecture 11 Graphics

AP CS Unit 11: Graphics and Events

UI Software Organization

Graphical User Interface (GUI)

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

Section Handout #7: Interactors and Classes

CS 101 Computer Programming

Adapted from slides by Brahm Capoor. Breakout YEAH hours. Michael (Chung Troute)

Simple Java YEAH Hours. Brahm Capoor and Vrinda Vasavada

Graphic User Interfaces. - GUI concepts - Swing - AWT

Solution to Section #7

Window Interfaces Using Swing Objects

DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

CS 170 Java Programming 1. Week 9: Learning about Loops

Section Handout #7: Interactors and Classes

H212 Introduction to Software Systems Honors

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

2110: GUIS: Graphical User Interfaces

Java Programming Lecture 6

MIT AITI Swing Event Model Lecture 17

Assignment #6 NameSurfer

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

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

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

Programming Lecture 4

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

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

Introduction. Introduction

EVENTS, EVENT SOURCES AND LISTENERS

Programming Lecture 4

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

Event Driven Programming

Events Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

RAIK 183H Examination 2 Solution. November 11, 2013

Graphical User Interfaces. Comp 152

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

CS 106A, Lecture 14 Events and Instance Variables

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

Breakout YEAH hours. Brahm Capoor & Jared Wolens

Programming in the Real World

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

RAIK 183H Examination 2 Solution. November 10, 2014

COMP 102: Test 2 Model Solutions

Answers to Additional Practice Final Problems

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

CS 251 Intermediate Programming GUIs: Event Listeners

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

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

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Class 16: The Swing Event Model

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

Comp Assignment 4: Commands and Graphics

We are on the GUI fast track path

Chapter 12 GUI Basics

Introduction to the JAVA UI classes Advanced HCI IAT351

CS108, Stanford Handout #22. Thread 3 GUI

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

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

Polygons and Angles: Student Guide

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

Design Programming DECO2011

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

Systems Programming Graphical User Interfaces

Lecture 18 Java Graphics and GUIs

PIC 20A GUI with swing

CS11 Java. Fall Lecture 4

YEAH Hours. January , 7-8 PM Jared Wolens

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Quiz 2: April 16, 2004

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

More About Objects and Methods

Chapter 17 Creating User Interfaces

Solutions for Section #2

1 Getting started with Processing

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 2: April 16, 2004

Mehran Sahami Handout #39 CS 106A November 14, 2007 Assignment #6 NameSurfer Due: 3:15pm on Wednesday, November 28th

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

Solutions to Midterm Exam

Transcription:

Interactors Brahm Capoor brahm@stanfordedu

Learning goals for today To learn how to use interactors in our programs

Learning goals for today To learn how to use interactors in our programs To go under the hood of a program

Learning goals for today To learn how to use interactors in our programs To go under the hood of a program To see how we can use Computer Science to understand people

a device that [ ] had about a hundred tiny flat press buttons and a screen about four inches square on which any one of a million pages could be summoned at a moment s notice It looked insanely complicated, and [ ] had the words DON T PANIC printed on it in large friendly letters

a device that [ ] had about a hundred tiny flat press buttons and a screen about four inches square on which any one of a million pages could be summoned at a moment s notice It looked insanely complicated, and [ ] had the words DON T PANIC printed on it in large friendly letters - Douglas Adams, The Hitchhiker s Guide to the Galaxy

How do we interact with programs?

How do we interact with programs? As Console Programs

How do we interact with programs? As Console Programs public class myprogram extends ConsoleProgram {}

How do we interact with programs? As Console Programs Using our Mouse and Keyboard public class myprogram extends ConsoleProgram {}

How do we interact with programs? As Console Programs public class myprogram extends ConsoleProgram {} Using our Mouse and Keyboard public void mousemoved(mouseevent e){}

How do we interact with programs? As Console Programs public class myprogram extends ConsoleProgram {} Using our Mouse and Keyboard public void mousemoved(mouseevent e){} Using UI Elements (buttons, sliders, text fields)

How do we interact with programs? As Console Programs public class myprogram extends ConsoleProgram {} Using our Mouse and Keyboard public void mousemoved(mouseevent e){} Using UI Elements (buttons, sliders, text fields) // \_(ツ)_/

Using the tools we already have, how could we make a button?

What s wrong with this approach to making buttons?

What s wrong with this approach to making buttons? Not a separate part of the interface

What s wrong with this approach to making buttons? Not a separate part of the interface Doesn t give any indication that it was clicked

What s wrong with this approach to making buttons? Not a separate part of the interface Doesn t give any indication that it was clicked Looks pretty bad

What s wrong with this approach to making buttons? Not a separate part of the interface Doesn t give any indication that it was clicked Looks pretty bad Inconsistent with other programs

What s wrong with this approach to making buttons? Not a separate part of the interface Doesn t give any indication that it was clicked Looks pretty bad Inconsistent with other programs Can t use it in ConsolePrograms

What s wrong with this approach to making buttons? Not a separate part of the interface Doesn t give any indication that it was clicked Looks pretty bad Inconsistent with other programs Can t use it in ConsolePrograms Lots of work to create

Making these interfaces would be devastating

Programming is about standing on the shoulders of giants

Meet today s giant JComponent JLabel JTextField JButton JSlider JSpinner JFileChooser JMenuBar JScrollBar JTree

Meet today s giant JComponent JLabel JTextField JButton JSlider JSpinner JFileChooser JMenuBar JScrollBar JTree

JComponents Java handles how they look You handle how they work

Our first JComponents JLabel label = new JLabel( banter ); JTextField field = new JTextField(20); JButton button = new JButton( Click me ); // how do we add these to the window? // 20 characters wide

Regions in a window Java divides every window into 5 regions

Regions in a window Java divides every window into 5 regions Center: your ConsoleProgram or GraphicsProgram CENTER

Regions in a window NORTH Java divides every window into 5 regions Center: your ConsoleProgram or GraphicsProgram The other regions only show up when you add things to them W E S T CENTER SOUTH E A S T

Putting JComponents on the window JLabel label = new JLabel( banter ); JTextField field = new JTextField(20); // 20 characters wide JButton button = new JButton( Click me ); add(label, SOUTH); add(field, SOUTH); add(button, SOUTH); } Java automatically arranges the components in the SOUTH region for you

Let s run() with it

What we know so far Your program starts here run() method called Window appears on the screen mouselisteners start listening

Diving under the hood Your program starts here run() method called Window needs to be set up, or initialized here Window appears on the screen mouselisteners start listening

Diving under the hood Your program starts here run() method called public void init() { Window needs to be set Window appears on the // Set up the window here up, or initialized here screen } mouselisteners start listening

Diving under the hood Your program starts here run() method called init() method called Window appears on the screen mouselisteners start listening

Let s run with init()

Let s run with init() // (sorry)

The takeaway: add JComponents in init()

How to use JComponents

How to use JComponents Where else have our programs had to respond to user actions that could happen anytime?

How to use JComponents Where else have our programs had to respond to user actions that could happen anytime? Like MouseListeners, using components requires Event-Driven programming

How to use JComponents Where else have our programs had to respond to user actions that could happen anytime? Like MouseListeners, using components requires Event-Driven programming public void actionperformed(actionevent e){ String command = egetactioncommand(); // Process command }

public void init() { } public void actionperformed(actionevent e){ String command = egetactioncommand(); // Process command }

public void init() { JButton button = new JButton( Click me! ); add(button, SOUTH); } public void actionperformed(actionevent e){ String command = egetactioncommand(); // Process command }

public void init() { JButton button = new JButton( Click me! ); add(button, SOUTH); addactionlisteners(); // start listening for user actions } public void actionperformed(actionevent e){ String command = egetactioncommand(); // Process command }

public void init() { JButton button = new JButton( Click me! ); add(button, SOUTH); addactionlisteners(); // start listening for user actions } public void actionperformed(actionevent e){ String command = egetactioncommand(); // Process command }

public void init() { JButton button = new JButton( Click me! ); add(button, SOUTH); addactionlisteners(); // start listening for user actions } public void actionperformed(actionevent e){ String command = egetactioncommand(); if (commandequals( Click me! )) { println( Button clicked! ); } }

public void init() { JButton button = new JButton( Click me! ); // TODO: // Set up text field add(button, SOUTH); // TODO: Add text field to window addactionlisteners(); // start listening for user actions } public void actionperformed(actionevent e){ String command = egetactioncommand(); if (commandequals( Click me! )) { println( Button clicked! ); } }

public void init() { JButton button = new JButton( Click me! ); fieldaddactionlistener(this); // enable pressing enter fieldsetactioncommand( Typed ); // set the field s action command add(button, SOUTH); // TODO: Add text field to window addactionlisteners(); // start listening for user actions } public void actionperformed(actionevent e){ String command = egetactioncommand(); if (commandequals( Click me! )) { println( Button clicked! ); } }

public void init() { JButton button = new JButton( Click me! ); fieldaddactionlistener(this); // enable pressing enter fieldsetactioncommand( Typed ); // set the field s action command add(button, SOUTH); add(field, SOUTH); addactionlisteners(); // start listening for user actions } public void actionperformed(actionevent e){ String command = egetactioncommand(); if (commandequals( Click me! )) { println( Button clicked! ); } }

public void init() { JButton button = new JButton( Click me! ); fieldaddactionlistener(this); // enable pressing enter fieldsetactioncommand( Typed ); // set the field s action command add(button, SOUTH); add(field, SOUTH); addactionlisteners(); // start listening for user actions } public void actionperformed(actionevent e){ String command = egetactioncommand(); if (commandequals( Click me! )) { println( Button clicked! ); } // TODO: // Deal with action from field }

public void init() { JButton button = new JButton( Click me! ); fieldaddactionlistener(this); // enable pressing enter fieldsetactioncommand( Typed ); // set the field s action command add(button, SOUTH); add(field, SOUTH); addactionlisteners(); // start listening for user actions } public void actionperformed(actionevent e){ String command = egetactioncommand(); if (commandequals( Click me! )) { println( Button clicked! ); } if (commandequals( Typed )) { println(fieldgettext()); // needs to be an instance variable } }

Diving under the hood Your program starts here run() method called init() method called Window appears on the screen mouselisteners start listening actionlisteners start listening

Let s make something cool!

The xkcd Color survey: what names do people give colors? Dataset: 285 million RGB colors + names navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66

This is a lot of data

A cool visualization!

Milestone 1: How do we represent this data? army green 106, 110, 85 184, 179, 50 96, 117, 3 lilac 145, 110, 244 142, 115, 242 248, 141, 243 hot pink 219, 63, 138 253, 18, 181 239, 1, 152 Hint: each of these is a Color

Milestone 1: How do we represent this data? army green 106, 110, 85 184, 179, 50 96, 117, 3 lilac 145, 110, 244 142, 115, 242 248, 141, 243 hot pink 219, 63, 138 253, 18, 181 239, 1, 152 HashMap<String, ArrayList<Color>> colormap;

Milestone 2: How do we load data from the file? navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 private HashMap<String, ArrayList<Color>> readfile() { // fun Scanner shenanigans }

Milestone 2: How do we load data from the file? navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 private HashMap<String, ArrayList<Color>> readfile() { // fun Scanner shenanigans } Click here for an animation of the file reading

Milestone 3: How do we set up the interactors?

Milestone 4: How do we put all the pieces together? Suppose you have a method private void plotcolor(color color) that puts a single color dot on the screen in the correct place and another method private void clearall() that removes all the colored dots

Computer Science helps us learn about people xkcd's analysis of the results

Overflow slides 1 How do we plot a color? 2 A file reading demo

How do we plot a color in xkcd colors? The HSB Color Space // (you re not required to know this)

How do we plot a color? We normally break colors into a red, green and blue component You can think of each color as a point on a 3d graph with a red, green and blue axis This graph is called the RGB Color Space Each axis on the graph goes from 0 to 255 RGB Color Space Atlas

How do we plot a color? We don t have to break a color into RGB values RGB is easy for computers to understand, but not for humans What does it mean for a color to be 127 / 255 red? \_(ツ)_/ We tend to think of a color in terms of its general color range, how vivid it is, and how bright it is

How do we plot a color? We can also break a color into these three components: The color s hue represents its general color range (its location on the color wheel) The color s saturation represents how vivid it is The color s brightness represents how bright it is Each color is a now a point in the HSB Color Space source

How do we plot a color? Java gives us a method to break a color up into HSB Components: float[] HSBComponents = ColorRGBtoHSB(colorgetRed(), colorgetgreen(), colorgetblue(), null); HSBComponents has three elements, which are in order: 1 color s hue 2 color s saturation 3 color s brightness don t worry about the float data type! (it s like a double, but for smaller numbers)

How do we plot a color? We use the color s hue and saturation to figure out where in the circle the color s point goes We color the point with its corresponding color, which captures brightness e Hu n tio ra tu Sa

How do we plot a color? float[] components = ColorRGBtoHSB(colorgetRed(), colorgetgreen(), colorgetblue(), null); double radius = getradius() * components[1]; double theta = components[0] * MathPI * 20; double x = getwidth() / 20 + radius * Mathcos(theta); double y = getheight() / 20 - radius * Mathsin(theta); GRect pt = new GRect(x, y, 1, 1); ptsetfilled(true); ptsetcolor(color); Get HSB Components from color

How do we plot a color? float[] components = ColorRGBtoHSB(colorgetRed(), colorgetgreen(), colorgetblue(), null); double radius = getradius() * components[1]; double theta = components[0] * MathPI * 20; double x = getwidth() / 20 + radius * Mathcos(theta); double y = getheight() / 20 - radius * Mathsin(theta); GRect pt = new GRect(x, y, 1, 1); ptsetfilled(true); ptsetcolor(color); radius is based on saturation (as a fraction of 100) and angle is based on hue (as a fraction of 360)

How do we plot a color? float[] components = ColorRGBtoHSB(colorgetRed(), colorgetgreen(), colorgetblue(), null); double radius = getradius() * components[1]; double theta = components[0] * MathPI * 20; double x = getwidth() / 20 + radius * Mathcos(theta); double y = getheight() / 20 - radius * Mathsin(theta); GRect pt = new GRect(x, y, 1, 1); ptsetfilled(true); ptsetcolor(color); Calculate exact position of the point on the screen using trigonometry

How do we plot a color? float[] components = ColorRGBtoHSB(colorgetRed(), colorgetgreen(), colorgetblue(), null); double radius = getradius() * components[1]; double theta = components[0] * MathPI * 20; double x = getwidth() / 20 + radius * Mathcos(theta); double y = getheight() / 20 - radius * Mathsin(theta); GRect pt = new GRect(x, y, 1, 1); ptsetfilled(true); ptsetcolor(color); Plot the point on the screen

How colormap is made A more detailed animation back to main slides

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 Scanner sc = new Scanner(new File(COLORS_FILE)); HashMap<String, ArrayList<Color>> result = new HashMap<String, ArrayList<Color>>(); while (schasnextline()) { String colorname = scnextline(); String red = scnextline(); String green = scnextline(); String blue = scnextline(); int r = IntegerparseInt(red); int g = IntegerparseInt(green); int b = IntegerparseInt(blue); Color color = new Color(r, g, b); if (!resultcontainskey(colorname)) { resultput(colorname, new ArrayList<Color>()); } resultget(colorname)add(color); }

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 while (schasnextline()) { result NO KEYS

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 String colorname = scnextline(); while loop variables colorname: result NO KEYS navy blue \

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 String red = scnextline(); while loop variables colorname: red: result NO KEYS navy blue 27

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 String green = scnextline(); while loop variables colorname: red: green: result NO KEYS navy blue 27 34

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 String blue = scnextline(); while loop variables colorname: red: green: blue: result NO KEYS navy blue 27 34 98

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 int r = IntegerparseInt(red); while loop variables colorname: red: green: blue: r: result NO KEYS navy blue 27 34 98 27

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 int g = IntegerparseInt(green); while loop variables colorname: red: green: blue: r: g: result NO KEYS navy blue 27 34 98 27 34

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 int b = IntegerparseInt(blue); while loop variables colorname: red: green: blue: r: g: b: result NO KEYS navy blue 27 34 98 27 34 98

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 Color color = new Color(r, g, b); while loop variables colorname: red: green: blue: r: g: b: result NO KEYS navy blue 27 34 98 27 34 98 color: aaaaa

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 if (!resultcontainskey(colorname)) { resultput(colorname, new ArrayList<Color>()); } while loop variables colorname: red: green: blue: r: g: b: navy blue 27 34 98 27 34 98 result navy blue : {} color: aaaaa

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 resultget(colorname)add(color); while loop variables colorname: red: green: blue: r: g: b: navy blue 27 34 98 27 34 98 result navy blue : {aaaaa} color: aaaaa

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 while (schasnextline()) { result navy blue : {aaaaa}

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 String colorname = scnextline(); while loop variables colorname: blue result navy blue : {aaaaa}

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 String red = scnextline(); while loop variables colorname: red: blue 41 result navy blue : {aaaaa}

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 String green = scnextline(); while loop variables colorname: red: green: blue 41 201 result navy blue : {aaaaa}

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 String blue = scnextline(); while loop variables colorname: red: green: blue: blue 41 201 234 result navy blue : {aaaaa}

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 int r = IntegerparseInt(red); while loop variables colorname: red: green: blue: r: blue 41 201 234 41 result navy blue : {aaaaa}

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 int g = IntegerparseInt(green); while loop variables colorname: red: green: blue: r: g: blue 41 201 234 41 201 result navy blue : {aaaaa}

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 int b = IntegerparseInt(blue); while loop variables colorname: red: green: blue: r: g: b: blue 41 201 234 41 201 234 result navy blue : {aaaaa}

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 Color color = new Color(r, g, b); while loop variables colorname: red: green: blue: r: g: b: blue 41 201 234 41 201 234 result navy blue : {aaaaa} color: aaaaa

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 if (!resultcontainskey(colorname)) { resultput(colorname, new ArrayList<Color>()); } while loop variables colorname: red: green: blue: r: g: b: blue 41 201 234 41 201 234 result navy blue : {aaaaa} blue : {} color: aaaaa

navy blue 27 34 98 blue 41 201 234 lime green 99 212 32 red brown 160 89 66 resultget(colorname)add(color); while loop variables colorname: red: green: blue: r: g: b: blue 41 201 234 41 201 234 color: aaaaa result navy blue : {aaaaa} blue : {aaaaa} back to main slides