Simple Line Drawing. Description of the Simple Line Drawing program



Similar documents
Part 1 Foundations of object orientation

CaptainCasa. CaptainCasa Enterprise Client. CaptainCasa Enterprise Client. Feature Overview

Welcome to CorelDRAW, a comprehensive vector-based drawing and graphic-design program for the graphics professional.

Instructions for Creating a Poster for Arts and Humanities Research Day Using PowerPoint

CS170 Lab 11 Abstract Data Types & Objects

Tutorials. If you have any questions, comments, or suggestions about these lessons, don't hesitate to contact us at

Lesson 10: Video-Out Interface

Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

GelAnalyzer 2010 User s manual. Contents

Mouse Event Handling (cont.)

Creating a Logo in CorelDRAW

Microsoft Office PowerPoint Creating a new presentation from a design template. Creating a new presentation from a design template

Microsoft Word Quick Reference Guide. Union Institute & University

Create a Poster Using Publisher

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

GUI Components: Part 2

This assignment explores the following topics related to GUI input:

Using Microsoft Word. Working With Objects

Adobe Illustrator CS5 Part 1: Introduction to Illustrator

Microsoft Word defaults to left justified (aligned) paragraphs. This means that new lines automatically line up with the left margin.

Data Visualization. Prepared by Francisco Olivera, Ph.D., Srikanth Koka Department of Civil Engineering Texas A&M University February 2004

Microsoft Excel 2010 Charts and Graphs

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

Chapter 1. Creating Sketches in. the Sketch Mode-I. Evaluation chapter. Logon to for more details. Learning Objectives

DIA Creating Charts and Diagrams

DOING MORE WITH WORD: MICROSOFT OFFICE 2010

Creating a PowerPoint Poster using Windows

Auto Clicker Tutorial

ImageNow Document Management Created on Friday, October 01, 2010

Graphical User Interfaces

First Bytes Programming Lab 2

Programming with Java GUI components

Visual Basic 2010 Essentials

Scatter Plots with Error Bars

Adobe Dreamweaver CC 14 Tutorial

Welcome to CorelDRAW, a comprehensive vector-based drawing and graphic-design program for the graphics professional.

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Microsoft Excel Basics

Creating an invitation

Hierarchical Clustering Analysis

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

How To Use Microsoft Word 2003

NIS-Elements Viewer. User's Guide

TATJA: A Test Automation Tool for Java Applets

HOUR 9. Formatting Worksheets to Look Great

Mobile App Tutorial Animation with Custom View Class and Animated Object Bouncing and Frame Based Animation

In this session, we will explain some of the basics of word processing. 1. Start Microsoft Word 11. Edit the Document cut & move

Graphics Module Reference

The following is an overview of lessons included in the tutorial.

Done. Click Done to close the Capture Preview window.

Basic Microsoft Excel 2007

Unit 21 - Creating a Button in Macromedia Flash

New Perspectives on Creating Web Pages with HTML. Considerations for Text and Graphical Tables. A Graphical Table. Using Fixed-Width Fonts

Introduction to Measurement Tools

Microsoft Excel 2010 Tutorial

Wincopy Screen Capture

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Microsoft Office 2010: Introductory Q&As PowerPoint Chapter 1

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

WEB TRADER USER MANUAL

Sharing Presentations, Documents, and Whiteboards

Get To Know Paint Shop Pro: Layers

How to create buttons and navigation bars

Developing Website Using Tools

Part 3: GridWorld Classes and Interfaces

Excel 2003 Tutorials - Video File Attributes

MyOra 3.5. User Guide. SQL Tool for Oracle. Kris Murthy

Describe the Create Profile dialog box. Discuss the Update Profile dialog box.examine the Annotate Profile dialog box.

Photoshop- Image Editing

Adobe InDesign Creative Cloud

Data Tool Platform SQL Development Tools

Creating Web Pages with Microsoft FrontPage

Making Visio Diagrams Come Alive with Data

Introduction to Autodesk Inventor for F1 in Schools

Mantano Reader for Android

Writer Guide. Chapter 15 Using Forms in Writer

Creating an invitation

MICROSOFT WORD TUTORIAL

Create Charts in Excel

Creating Hyperlinks & Buttons InDesign CS6

TakeMySelfie ios App Documentation

WebEx Sharing Resources

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012


User Tutorial on Changing Frame Size, Window Size, and Screen Resolution for The Original Version of The Cancer-Rates.Info/NJ Application

CXQuotes Doors and Windows Quotation software. Cogitrix

