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

Similar documents
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.

Come & Join Us at VUSTUDENTS.net

Which of the following syntax used to attach an input stream to console?

Lecture 5: Java Graphics

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


Lecture 3: Java Graphics & Events

Computer Science 210: Data Structures. Intro to Java Graphics

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

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

Event Driven Programming

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Java: Graphical User Interfaces (GUI)

Applet which displays a simulated trackball in the upper half of its window.

The AWT Event Model 9

Graphical User Interfaces 2

Unit 7: Event driven programming

Contents Chapter 1 Introduction to Programming and the Java Language

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

PIC 20A Streams and I/O

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

COURSE DESCRIPTION. John Lewis and William Loftus; Java: Software Solutions; Addison Wesley

AP CS Unit 12: Drawing and Mouse Events

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

Java GUI Design: the Basics

public void mouseexited (MouseEvent e) setminute(getminute()+increment); 11.2 public void mouseclicked (MouseEvent e) int x = e.getx(), y = e.gety();

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Advanced Java Unit 6: Review of Graphics and Events

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Lab 10: Inheritance (I)

G51PRG: Introduction to Programming Second semester Applets and graphics

Java Programming Lecture 6

3 Combining Widgets to create Graphical User Interfaces

Outline. More on the Swing API Graphics: double buffering and timers Model - View - Controller paradigm Applets

User interfaces and Swing

Java for Interfaces and Networks (DT3010, HT10)

Introduction to the JAVA UI classes Advanced HCI IAT351

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

Graphical User Interfaces 2

Advanced Java Programming

Graphical User Interfaces 2

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

Programming graphics

Lecture 11.1 I/O Streams

Advanced Java Programming (17625) Event Handling. 20 Marks

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions

Active Learning: Streams

PIC 20A GUI with swing

Window Interfaces Using Swing Objects

CS2110. GUIS: Listening to Events

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

CS 251 Intermediate Programming GUIs: Event Listeners

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

ECE 462 Object-Oriented Programming using C++ and Java. Key Inputs in Java Games

Graphic User Interfaces. - GUI concepts - Swing - AWT

CS11 Java. Fall Lecture 3

Contents. 18 Algorithms Case study: network search The search algorithm Case study: automatic language recognition 57

GUI in Java TalentHome Solutions

CS 180 Final Exam Review 12/(11, 12)/08

Watch the following for more announcements

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Example: Building a Java GUI

IT101. Graphical User Interface

Programmierpraktikum

CSIS 10A Assignment 14 SOLUTIONS

Example: Building a Java GUI

Chapter 4 Java I/O. X i a n g Z h a n g j a v a c o s q q. c o m

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

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

CS2110. GUIS: Listening to Events

M301: Software Systems & their Development

This page intentionally left blank

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

II 12, JFileChooser. , 2. SolidEllipse ( 2), PolyLine.java ( 3). Draw.java

CS 251 Intermediate Programming Java I/O Streams

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

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

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Window Interfaces Using Swing Objects

Java Coordinate System

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

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

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

Aggregation. Chapter 8. Java By Abstraction Chapter 8. Outline. 8.1 What is Aggregation? Copyright 2006 Pearson Education Canada Inc.

CSE 219 COMPUTER SCIENCE III BEHAVIORAL DESIGN PATTERNS SLIDES COURTESY: RICHARD MCKENNA, STONY BROOK UNIVERSITY

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Implementing Graphical User Interfaces

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

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

Windows and Events. created originally by Brian Bailey

Transcription:

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

Outline Character Streams Random Access Files Design case study Planning Iterations

Character Streams Java internally represents strings as Unicode which uses two bytes/char. Externally, text files are stored in a variety of different formats depending on the machine s locale. Here a locale means a geographic region which generally shares the same language. The default locale here in California is ISO-8859-1. Also known as ASCII. Character based I/O streams perform conversions between Unicode and locale characters when reading and writing strings. This is in contrast to the byte streams we talked about last day which are not locale sensitive.

More on Character Streams Two abstract classes Reader and Writer are at the root of the inheritance hierarchy for character-based I/O. Reader -- supports read(), read(ca), read(ca,off,len), close() Writer -- supports write(c), write(ca), write(ca,off,len), close() InputStreamReader and OutputStreamReader are two filter that serve as a bridge to make a stream into a reader: Ex: InputStreamReader isr =new InputStreamReader(new FileInputStream( hi.txt )); /* another constructor takes a stream and an encoding name*/ Other Reader s include FileReader and BufferedReader Other Writer s include FileWriter, BufferedWriter,and PrintWriter. The last supports print and println which are very useful.

