Graphics: Legacy Library

Similar documents
PLT Miscellaneous Libraries: Reference Manual

The Racket Drawing Toolkit

Embedded GUI: Widgets Within Editors

MrLib: Extra GUI Libraries

Browser: Simple HTML Rendering

PLoT: Graph Plotting

FrTime: A Language for Reactive Programs

File: PLT File Format Libraries

FrTime: A Language for Reactive Programs

How to Design Programs Teachpacks

PLoT: Graph Plotting

Plot: Graph Plotting

Readline: Terminal Interaction

File: Racket File Format Libraries

File: Racket File Format Libraries

File: Racket File Format Libraries

Version 5.0. Alexander Friedman and Jamie Raymond. June 6, 2010

Unit 21 - Creating a Navigation Bar in Macromedia Fireworks

Readline: Terminal Interaction

Images. Version February 2, 2012

Racket: Modules, Contracts, Languages

GUI: Racket Graphical Interface Toolkit

Framework: Racket GUI Application Framework

Plot: Graph Plotting

Implementing HtDP Teachpacks, Libraries, and Customized Teaching Languages

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

Introduction To Inkscape Creating Custom Graphics For Websites, Displays & Lessons

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

How to...create a Video VBOX Gauge in Inkscape. So you want to create your own gauge? How about a transparent background for those text elements?

Redex: Practical Semantics Engineering

Do a domain analysis by hand-drawing three or more pictures of what the world program will look like at different stages when it is running.

Images. Version 6.1. August 1, 2014

IDL Tutorial. Working with Images. Copyright 2008 ITT Visual Information Solutions All Rights Reserved

INKSCAPE BASICS. 125 S. Prospect Avenue, Elmhurst, IL (630) elmhurstpubliclibrary.org. Create, Make, and Build

2D Syntax. Version October 30, 2017

Distributed Places. Version 6.3. Kevin Tew. November 20, (require racket/place/distributed)

CISC 1600, Lab 3.1: Processing

Redex: Practical Semantics Engineering

Parser Tools: lex and yacc-style Parsing

XML: Parsing and Writing

Images. Version 6.8. January 24, 2017

CISC 1600, Lab 2.1: Processing

Work with Shapes. Concepts CHAPTER. Concepts, page 3-1 Procedures, page 3-5

File: Racket File and Format Libraries

Recitation 3 Further Work with Dreamweaver and Photoshop: Refining your Web Site

DrRacket Plugins. Version 6.7. Robert Bruce Findler. October 26, package: drracket

Syntax: Meta-Programming Helpers

Parser Tools: lex and yacc-style Parsing

2. If a window pops up that asks if you want to customize your color settings, click No.

GIMP TEXT EFFECTS. Text Effects: Outline Completed Project

Word 2007: Flowcharts Learning guide

S206E Lecture 3, 5/15/2017, Rhino 2D drawing an overview

Tutorial Tracing and Machining Images

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Rich Text Editor with Logger Requirements 1.0 Author: Daniel Riegelhaupt

Images. Version May 8, 2013

ITP 101 Project 2 - Photoshop

GIMP WEB 2.0 ICONS. GIMP is all about IT (Images and Text) OPEN GIMP

Lecture 8: Images. CS 383 Web Development II Monday, February 19, 2018

CMPT-166: Sample Midterm

How to create an animated face

The scan tools are used to convert and edit bitmap images so that they can be cut or routed by the Torchmate.

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

Pl_Editor. August 24, 2017

User's Guide Album Express 7

Programming Languages: Application and Interpretation

All textures produced with Texture Maker. Not Applicable. Beginner.