Microsoft Word 2010 Basics

Building and Using Web Services With JDeveloper 11g

MicroStrategy Analytics Express User Guide

Using Parametric Equations in SolidWorks, Example 1

MS Word 2007 practical notes

Creating Fill-able Forms using Acrobat 8.0: Part 1

Fundamentals of Java Programming

Handout: Word 2010 Tips and Shortcuts

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Microsoft Publisher 2010 What s New!

Schoolwires Staff Website Reference Guide

Transcription:

Simple Line Drawing Description of the Simple Line Drawing program JPT Techniques Creating and using the ColorView Creating and using the TextAreaView Creating and using the BufferedPanel with graphics Installing GUI Components Part 2 In this program we introduce the BufferedPanel, the ColorView and the TextAreaView. The BufferedPanel is used to display graphics. The ColorView allows the user to select the of the line before it is drawn. The TextAreaView is used to display the history: the coordinates of the lines already drawn. The program also makes a more extensive use of the TablePanel. Description of the Simple Line Drawing program We build a simple program that will let the user make a simple line drawing (possibly dot-to-dot) and select the of the lines. Two snapshots of the view of the entire GUI are shown below. JPT Tutorial: Simple Line Drawing Page 1

The snapshot illustrates the fact that the history panel automatically adds scrollbars, once it runs out of space to hold all information needed. The line endpoints were chosen at random by the program, to speed up the testing of the program. View This program uses two pairs of TextFieldViews to allow the user to select two sets of endpoints for a line. The ColorView allows the user to select of the line before it is drawn. The TextAreaView is used to display the history: the coordinates of the lines already drawn. The graphics is displayed in a BufferedPanel. Model The internal data model consists of two Point2D.Double objects that represent the two endpoints of the line. These two objects are instantiated every time the draw action is selected. The line is drawn, the history is recorded, the TextFieldViews are set to new values, and the objects die as the action method exits. This represents the Model part of the program. Actions The program uses one ActionsPanel with two associated Actions. The clear Action clears the history, the graphics window, and resets the four TextFieldViews to the original default values used in the constructor. The draw Action extracts the coordinates for the endpoints of a line from the TextFieldViews, extracts the from the ColorView, sets the, draws the lines, and performs several view update operations. It records the current line in the history, makes the current endpoint a suggested start point for the next line, and selects a random point to be the suggested next endpoint. This represents the Action part of the program. GUI The GUI is enclosed in a QuickJPTFrame titled Simple Line Drawing. The SimpleDrawing class extends the DisplayPanel class and uses a BorderLayout to organize its appearance. The left hand side of the GUI contains the ColorView, the actions panel, and the TextAreaView. These are combined in a TablePanel named controls. The ColorView is wrapped in Display to get a title and the TextAreaView that holds the history is installed in a ScrollableDisplay with a title to make sure we can see all of the line coordinates as needed. The two pairs of TextFieldViews that represent the endpoints of a line are installed in two TablePanel with horizontal alignment, and with String annotations to the left. They are combined into a TablePanel named pointdata. The TablePanel named rhs then holds the BufferedPanel installed in a Display titled Graphics, and the pointdata TablePanel. Finally, the TablePanel SimpleDrawing DisplayPanel. controls, and the TablePanel rhs, are installed in the JPT Tutorial: Simple Line Drawing Page 2

The diagram shows how the components fit together - omitting the lowest level text field views: Simple Drawing Display Panel Table Panel Controls Color View Graphics Display Buffered Panel Actions Panel Scrollable Display Text Area View History Point Data Table Panel (HORIZONTAL) JPT Techniques Start Point Table Panel End Point Table Panel JPT Techniques Creating and using the ColorView The ColorView is used to display a selected and allows the user to select a for the graphics either through the use of the JColorChooser dialog, or by typing in the RGB values in the associated text field view. It is a JPT component that implements the Displayable interface. The programmer may choose to create a simple view that contains only a swatch or to instantiate the full view that includes a swatch and a corresponding text field view. The text field view displays the RGB values for the currently selected formatted as comma-separated integers. The user may change the selected by typing the changes into the text field and hitting the RETURN key. Full scale parsing and the corresponding error strategy for the text field view will be activated. The suggestion for the error strategy will be set to the initial default selected by the user in the constructor. The programmer may also select the number of clicks needed by the user to activate the JColorChooser dialog. Additionally, the text field view allows for specifying the in the RGB-Alpha format, where the fourth integer defines the saturation level for the. An example of its use can be found in the Circle Deluxe sample program and tutorial. JPT Tutorial: Simple Line Drawing Page 3