Random Access Files Random access files support both reading and writing data at any position in a file. The Java class that encapsulates this ability is RandomAccessFile. The constructor is RandomAccessFile(filename, mode) where mode is a string r (for read-only), rw (read-write), rws, or rwd (rw with force-writing). This class implements DataInput and DataOutput as well as supports the methods: seek(l) to move to the lth byte from the start of the file skipbytes(i) to move forward or backward with the file i bytes (backward if negative).

Design case study Chapter 9 in the book consists of an extensive case study of a drawing pad programming. This study illustrates the iterative development process so we will go through it in some detail.

Planning It is decided in the planning phase that the program will use Swing rather than AWT. The following requirements were decided upon. The drawing tool should support: Scribbling and drawing various shapes Saving drawings to files and loading the drawings from files Typing from the keyboard Choosing fonts and colors

Iterations The plan to develop this project is divided into six iterations, each of which adds functionalities to the previous iteration. The iterations make heavy use of design patterns to keep the code flexible enough to make it easy to go from one iteration to the next. The iterations are: 1. Create a simple scribble pad, consisting of only a canvas for scribbling 2. Add support for saving, loading, a menu bar, and dialogs. 3. Refactor to support various tools for different shapes. 4. Add tools for lines, rectangles, and ovals. 5. Refactor and tools for filled versions of (4). 6. Add tools for drawing text.

Iteration 1 The application class for this iteration is Scribble. This extends JFrame and has on it a ScribbleCanvas which extends JPanel. The ScribbleCanvas has a ScribbleCanvasListener on it that implements MouseListener and MouseMotionListerner.

ScribbleCanvas Has fields mousebuttondown (boolean); x, y -- the location of the mouse in the canvas; and listener -- for the ScribbleCanvasListener. MouseListener needs: mousepressed, mousereleased, mouseentered, mouseexited, mouseclicked implemented. MouseMotionListener needs: mousedragged and mousemoved. ScribbleCanvasListener gives blank implementations to all but mousepressed, mousereleased, and mousedragged. The desired behavior for scribbling is: a stroke begins when a mouse button is pressed, it continues when the mouse is dragged, and it ends when the mouse is released.

Example ScribbleCanvasListener Methods public void mousepressed(mouseevent e) { Point p = e.getpoint(); canvas.mousebuttondown = true; canvas.x =p.x; canvas.y =p.y; } public void mousereleased(mouseevent e) { canvas.mousebuttondown = false; } public void mousedragged(mouseevent e) { Point p =e.getpoint(); if(canvas.mousebuttondown) { canvas.getgraphics().drawline(canvas.x, canvas.y, p.x, p.y); canvas.x =p.x; canvas.y=p.y; } }

Iteration 2 For this iteration we need: to be able to store drawings so that they can be redrawn to be able to save the drawing into files and load them build a menu bar use file dialogs create a dialog box for selecting colors

Strokes The original draw program draws strokes to the screen but does not store them internally. In this iteration, ScribbleCanvas has a ArrayList of Stroke objects on it called strokes. It also keeps track of a curstroke and a curcolor. A Stroke has an ArrayList of Point s called points and a color. A Stroke supports the methods setcolor(), getcolor(), addpoint(), getpoints()

New ScribbleCanvas To the original ScribbleCanvas the following methods are added: setcurcolor() getcurcolor() startstroke() --invoked by the listener when a stroke is started (calls Stroke constructor) addpointtostroke() -- invoked by the listener to append a point to the current stroke endstroke() -- adds the current stroke to the list of Stroke s paint() -- drawing is repainted onto canvas based on strokes stored internally newfile(), savefile(), openfile() -- are for saving/loading

Scribble Constructor/ Menubar public Scribble() { settitle( Scribble Pad ); canvas = new ScribbleCanvas(); getcontentpane().setlayout(new BorderLayout); getcontentpane().add(createmenubar(), BorderLayout.NORTH); getcontentpane().add(canvas, BorderLayout.CENTER); // rest of constructor } protected JMenuBar createmenubar() { JMenuBar menubar = new JMenuBar(); JMenu menu = new JMenu( File ); JMenuItem mi = new JMenuItem( New ); menubar.add(menu); menu.add(mi); mi.addactionlistener(new NewFileListener()); // rest of method }

NewFileListener class NewFileListener implements ActionListener { public void actionperformed(actionevent e) { newfile(); } }