190 likes | 208 Vues
Cool Graphics in Java. A picture's worth a thousand words. CS 102-02 Lecture 6-3. Agenda. Programming Recursion Painting with Java Drawing text Color. The AWT Hierarchy. Text on Computers. Text Mode Fixed (usually small: 128 or 256 different characters) character sets
E N D
Cool Graphics in Java A picture's worth a thousand words CS 102-02 Lecture 6-3
Agenda • Programming • Recursion • Painting with Java • Drawing text • Color
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
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)
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
Graphics Screen Increasing x 0,0 Increasing y
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 • 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
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()
Repainting • The repaint() method calls: update(Graphics g) paint(Graphics g)
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()
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
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.
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.
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.
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.
Colors • RGB • Computer displays have three signals • Various combinations make the many different colors • Different monitors have different colors • Dithering • Halftones
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)
Color Class • Create colors • Color constants black blue cyan darkGray gray green lightGray magenta orange pink red white yellow • Brighten or darken • Switch between RGB and HSB (Hue, Saturation and Brightness)