The ColorView is defined in the following statement: // view where user can select the for the drawing private ColorView = new ColorView(Color.red, true); // initial is red // include text field view In the constructor for the GUI we select the click count that activates the chooser: // activate chooser using single click.setchooserclickcount(1); Also in the constructor, we add the view to the GUI, installing it as a wrapped titled display into the controls display collection: // add view to controls controls.add( new DisplayWrapper( new Display(, null, "Color"))); The current selection can be extracted from the view in several ways. The getcolor and setcolor methods use the Java Color class. The demandobject and requestobject methods call the getcolor method and return an XColor object. The getviewstate method first calls demandobject and then it converts the XColor it extracted to a String using the tostringdata method in the XColor class. We extract the from the view inside the draw action in the following manner: // set paint to user choice G.setPaint(.getColor()); where G is the graphics context for the BufferedPanel. We also display the view state of the view in the history panel as a commaseparated string using the getviewstate method as follows: // show the and the line coordinates for history history.append("(" +.getviewstate() + ")\n");... Creating and using the TextAreaView A TextAreaView is a JPT component that implements the Displayable interface and can display several lines of text. If it is installed in a ScrollableDisplay, the scroll bars will automatically appear when needed. The entire contents of the TextAreaView can be extracted with a single getviewstate method call that returns an XString object. The TextAreaView extends the Java Swing JTextArea class and so we can call the append method of JTextArea to add text to the TextAreaView. JPT Tutorial: Simple Line Drawing Page 4

We defined a TextAreaView object as follows: // panel to display past graphed points private TextAreaView history = new TextAreaView(); We set its size using the following two methods in the JTextArea class: // set preferred size for the history text area history.setcolumns(13); history.setrows(18); The TextAreaView is installed in a ScrollableDisplay as follows: // install history panel in a scrollable display // add to controls wrapped with a title controls.add(new DisplayWrapper(new Display( new ScrollableDisplay(history), // installed as scrollable null, // no annotation "Lines drawn"))); // title for the display Finally, we add text to the TextAreaView as follows: // show the and the line coordinates for history history.append("(" +.getviewstate() + ")\n"); history.append("(" + (int)p1.x + ", " + (int)p1.y + ") : "); history.append("(" + (int)p2.x + ", " + (int)p2.y + ")\n"); Creating and using the BufferedPanel with graphics The JPT component used for the display of graphics is a BufferedPanel. The latest graphics content is stored in a buffer and repainted when desired or when Java deems it necessary. As a result, displaying graphics is very pleasing with no flickering. The BufferedPanel comes with an automatically installed mouse listener. This feature will be explored in a subsequent tutorial. Here we only show how it can be used to display simple output graphics. The standard Java Graphics2D package is used for creating the actual graphics content. We declare the BufferedPanel and a graphics context object that will refer to this panel later as follows: // square window for painting private BufferedPanel window = new BufferedPanel(bufferWidth, bufferheight); // graphics context to draw lines and other shapes private Graphics2D G = null; JPT Tutorial: Simple Line Drawing Page 5

We add the BufferedPanel to the GUI in the constructor as follows: // wrap the graphics window into a titled display Display windowdisplay = new Display(window, null, "Graphics"); Finally, in the actions methods, we retrieve the graphics context, install the new graphics objects, and call the repaint() method to show the updated graphics state: // get the graphics context to draw the line Graphics2D G = window.getbuffergraphics(); // set paint to user choice G.setPaint(.getColor()); // line to draw - from P1 to P2 Line2D.Double L = new Line2D.Double(P1, P2); G.draw(L); repaint(); Installing the GUI components Part 2 There are several new ideas in this program. The first is the fact that the TablePanel is used in a more powerful way. When creating the controls panel, the programmer selects the orientation and the alignment. We also see that different types of components can be installed in a TablePanel. // overall control panel for the GUI private TablePanel controls = new TablePanel ( new Object[]{ // array for table contents new Display(, null "Color"), // install titled ColorView actions, // install actions panel new Display( // install history new ScrollableDisplay(history), // make it scrollable null, "Lines Drawn")}, // wrap with title VERTICAL, // table orientation 5, 5, CENTER); // hgap/vgap, alignment Next, the main GUI component is now a DisplayPanel with a BorderLayout. That allows us to install the controls on the WEST and the rhs panel in the CENTER: // class declaration: public class SimpleDrawing extends DisplayPanel // beginning of the constructor: public SimpleDrawing() { // layout for panel as a whole setlayout(new BorderLayout()); JPT Tutorial: Simple Line Drawing Page 6

