5-1 graphics - People.cs.uchicago.edu
Download
Report
Transcript 5-1 graphics - People.cs.uchicago.edu
Cool Graphics in Java
A picture's worth a thousand words
CS 102-02
Lecture 6-3
May 8, 1998
CS102-02
Lecture 6-3
Agenda
•
•
•
•
•
Programming
Recursion
Painting with Java
Drawing text
Color
May 8, 1998
CS102-02
Lecture 6-3
The AWT Hierarchy
May 8, 1998
CS102-02
Lecture 6-3
Text on Computers
• Text Mode
– Fixed (usually small: 128 or 256 different
characters) character sets
– Sized by characters
• 25 rows of 80 characters
• 25 rows of 40 characters
May 8, 1998
CS102-02
Lecture 6-3
Graphics on Computers
• Graphics
– Graphics modes allow for pixel control
– Resolution (640x480, 800x600,
1280x1024)
– Color depth
• 8-bit gives 256 colors
• 16-bit gives 65,536
• 24-bit (32-bit) gives about 16,777,216
– Refresh rate (Horizontal, vertical)
May 8, 1998
CS102-02
Lecture 6-3
Graphics Trade-Offs
• Graphics can be slow
– Lots of data
• Example: A 320x240 24-bit image is 225K
– Video games are very demanding
• Fine-grained control
– You determine each pixel's color
May 8, 1998
CS102-02
Lecture 6-3
Graphics Screen
0,0
I
n
c
r
e
a
s
i
n
g
Increasing x
y
May 8, 1998
CS102-02
Lecture 6-3
The Graphics Class
• Graphics class is the abstract (why is it
abstract?) base class for all graphics contexts
– Encapsulates state information needed for the
basic rendering operations
•
•
•
•
•
•
•
May 8, 1998
The Component object on which to draw.
A translation origin
Current clip.
The current color.
The current font.
Current logical pixel operation function
Current XOR alternation color
CS102-02
Lecture 6-3
Using Graphics Objects
• The paint() method
– paint() is a Component method
– Draws the Component object
– paint() takes a Graphics object as an
argument
• Need to paint()? Call repaint()!
– Don't need a Graphics object to call
repaint()
May 8, 1998
CS102-02
Lecture 6-3
Repainting
• The repaint() method calls:
update(Graphics g)
paint(Graphics g)
May 8, 1998
CS102-02
Lecture 6-3
Updating the Screen
public void update(Graphics g) {
if (!(peer instanceof LightweightPeer)) {
g.setColor(getBackground());
g.fillRect(0, 0, width, height);
g.setColor(getForeground());
}
paint(g);
}
Why do we need update()?
Create special effects by overloading update()
May 8, 1998
CS102-02
Lecture 6-3
Drawing in Java I
• Looks like text, but it's really graphics
• Drawing in Java
– Coordinates are infinitely thin and lie
between the pixels of the output device
– Pixel-sized pen that hangs down and to the
right of the anchor point on the path
May 8, 1998
CS102-02
Lecture 6-3
Drawing in Java II
• Draw the outline of a figure by
traversing an infinitely thin path between
pixels
• Fill a figure by filling the interior of that
infinitely thin path
• Render horizontal text render the
ascending portion of character glyphs
entirely above the baseline coordinate.
May 8, 1998
CS102-02
Lecture 6-3
Drawing Strings
• drawString(String string, int startX, int
startY)
– Draws the text given by the specified
string, using this graphics context's current
font and color.
May 8, 1998
CS102-02
Lecture 6-3
Drawing Bytes
• drawBytes(byte bytes[],int start, int
howMany, int beginX, int beginY)
– Draws the text given by the specified byte
array, using this graphics context's current
font and color.
May 8, 1998
CS102-02
Lecture 6-3
Drawing Characters
• drawChars(char[], int, int, int, int)
– Draws the text given by the specified
character array, using this graphics
context's current font and color.
May 8, 1998
CS102-02
Lecture 6-3
Colors
• RGB
– Computer displays have three signals
– Various combinations make the many
different colors
• Different monitors have different colors
– Dithering
– Halftones
May 8, 1998
CS102-02
Lecture 6-3
Java's OOP, So...
• Colors are represented by Color objects
– Color objects encapsulate RGB values
• Color(float red, float green, float blue)
• Color(int colorNum)
– Red component is in bits 16-23 of the
argument, the green component is in bits
8-15 of the argument, and the blue
component is in bits 0-7
• Color(int red, int green, int blue)
May 8, 1998
CS102-02
Lecture 6-3
Color Class
• Create colors
• Color constants
black blue
gray
green
orange pink
yellow
cyan
darkGray
lightGray magenta
red
white
• Brighten or darken
• Switch between RGB and HSB (Hue,
Saturation and Brightness)
May 8, 1998
CS102-02
Lecture 6-3