Lecture 3: Java Graphics & Events

Similar documents
Lecture 5: Java Graphics

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

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

IT101. Graphical User Interface

Computer Science 210: Data Structures. Intro to Java Graphics

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Graphical User Interfaces (GUIs)

Introduction to the JAVA UI classes Advanced HCI IAT351

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

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.

CS 251 Intermediate Programming GUIs: Event Listeners

2IS45 Programming

AP CS Unit 12: Drawing and Mouse Events

CS11 Java. Fall Lecture 3

Packages: Putting Classes Together

CS 251 Intermediate Programming GUIs: Components and Layout

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

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

Java Programming Lecture 6

G51PRG: Introduction to Programming Second semester Applets and graphics

JFrame In Swing, a JFrame is similar to a window in your operating system

AP CS Unit 11: Graphics and Events

Come & Join Us at VUSTUDENTS.net

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

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

H212 Introduction to Software Systems Honors

Window Interfaces Using Swing Objects

Unit 7: Event driven programming

Programming Language Concepts: Lecture 8

PIC 20A GUI with swing

Example: Building a Java GUI

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

Example: Building a Java GUI

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

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

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Window Interfaces Using Swing Objects

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!

AplusBug dude = new AplusBug(); A+ Computer Science -

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

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Lecture 9. Lecture

Graphical User Interfaces 2

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

SD Module-1 Advanced JAVA

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

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

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

FirstSwingFrame.java Page 1 of 1

SD Module-1 Advanced JAVA. Assignment No. 4

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

CS 11 java track: lecture 3

Graphical User Interface (GUI)

Graphics and Painting

Graphical User Interface (GUI)

AP Computer Science Unit 13. Still More Graphics and Animation.

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

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

To gain experience using GUI components and listeners.

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

Graphics User Defined Forms, Part I

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

Programming graphics

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

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

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Windows and Events. created originally by Brian Bailey

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

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Programming Languages and Techniques (CIS120)

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

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CS11 Java. Fall Lecture 4

2110: GUIS: Graphical User Interfaces

BASICS OF GRAPHICAL APPS

8. Polymorphism and Inheritance

UI Software Organization

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Object-oriented programming in Java (2)

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

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

I/O Framework and Case Study. CS151 Chris Pollett Nov. 2, 2005.

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Chapter 14: Applets and More

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

Swing from A to Z Using Focus in Swing, Part 2. Preface

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

CS108, Stanford Handout #22. Thread 3 GUI

Graphical User Interfaces 2

1.00 Lecture 14. Lecture Preview

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011

Programming Languages 2nd edition Tucker and Noonan"

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

Transcription:

Lecture 3: Java Graphics & Events CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki Text Input Scanner class Constructor: myscanner = new Scanner(System.in); can use file instead of System.in new Scanner(new File( filename )) Read values: myscanner.nextint() -- returns an int myscanner.nextdouble() -- returns a double myscanner.nextline() -- returns String -- to end of line see documentation for more 1 2 For more details See document on course web page associated with lecture. See GUI cheat sheet in documentation and handouts section. Overview Graphical User Interfaces (GUI) JFrame (window), JPanel (grouping) JButton, JTextField, JSlider, JChooser, Graphics Drawing items on the screen Events Generated by mouse actions, button clicks etc. Use MouseListener, MouseMotionListener, ActionListener, etc. to respond 3 1