// adding the highest level components to the main panel: add(controls, BorderLayout.WEST); add(rhs, BorderLayout.CENTER); Finally, we show how the reset() function for the main panel recursively calls the reset functions for all components it contains: public void clear() { // reset all components reset(); // Note: the reset() is called recursively for all components // That means that the following statements will be invoked: // controls.reset(); which will then call //.reset(); // history.reset(); // actions.reset(); // // rhs.reset(); which will then call // window.reset(); // pointdata.reset(); which will then call // startpoint.reset(); // endpoint.reset(); // // to reset all states to their initial defaults } // clear the graphics window window.clearpanel(); repaint(); JPT Tutorial: Simple Line Drawing Page 7

JPT Idioms ColorView ColorView constructor: private ColorView = new ColorView ( Color.red, // initial displayed in the view true); // true = use text field view Color.red true the view identifier Color - initial value that will be displayed in the view and will be used as the default and the suggestion value boolean - use the text field view in addition to the swatch Selecting click count for activating the chooser:.setchooserclickcount(1); the view identifier 1 integer - the number of clicks needed to activate the chooser Extracting Color from the view: Color col =.getcolor(); col identifier for the Color object that will get the the view identifier Extracting from the view in the form of XColor object: XColor xcol =.demandobject(); XColor xcol =.requestobject(); // or xcol identifier for the XColor object that will get the the view identifier Note: only the mandatory model is supported for view Note: suggestion is automatically set to the initial value Extracting from the view in the form of a formatted String: String String =.getviewstate(); String identifier for the String object that will get the formatted string representing the JPT Tutorial: Simple Line Drawing Page 8

the view identifier Setting the form a formatted String in the data model:.setviewstate(r + "," + g + "," + b); r g b the view identifier int - the red shade value int - the green shade value int - the blue shade value Adding a view to a Display: controls.add(new Display(, null, "Color:"))); controls Sum null identifier of a DisplayPanel or a DisplayCollection to which the view is added identifier for the view that is being installed String - the title that will appear on the top of the display border String - the annotation that will appear to the left of the view Alternative way for adding a the view to a Display: controls.add( new DisplayWrapper( new Display(, null, "Color:"))); By using the DisplayWrapper we assure that the view will retain a sensible size when the window where it is contained is changes its size. JPT Tutorial: Simple Line Drawing Page 9

TextAreaView TextAreaView constructor: private TextAreaView history = new TextAreaView(); history identifier for the TextAreaView object Install TextAreaView into a scrollable display: controls.add( new DisplayWrapper( new Display(new ScrollableDisplay(history), null, "History")); controls history "History" identifier for the Display object where we install the scrollable identifier for the TextAreaView object title for the scrollable display Append text to the TextAreaView: history.append("text to be appended"); history "Text..." identifier for the TextAreaView object String that will be appended to the TextAreaView JPT Tutorial: Simple Line Drawing Page 10

BufferedPanel BufferedPanel constructor: private BufferedPanel window = new BufferedPanel(bufferWidth, bufferheight); window bufferwidth bufferheight identifier for the BufferedPanel object integer constant indicating the width in pixels integer constant indicating the height in pixels Adding the BufferedPanel panel to the GUI: Display windowdisplay = new Display(window, null, "Graphics"); windowdisplay identifier for the Display object where the BufferedPanel will be placed window identifier for the BufferedPanel object "Graphics" title for the Display Action methods will use the BufferedPanel by extracting its Graphics2D graphics context and adding graphics objects to it Graphics2D extractor (inside the action method): Graphics2D G = window.getbuffergraphics(); G window identifier for the Graphics2D object identifier for the BufferedPanel object Sample use of the graphics context: The functions defined in the Graphics2D are called to place graphics objects into the BufferedPanel window // set paint to user choice G.setPaint(.getColor()); G identifier for the Graphics2D object identifier for the ColorView object JPT Tutorial: Simple Line Drawing Page 11

// line to draw - from P1 to P2 Line2D.Double L = new Line2D.Double(P1, P2); G.draw(L); G L P1 P2 identifier for the Graphics2D object identifier for the Line2D.Double object that will be displayed identifier for the Point2D.Double object that represents the start point of the line identifier for the Point2D.Double object that represents the end point of the line JPT Tutorial: Simple Line Drawing Page 12