08 Text Processing

Embed Size (px)

Citation preview

  • 8/12/2019 08 Text Processing

    1/23

    Working With Text

    ISE 108: Introduction to Programming

    SUNY at Stony Brook

    1

  • 8/12/2019 08 Text Processing

    2/23

    Text and StringsGoal: display information besidesshapes and images

    We can do this now with print!"andprintln!"

    Problem: that output only appears atthe bottom of the sketchpad, where

    its hard to read

    Solution: find a way to add text to theprogram window

    22

  • 8/12/2019 08 Text Processing

    3/23

    Fonts and TextBefore we can display any text, weneed to tell Processing what font to

    useAfon!is a combination of:

    a typeface !ex. Times New Roman"

    a style !ex. bold or italic"

    a point size !ex. 48 point"

    33

  • 8/12/2019 08 Text Processing

    4/23

    Fonts and

    ProcessingUse the PFonttype to represent a font

    Processing needs to work with a special

    font format

    We can convert fonts to this format!.vlw"either before or while we run theprogram

    If we create the font first, we need tostore it in the sketchs data folder

    44

  • 8/12/2019 08 Text Processing

    5/23

    .vlw Font CreationUse the Tools menu in Processing

    Choose Create font... from the menu

    Select a font and size from the list

    Creating a font beforehand allows youto work with fonts that the user maynot have installed

    55

  • 8/12/2019 08 Text Processing

    6/23

    Loading FontsTo use a pre#existing .vlw font in aProcessing sketch, use loadFont()

    loadFont()takes the name of thefont file as its argument

    loadFont()returns a PFontobject

    PFont font =

    loadFont("FFScala-32.vlw");

    66

  • 8/12/2019 08 Text Processing

    7/23

    Creating Fonts at

    Run-timeTo create a font while your sketch isrunning, use createFont()

    createFont()requires the font name,the point size, and a boolean !indicatingwhether the font should be smoothed"

    PFont f =

    createFont(Courier, 24, true);

    77

  • 8/12/2019 08 Text Processing

    8/23

    Font Creation

    PitfallsProblem: you can only create a fonton the fly if the user has it installed

    Solutions:

    1.store an original copy of the file !in .ttf

    or .otf"format in the data folder

    2.use PFont.list()to get a list offonts installed on the users computer

    88

  • 8/12/2019 08 Text Processing

    9/23

    Using FontsThe textFont()command tellsProcessing what font to draw with

    This is like fill()or stroke()fordrawings

    textFont()takes a PFontas input

    Processing will use that font until youcall textFont()again to change it

    99

  • 8/12/2019 08 Text Processing

    10/23

    Displaying TextUse the text()command to actuallydisplay text on the screen

    text()uses the font set by textFont()

    text()takes three arguments:

    A String!character sequence"to print

    the x and y coordinates where the textshould be displayed

    1010

  • 8/12/2019 08 Text Processing

    11/23

    // Load the font to use (24-point Courier,

    // with anti-aliasing turned on)

    PFont f = createFont(Courier, 24, true);

    // Tell Processing to use this font

    // from now on

    setFont(f);

    // Display text starting at (25, 50)

    text(Hello, world!, 25, 50);

    11

  • 8/12/2019 08 Text Processing

    12/23

    Aligning TextProcessing lets you change where textis positioned

    textAlign()takes a constant as itsinput:

    Possible values: LEFT, RIGHT, CENTER

    Call textAlign()befor"text()

    Ex. textAlign(CENTER);

    1212

  • 8/12/2019 08 Text Processing

    13/23

    Strings and ThingsThe text()command takes a Stringas its first argument

    A Stringholds a sequence of characters

    Stringis one of the most frequentlyused data types in Processing !and Java"

    Stringhas a number of useful helpermethods

    1313

  • 8/12/2019 08 Text Processing

    14/23

    String MethodsString() creates a new String

    String s = new String(Hello!);

    Shorthand: String s = Hello!;

    This shorthand onlyworks for String!

    length() returns the total number ofcharacters in the String

    1414

  • 8/12/2019 08 Text Processing

    15/23

    String Adjustmentstrim() returns a new String

    with no leading or trailing whitespace

    toLowerCase()/toUpperCase() return a new copyof the Stringin

    lower/uppercase

    All three methods leave the original

    Stringunchanged

    Java strings are immutable

    1515

  • 8/12/2019 08 Text Processing

    16/23

    Extracting Data

    From StringsThe positions in a Stringare

    numbered from 0 to (length - 1)

    charAt() returns the character at

    a given position (index)

    indexOf(str) returns the first

    index at which stroccurs in the string(or -1 if it isnt there)

    1616

  • 8/12/2019 08 Text Processing

    17/23

    Extracting Data

    From Strings, contdsubstring(start, end) returns a

    new Stringcontaining the characters

    from position startup to (but notincluding) end

    You can also call substring()with

    exactly one argument

    In this case, it returns everything fromthe specified index through the end

    1717

  • 8/12/2019 08 Text Processing

    18/23

    String EqualityWhat makes two Strings equal?Do they have the same length? Thesame case? The same characters?

    According to Processing,

    abcdef == abcdef

    is false!they areno!the same".

    What gives?

    1818

  • 8/12/2019 08 Text Processing

    19/23

    Objects & Primitives

    19

    Remember the difference between primitive

    (built-in) types and objectsPrimitive variables hold an actual value

    Object variables (references) only hold theaddress of an object!

    This causes problems when we try tocompare two objects

    19

  • 8/12/2019 08 Text Processing

    20/23

    Processing is

    ShallowProcessing performs shallow comparisonsby default (using the == operator)

    A shallow comparison looks at the valueimmediately associated with a variable

    This is okay for primitive types

    For objects, this means that we compare

    their memory addresses, not their contents!

    Objects are only equal if they live at thesame memory address

    2020

  • 8/12/2019 08 Text Processing

    21/23

    String EqualityStringhas methods to test equality

    based on content, not memory location

    equals() returns trueif twoStrings have the same sequence of

    characters

    Usage: firstString.equals(secondString)

    equals()requires both strings to haveidentical capitalization

    2121

  • 8/12/2019 08 Text Processing

    22/23

    Comparing StringscompareTo() compares two strings

    for their relative lexicographical ordering

    case, then alphabetical, then by length

    Usage: firstString.compareTo(secondString)

    This method returns an integer value

    22

    Result Positive 0 Negative

    Meaning first > second first == second first < second

    22

  • 8/12/2019 08 Text Processing

    23/23

    Dont Be So

    Sensitive!Problem: equals()and compareTo()are case#sensitive

    Sometimes, we only want to compare twoStrings by length and/or characters

    Processing has two more methods for thissituation:

    equalsIgnoreCase()

    compareToIgnoreCase()

    2323