Graphical User Interfaces (GUIs) JFrame AWT - The Abstract Windowing Toolkit is found in the package java.awt. Heavyweight components. Implemented with native native code written for that particular computer. The AWT library was written in six weeks! Swing Java 1.2 extended AWT with the javax.swing package. Lightweight components Written in Java javax.swing.jframe inherits from java.awt.frame The outermost container in an application. To display a window in Java: create a JFrame set the size set the location set it visible import javax.swing.jframe; Screen Location public class MyFirstGUI extends JFrame{ public MyFirstGUI() { (0,0) Increasing X (1440,0) super("first Frame"); setsize(500, 300); setlocation(100, 100); setvisible(true); public static void main(string[] args) { Increasing Y Assume 1440x900 screen resolution MyFirstGUI mfgui = new MyFirstGUI(); (0,900) (1440,900) 2

Positioning a window Closing a GUI import javax.swing.jframe; import java.awt.dimension; import java.awt.toolkit; public class MySecondGUI extends JFrame{ public MySecondGUI() { super("second Frame"); setsize(500, 300); Toolkit toolkit = gettoolkit(); Dimension size = toolkit.getscreensize(); setlocation(size.width/2 - getwidth()/2, size.height/2 - getheight()/2); setvisible(true); The default operation of the quit button is to set the visibility to false The program does not terminate! setdefaultcloseoperation can be used to control this behavior. mfgui.setdefaultcloseoperation(jframe.exit_on_close); Exits the application using System.exit(0) More options (hide, do nothing, etc). 9 Basic Controls Interactive Displays 3

Graphics Graphics context Create objects you want to draw: Rectangle2D.Double, Line.Double, etc. Constructors take x,y coords and dimensions, but don t actually draw items. All drawing takes place in paint method using a graphics context Triggered implicitly by uncovering window or explicitly by calling repaint method. Adds repaint event to event queue eventually draws it All drawing is done in paint method of component public void paint(graphics g) g is a Graphics context provided by system pen that does the drawing Programmer calls repaint(), not paint!! Need to import classes from java.awt.*, java.geom.*, javax.swing.* See MyGraphicsDemo General Graphics Applications Geometric Objects Create an extension of component (either JPanel, JFrame, or JApplet) and implement paint method in the subclass. See main method of demo to get window to show At start of paint method cast g to Graphics2D to get access to new methods Call repaint() on component every time you make a change. Causes OS to schedule call of paint in event queue Called automatically if window obscured and revealed Objects from classes Rectangle2D.Double, Line2D.Double, etc. from java.awt.geom There are also float versions Constructors take params x, y, width, height, but don t draw object Rectangle2D.Double RoundRectangle2D.Double Ellipse2D.Double Arc2D.Double Line2D.Double, 4

java.awt.color Methods MyGraphicsDemo myobj.setframe(x,y,width,height) : can move object g2.draw(myobj) : gives outline g2.fill(myobj) : gives filled version g2.drawstring( a string,x,y) : draws string Class extends JFrame, which creates window. Constructor calls super with title of window. main method creates object, sets size, visibility, and enables go-away box. paint method creates and draws objects. 5

BorderLayout PostItApplication More sophisticated. JFrame contains two JPanels. JFrame uses BorderLayout, so add controls to JPanel in SOUTH, drawing canvas in CENTER of the JFrame. DrawingCanvas extends JPanel -- contains paint method Note use of ArrayList to hold PostIts. PostIt Class Represents the rectangles being dragged: Contains accessor and mutator methods to allow it to be manipulated by drawing program. Could add features (title bar, go-away box) without affecting PostItApplication code. PostItApplication PostItApplication class responsible for setting up the GUI Responding to button pressed and menu selections Sets up ArrayList of items on canvas. Class has 3 inner classes DrawingCanvas DrawingMouseListener DrawingMouseMotionListener Inner classes have access to private features of containing class 6

Inner Classes DrawingPanel extends JPanel Associates listeners for mouse actions on the panel Responsible for repainting the screen DrawingMouseListener and DrawingMouseMotionListener Responsible for responding to mouse actions by changing the items in the ArrayList. Event-Driven Programming Handling Mouse Events If want program to react to mouse press, click, or release on a component send addmouselistener(mlo) to component (usually in the constructor of the component) See PostItApplication.java For motion or drag, send addmousemotionlistener(mlo) When user presses mouse on a component Computer looks for registered MouseListener for component or its containers. If found, sends mousepressed(evt) to listener Listener object designated as mouse listener must implement MouseListener (& implement mousepressed, mousereleased, & mouseclicked) or extend MouseAdapter (which has default implementations of all 3) Second is easier unless class already extends another. Can only extend one class in Java Similarly, for mouse motion listener implement MouseMotionListener or extend MouseMotionAdapter 7

GUI Objects & Events Similar to handling mouse events, but must also install components in a container. See GUI cheat sheet in Documentation & Handouts. Listeners in PostItApplication Main class (this) is listener for button and choice. Set up when GUI items constructed Special listener objects for mouse actions. Set up by DrawingCanvas since listening for actions on that object. List Operations Review list operations from library interface List in Java 8 documentation. Bailey s List is slightly different. Think about how to implement with array. size, isempty, get, set functions ArrayList See Bailey s ArrayIndexList Similar to Java 8 s ArrayList Instance variables: elts: array instance variable, eltsfilled: number of slots filled. Some operations very cheap: size, isempty, get, set take constant time (no search) Others more expensive 8