BMP file format. Contents. Pixel storage. The BMP file format, sometimes called bitmap. or DIB file format (for device-independent

Slideshow: Figure and Presentation Tools

JASCO CANVAS PROGRAM OPERATION MANUAL

(0,0) (600, 400) CS109. PictureBox and Timer Controls

Bridging Graphics. Lori Scarlatos, January 2007

Adobe Photoshop CS2 Reference Guide For Windows

Chapter 3. Texture mapping. Learning Goals: Assignment Lab 3: Implement a single program, which fulfills the requirements:

Color and Pixmaps. Motif Programmer s Guide 1

Programming Project 1

Syntax Color: Utilities

Tutorial 17: Using Visual Image

Renderize Live Overview

Expression Design Lab Exercises

ECE 3331, Dr. Hebert, Summer-3, 2016 HW 11 Hardcopy HW due Tues 07/19 Program due Sunday 07/17. Problem 1. Section 10.6, Exercise 3.

Word 2003: Flowcharts Learning guide

Setup Examples. RTPView Project Program

Vizit Pro User Manual

This document should only be used with the Apple Macintosh version of Splosh.

KaleidaGraph Quick Start Guide

Press-Ready Cookbook Page Guidelines

Open GL Framework For A Computer Graphics Course

InDesign Part II. Create a Library by selecting File, New, Library. Save the library with a unique file name.

Vision Pointer Tools

User Manual Version 1.1 January 2015

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

GOALS [HTDP/2e Chapters 1 through 3.5]

Using Google SketchUp

Model-View-Controller Architecture

Lesson 3 Creating and Using Graphics

By PAD Product Development and Support Team

Introduction to version Instruction date

StitchGraph User Guide V1.8

Motic Images Plus 3.0 ML Software. Windows OS User Manual

Transcription:

Graphics: Legacy Library Version 5.1 February 14, 2011 (require graphics/graphics) The viewport graphics library is a relatively simple toolbox of graphics commands. The library is not very powerful; it is intended as a simplified alternative to scheme/gui s full graphical toolbox. The graphics library originated as SIXlib, a library of X Windows commands available within Chez Scheme at Rice University. The functionality of that library has been reproduced (with backward compatibility) in this version. 1

Contents 1 Basic Commands 4 2 Position Operations 6 3 Color Operations 7 4 Draw, Clear, and Flip Operations 8 4.1 Viewports................................... 8 4.2 Pixels..................................... 9 4.3 Lines..................................... 9 4.4 Rectangles................................... 10 4.5 Ellipses.................................... 11 4.6 Polygons................................... 13 4.7 Strings..................................... 15 4.8 Pixmaps.................................... 15 5 World Operations 17 6 Miscellaneous Operations 18 7 An Example 19 8 A More Complicated Example 20 9 Protecting Graphics Operations 21 10 Mouse Operations 22 11 Keyboard Operations 24 2

12 Flushing 25 13 Graphics Library as a Unit 26 13.1 Signatures................................... 26 13.2 Unit with posn................................ 26 13.3 Unit without posn............................... 26 3

1 Basic Commands (open-graphics) void? Initializes the library s graphics routines. It must be called before any other graphics operations. (close-graphics) void? Closes all of the windows. Until open-graphics is called again, no graphics routines will work. (open-viewport name horiz vert) viewport? name : string? horiz : exact-nonnegative-integer? vert : exact-nonnegative-integer? (open-viewport name dimensions) viewport? name : string? dimensions : posn? Creates a new window called name. The window is horiz pixels wide and vert pixels high. For backward compatibility, a single posn value can be submitted in the place of horiz and vert. The result is a viewport descriptor. (open-pixmap name horiz vert) viewport? name : string? horiz : exact-nonnegative-integer? vert : exact-nonnegative-integer? (open-pixmap name dimensions) viewport? name : string? dimensions : posn? Like open-viewport, but the resulting viewport is not displayed on the screen. Offscreen pixmaps are useful for executing a sequence of drawing commands and displaying them all at once with copy-viewport. Offscreen pixmaps are also useful in conjunction with viewport->snip (see below). This allows functions to compute with graphical objects and view the graphics when results are returned to the interactions window. (close-viewport viewport) void? 4

Removes the viewport from the screen and makes subsequent operations dealing with the viewport illegal. (viewport? v) boolean? v : any/c Returns #t if v is a viewport (i.e., a destination for drawing), #f otherwise. 5

2 Position Operations A position is a pixel location within a viewport. The upper-left corner is pixel (0, 0), and positions increase to the right and down. (struct posn (x y) #:extra-constructor-name make-posn) x : real? y : real? Represents a positions. ((get-pixel viewport) p) (one-of/c 0 1) Returns the color of the pixel at position p in viewport; 0 denotes white and 1 denotes not white. ((get-color-pixel viewport) p) rgb? Returns an rgb value for color of the pixel at position p in viewport. ((test-pixel viewport) color) rgb? color : (or/c (integer-in 0 299) string? rgb?) Returns the color that will actually be used if color is used to draw. 6

3 Color Operations A color can be represented in three ways: as a color index (an integer in 0 to 299, inclusive), as a color name string, or as a rgb value. All drawing functions which take a color argument accept colors in any form. An rgb value is assigned to an index with change-color. (struct rgb (red green blue) #:extra-constructor-name make-rgb) red : (real-in 0 1) green : (real-in 0 1) blue : (real-in 0 1) Takes three values in the range 0 (dark) to 1 (bright) and returns an rgb (a color). (change-color index rgb) void? index : (integer-in 0 299) rgb : rgb? Changes the color at index in the color table to the color specified in rgb. Only the first twenty-one indices are initialized; a color index should not be used until it has been initialized. (default-display-is-color?) boolean? Returns #t if the default display screen for viewports is in color or #f otherwise. 7

4 Draw, Clear, and Flip Operations The following are the basic graphics operations for drawing to a viewport. Each function takes a viewport as its argument and returns a function operating within that viewport. Further arguments, if any, are curried. For example, (draw-line viewport) returns a function, that can then be applied to the proper arguments to draw a line in the viewport corresponding to viewport descriptor viewport. In general, draw- functions make pixels black or colored, clear- functions make them white, and flip- commands invert pixels (which makes black white, white black, and is otherwise ill-defined). 4.1 Viewports ((draw-viewport viewport) [color]) void? color : (or/c (integer-in 0 299) = "black" string? rgb?) Colors the entire contents of viewport with color. ((clear-viewport viewport)) void? Whitens the entire contents of viewport. ((flip-viewport viewport)) void? Inverts the entire contents of viewport. (copy-viewport source dest) void? source : viewport? dest : viewport? Copies the content of source into dest. 8

4.2 Pixels ((draw-pixel viewport) p [color]) void? color : (or/c (integer-in 0 299) = "black" string? rgb?) Colors the pixel in viewport at p. ((clear-pixel viewport) p) void? Whitens the pixel in viewport at p. ((flip-pixel viewport) p) void? Inverts the pixel in viewport at p. 4.3 Lines ((draw-line viewport) p1 p2 [color]) void? p1 : posn? p2 : posn? color : (or/c (integer-in 0 299) = "black" string? rgb?) Draws a line in viewport connecting positions p1 and p2. ((clear-line viewport) p1 p2) void? p1 : posn? p2 : posn? Whitens a line in viewport connecting positions p1 and p2. 9

((flip-line viewport) p1 p2) void? p1 : posn? p2 : posn? Inverts a line in viewport connecting positions p1 and p2. 4.4 Rectangles ((draw-rectangle viewport) p width height [color]) void? width : (and/c real? (not/c negative?)) height : (and/c real? (not/c negative?)) color : (or/c (integer-in 0 299) = "black" string? rgb?) Draws a rectangle border in the viewport with the top-left of the rectangle at the position p and with sides width across and height tall. ((clear-rectangle viewport) p width height) void? width : (and/c real? (not/c negative?)) height : (and/c real? (not/c negative?)) Whitens a rectangle border in the viewport, analogous to draw-rectangle. ((flip-rectangle viewport) p width height) void? width : (and/c real? (not/c negative?)) height : (and/c real? (not/c negative?)) Inverts a rectangle border in the viewport, analogous to draw-rectangle. 10

((draw-solid-rectangle viewport) p width height [color]) void? width : (and/c real? (not/c negative?)) height : (and/c real? (not/c negative?)) color : (or/c (integer-in 0 299) = "black" string? rgb?) Draws a solid rectangle in the viewport with the top-left of the rectangle at the position p and with sides width across and height tall. ((clear-solid-rectangle viewport) p width height) void? width : (and/c real? (not/c negative?)) height : (and/c real? (not/c negative?)) Whitens a rectangle border in the viewport, analogous to draw-solid-rectangle. ((flip-solid-rectangle viewport) p width height) void? width : (and/c real? (not/c negative?)) height : (and/c real? (not/c negative?)) Inverts a rectangle border in the viewport, analogous to draw-solid-rectangle. 4.5 Ellipses ((draw-ellipse viewport) p width height [color]) void? 11

width : (and/c real? (not/c negative?)) height : (and/c real? (not/c negative?)) color : (or/c (integer-in 0 299) = "black" string? rgb?) Draws a ellipse border in the viewport. The ellipse is inscribed with a rectangle whose top-left is at position p and with sides width across and height tall. ((clear-ellipse viewport) p width height) void? width : (and/c real? (not/c negative?)) height : (and/c real? (not/c negative?)) Whitens a ellipse border in the viewport, analogous to draw-ellipse. ((flip-ellipse viewport) p width height) void? width : (and/c real? (not/c negative?)) height : (and/c real? (not/c negative?)) Inverts a ellipse border in the viewport, analogous to draw-ellipse. ((draw-solid-ellipse viewport) p width height [color]) void? width : (and/c real? (not/c negative?)) height : (and/c real? (not/c negative?)) color : (or/c (integer-in 0 299) = "black" string? rgb?) Draws a solid ellipse in the viewport. The ellipse is inscribed with a rectangle whose top-left is at position p and with sides width across and height tall. 12

((clear-solid-ellipse viewport) p width height) void? width : (and/c real? (not/c negative?)) height : (and/c real? (not/c negative?)) Whitens a ellipse border in the viewport, analogous to draw-solid-ellipse. ((flip-solid-ellipse viewport) p width height) void? width : (and/c real? (not/c negative?)) height : (and/c real? (not/c negative?)) Inverts a ellipse border in the viewport, analogous to draw-solid-ellipse. 4.6 Polygons ((draw-polygon viewport) points offset [color]) void? points : (listof posn?) offset : posn? color : (or/c (integer-in 0 299) = "black" string? rgb?) Draws a polygon border in viewport using points for the polygon vertices and offset as an offset added to all points. ((clear-polygon viewport) points offset) void? points : (listof posn?) offset : posn? Whitens a polygon border in viewport, analogous to draw-polygon. 13

((flip-polygon viewport) points offset) void? points : (listof posn?) offset : posn? Inverts a polygon border in viewport, analogous to draw-polygon. ((draw-solid-polygon viewport) points offset [color]) void? points : (listof posn?) offset : posn? color : (or/c (integer-in 0 299) = "black" string? rgb?) Draws a solid polygon in viewport using points for the polygon vertices and offset as an offset added to all points. ((clear-solid-polygon viewport) points offset) points : (listof posn?) offset : posn? void? Whitens a polygon border in viewport, analogous to draw-solid-polygon. ((flip-solid-polygon viewport) points offset) points : (listof posn?) offset : posn? void? Inverts a polygon border in viewport, analogous to draw-solid-polygon. 14

4.7 Strings ((draw-string viewport) p str [color]) void? str : string? color : (or/c (integer-in 0 299) = "black" string? rgb?) Draws a string at a specified location in the viewport. The lower left of the string begins at p. ((clear-string viewport) p str) void? str : string? Whitens a string at a specified location in the viewport. The lower left of the string begins at p. ((flip-string viewport) p str) void? str : string? Inverts a string at a specified location in the viewport. The lower left of the string begins at p. 4.8 Pixmaps (((draw-pixmap-posn file : path-string? type : file [type])) viewport) p [color]) void? (one-of/c unknown unknown/mask gif gif/mask jpeg png png/mask xbm xpm bmp pict) = unknown/mask 15

color : (or/c (integer-in 0 299) string? rgb?) = "black" Draws a pixmap into viewport with its upper left corner at position p. If type is unknown or unknown/mask, then the content of the file is examined to determine the type. All formats are supported on all platforms, except pict which is only supported under Mac OS X. The gif/mask, png/mask, and unknown/mask types draw the bitmap with a transparent background if filename refers to a GIF/PNG file with a transparent background. The argument color is only used when the loaded pixmap is monochrome. In that case, the color is used instead of black in the drawn image. ((draw-pixmap viewport) file p [color]) void? file : path-string? color : (or/c (integer-in 0 299) = "black" string? rgb?) Equivalent to (((draw-pixmap-posn file) viewport) p color). ((save-pixmap viewport) file [type]) void? file : path-string? type : (one-of/c gif jpeg png xbm xpm bmp) = xpm Saves the current content of viewport to file. The type argument determines the kind of file that is written. 16

5 World Operations Every canvas comes with an associated world. A client program can set the world, start the world s clock, stop the world s clock, and deal with tick events (the clock ticks) and keyboard inputs (keyevents). ((init-world viewport) v) void? v : any/c Sets the initial value of viewport s world to v. ((set-on-tick-event viewport) secs update-callback) void? secs : real? update-callback : (any/c. ->. any/c) For viewport, sets update-callback to be invoked to transform the world value every secs seconds. Only one callback is installed at a time. ((stop-tick viewport)) void? Stops updating viewport s world via a callback installed with set-on-tick-event. ((set-on-key-event viewport) key-callback) void? key-callback : (any/c any/c. ->. any/c) Sets key-callback as the function to call whenever a key event is received for viewport. The key-callback is given a key event and the current world, and it produces an updated world. 17

6 Miscellaneous Operations ((get-string-size viewport) str) (list/c real? real?) str : string? Returns the size of str as drawn into viewport as a list of two numbers: width and height. (viewport->snip viewport) (is-a?/c snip%) Returns an object that can be inserted into an editor buffer to display the current image in the viewport. (Subsequent drawing to the viewport does not affect the snip s image.) When snips are the results of computations in the interactions window, DrRacket shows the snip in the interactions window.} (viewport-dc viewport) (is-a?/c dc<%>) Returns an object for direct drawing into viewport s on-screen representation (if any). Mirror all such drawing to the result of (viewport-buffer-dc viewport), too. (viewport-buffer-dc viewport) (is-a?/c dc<%>) Returns an object for direct drawing into viewport s off-screen representation. Mirror all such drawing to the result of (viewport-dc viewport), too. 18

7 An Example (open-graphics) ; nothing appears to happen, but the library is initialized... (define w (open-viewport "practice" 300 300)) ; viewport window appears ((draw-line w) (make-posn 30 30) (make-posn 100 100)) ; line appears (close-viewport w) ; viewport disappears (close-graphics) ; again, nothing appears to happen, but ; unclosed viewports (if any) would disappear 19

8 A More Complicated Example The use of multiple viewports, viewport descriptors, drawing operations for multiple viewports is as easy as the use of a single viewport: (open-graphics) (let* (; w1 and w2 are viewports for different windows [w1 (open-viewport "viewport 1" 300 300)] [w2 (open-viewport "viewport 2" 200 500)] ; d1 and d2 draw lines in different viewports [d1 (draw-line w1)] [d2 (draw-line w2)]) ; draws a line in viewport labeled viewport 1 (d1 (make-posn 100 5) (make-posn 5 100)) ; draws a line in viewport labeled viewport 2 (d2 (make-posn 100 100) (make-posn 101 400))) ; we no longer have access to viewports 1 and 2, ; since their descriptors did not escape the let (close-graphics) ; removes the viewports 20

9 Protecting Graphics Operations To guarantee the proper closing of viewports in cases of errors, especially when a program manages several viewports simultaneously, a programmer should use dynamic-wind: (let ([w (open-viewport "hello" 100 100)]) (dynamic-wind ; what we want to happen first: nothing void ; the main program (errors constrained to this piece) (lambda () (draw-pixel 13)) ; an error ; what we would like to happen, whether the main program ; finishes normally or not (lambda () (close-viewport w)))) 21

10 Mouse Operations The graphics library contains functions that determine where the mouse is, if there are any clicks, etc. The functions get-mouse-click and ready-mouse-click first return a mouse-click descriptor, and then other functions take the descriptor and return the mouse s position, which button was pushed, etc. Mouse clicks are buffered and returned in the same order in which they occurred. Thus, the descriptors returned by get-mouse-click and ready-mouse-click may be from clicks that occurred long before these functions were called. (get-mouse-click viewport) mouse-click? Returns the next mouse click in viewport, waiting for a click if necessary. (ready-mouse-click viewport) (or/c mouse-click? false/c) Returns either a mouse click descriptor or #f if none is available. Unlike get-mouseclick, ready-mouse-click always returns immediately. (ready-mouse-release viewport) (or/c mouse-click? false/c) Returns either a click descriptor from a mouse-release (button-up) event or #f if none is available. (query-mouse-posn viewport) (or/c posn? false/c) Returns either the position of the mouse cursor within viewport or else #f if the cursor is currently outside viewport. (mouse-click-posn mouse-click) posn? mouse-click : mouse-click? Returns the position of the pixel within a viewport where a given mouse click occurred. (left-mouse-click? mouse-click) boolean? mouse-click : mouse-click? Returns #t if the mouse click occurred with the left mouse button, #f otherwise. 22

(middle-mouse-click? mouse-click) boolean? mouse-click : mouse-click? Returns #t if the mouse click occurred with the middle mouse button, #f otherwise. (right-mouse-click? mouse-click) boolean? mouse-click : mouse-click? Returns #t if the mouse click occurred with the right mouse button, #f otherwise. 23

11 Keyboard Operations The graphics library contains functions that report key presses from the keyboard. The functions get-key-press and ready-key-press return a key-press descriptor, and then key-value takes the descriptor and returns a character or symbol (usually a character) representing the key that was pressed. Key presses are buffered and returned in the same order in which they occurred. Thus, the descriptors returned by get-key-press and readykey-press may be from presses that occurred long before these functions were called. (get-key-press viewport) key-press? Returns the next key press in the viewport, waiting for a key press if necessary. (ready-key-press viewport) key-press? Returns the next key press in the viewport or returns #f if none is available. Unlike getkey-press, ready-key-press always returns immediately. (key-value key-press) (or/c character? symbol?) key-press : key-press? Returns a character or special symbol for the key that was pressed. For example, the Enter key generates #\return, and the up-arrow key generates up. For a complete list of possible return values, see get-key-code. 24

12 Flushing (viewport-flush-input viewport) void? Empties all mouse and keyboard events in the input buffer of viewport. 25

13 Graphics Library as a Unit 13.1 Signatures (require graphics/graphics-sig) graphics^ : signature Includes all of the bindings defined earlier in this chapter, except the posn bindings of 2 Position Operations. graphics:posn^ : signature Includes the posn bindings of 2 Position Operations. 13.2 Unit with posn (require graphics/graphics-unit) graphics@ : unit? Imports mred^ and exports both graphics^ and graphics:posn^. 13.3 Unit without posn (require graphics/graphics-posn-less-unit) graphics-posn-less@ : unit? Imports mred^ and graphics:posn^ and exports graphics^. 26