Programming Languages and Techniques (CIS120e)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

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

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

Programming Languages and Techniques (CIS120)

Containers and Components

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

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!

No SVN checkout today. Object-Oriented Design

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

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

Programming Languages and Techniques (CIS120)

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

CS11 Java. Fall Lecture 4

Swing from A to Z Some Simple Components. Preface

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

Nested Loops. A loop can be nested inside another loop.

Programming Languages and Techniques (CIS120e)

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

Using Several Components

Swing: Building GUIs in Java

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

Basicsof. JavaGUI and SWING

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

Layout. Dynamic layout, Swing and general layout strategies

Swing: Building GUIs in Java

Chapter 14. More Swing

CMSC 330: Organization of Programming Languages

Programming Languages and Techniques (CIS120)

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Charlie Garrod Bogdan Vasilescu

Programming Languages and Techniques (CIS120)

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

Programming Languages and Techniques (CIS120)

Hints for Assignment I

Java: Graphical User Interfaces (GUI)

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

2. (True/False) All methods in an interface must be declared public.

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

Programming Languages and Techniques (CIS120)

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

Research opportuni/es with me

Graphical User Interfaces in Java - SWING

CS Exam 1 Review Suggestions

Part I: Learn Common Graphics Components

Jonathan Aldrich Charlie Garrod

The Environment Model

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

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

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

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

Programming Languages and Techniques (CIS120)

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

The Environment Model. Nate Foster Spring 2018

Lecture 19 GUI Events

Programming Languages and Techniques (CIS120)

CHAPTER 1: A GENERAL INTRODUCTION TO PROGRAMMING 1

2IS45 Programming

Graphical User Interface (GUI)

CSE 331 Software Design & Implementation

Array. Prepared By - Rifat Shahriyar

CSCI 201L Midterm Written SOLUTION Fall % of course grade

Window Interfaces Using Swing. Chapter 12

Laying Out Components. What is Widget Layout?

Project Compiler. CS031 TA Help Session November 28, 2011

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

BASICS OF GRAPHICAL APPS

Java.net - the Source for Java(tm) Technology Collaboration

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

Programming Languages and Techniques (CIS120)

Graphical User Interface (GUI)

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

1.00/ Introduction to Computers and Engineering Problem Solving. Quiz 2 / November 5, 2004

Dr. Hikmat A. M. AbdelJaber

G Programming Languages - Fall 2012

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Topic 6: Inner Classes

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Class 16: The Swing Event Model

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Class 27: Nested Classes and an Introduction to Trees

Java Application Development

We are on the GUI fast track path

User interfaces and Swing

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

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

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

Programming Languages and Techniques (CIS120e)

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

FirstSwingFrame.java Page 1 of 1

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

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

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

Object Oriented Design & Patterns. Part 1

Transcription:

Programming Languages and Techniques (CIS120e) Lecture 30 Nov. 22, 2010 Swing II

HW10 Preview

HW10 Available Wednesday; Due Friday, Dec 10 We give you code for a simple Pong game Goal: Build a GUI for a game of your choice Two opqons: Do a few reasonably straighrorward, well- specified extensions of Pong, for up to 70% credit Or, for more points, go a liyle further and design your own game 3

Some ideas... Breakout 4

Some ideas... Space Invaders 5

Reversi / Connect Four Some ideas... 6

Some ideas... Tetris 7

Layouts 8

Flow Layout Items just flow Layout manager places them one afer another Swing s default layout This and following examples taken from The Swing Tutorial 9

Grid Layout Places components in a grid of cells. Each component takes all the available space within its cell, Each cell is exactly the same size. This JPanel contains a Grid (2x3) This JPanel contains another Grid (3x2) 10

Border Layout 11

Include: BoxLayout More SophisQcated Managers CardLayout GridBagLayout GroupLayout SpringLayout These are harder to use when coding by hand BeYer way: Use a layout tool such as NetBeans GUIs that write code for GUIs! 12

Time for some more demos... See DrawingExample1.java 13

Nested Classes

Nested Classes 24

Nested Classes Key idea: Classes can be members of other classes public class Outer { public int outervar; public Outer () { outervar1 = 6; } public class Inner { public int innervar; public Inner(int z) { innervar = outervar+z; } } } Reference from inner class to variable bound in outer class 25

Dynamic vs. Sta>c Nested Classes Two disqnct kinds of nested classes Dynamic nested classes (also called inner classes): class A { class B { } } Sta>c nested classes: class A { static class B { } } 26

StaQc nested classes Not very different from plain old classes Logically group classes that are only used in one place More fine- grained access control than public/private Put definiqons of small classes near where they are used We won t say more Interes>ng and somewhat subtle scope issues... 27

Dynamic Nested Classes ( Inner Classes ) 28

Dynamic Nested Classes (Inner Classes) Useful in situaqons where two objects require deep access to each other s internals Replaces tangled workarounds like owner object (as in the drawing example) SoluQon with inner classes is easier to read No need to allow public access to instance variables of outer class 29

Details Inner class instances cannot be created independently of a containing class instance. Outer.Inner b = new Outer.Inner() Outer a = new Outer(); Outer.Inner b = a.new Inner(); Outer.Inner b = (new Outer()).new Inner(); Inner classes can refer to the instance variables and methods of the outer class 30

Inner Classes at Run Time heap Outer$Inner owner: innervar: 7 Outer$Inner owner: innervar: 4 Outer outervar: 6 Inner class instances with implicit pointers to owning outer class object Outer class instance 31

Anonymous Inner Classes Remember OCaml s anonymous funcqons? map (fun x - > x*3) [1;2;3] Java has something similar: anonymous inner classes Same moqvaqon in both cases: local funcqon definiqons (in OCaml) and inner classes (in Java) are ofen used in only one place so it s silly to give them a name beyer just to just directly write the funcqon/class itself at the point where it s needed! 32

Recall class TimerAction implements ActionListener {!!JButton button;!!timeraction (JButton b) { button = b; }!!public void actionperformed(actionevent e) {!!!Color f = button.getbackground();!!!color b = button.getforeground();!!!button.setforeground(f);!!!button.setbackground(b);!!}! }!! class OnOff6 {!!public OnOff6() {!!!Timer timer = new Timer(1000, new TimerAction(button));!!!timer.start();!!}!!! }!! 33

BeYer class AnonExample {!!public AnonExample() {!!!final JButton button = new JButton("On/Off");!!!!!!!Timer timer = new Timer(1000, new ActionListener() {!!!!public void actionperformed(actionevent e) {!!!!!Color f = button.getbackground();!!!!!color b = button.getforeground();!!!!!button.setforeground(f);!!!!!button.setbackground(b);!!!!}!!!});!!!timer.start();!!}!!! }! 34

Points to note Syntax: new interfacename() {!!!method definitions!!}! Other forms also available, but this is enough for present purposes Local variables (like button here) that are referenced from the inner class must be declared final reasons have to do with the fact that inner classes were added to Java late in its development 35

Back to the demos... See DrawingExample2.java and friends 36