1 / 19

Cool Graphics in Java

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

partidaj
Download Presentation

Cool Graphics in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cool Graphics in Java A picture's worth a thousand words CS 102-02 Lecture 6-3

  2. Agenda • Programming • Recursion • Painting with Java • Drawing text • Color

  3. The AWT Hierarchy

  4. 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

  5. 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)

  6. 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

  7. Graphics Screen Increasing x 0,0 Increasing y

  8. 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

  9. 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()

  10. Repainting • The repaint() method calls: update(Graphics g) paint(Graphics g)

  11. 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()

  12. 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

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. Colors • RGB • Computer displays have three signals • Various combinations make the many different colors • Different monitors have different colors • Dithering • Halftones

  18. 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)

  19. 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)

More Related