Hour 23. Working with Graphics

Graphics can be represented on a computer in several ways. One is to store the image using a file format that approximates what the graphic looks like. For example, if you took a photo of the Californian sasquatch swimming in your pool and scanned it into your computer, you could save it as a JPEG file. The JPEG format is ideal for storing photographic images in reasonably small file sizes. You also can represent graphics as a series of drawing instructions used to create the image. These are called "vector graphics," and they are becoming popular on the World Wide Web in the form of Macromedia Flash programs. These graphics don't look like photographic images, but they are much smaller in file size and can be redrawn easily at different sizes, making them ideal for animation. If you created a cartoon about the sasquatch, you could save it in a vector format. Java supports vector graphics through Java2D, the same classes that were used to draw text in different ways in Hour 22, "Using Fonts and Color." You'll get a chance to draw shapes of different colors in a program—everything from rectangles to ovals to lines. The following subjects will be covered:

  • The drawing methods of the Graphics2D class
  • Drawing lines
  • Drawing rectangles and rounded rectangles
  • Drawing polygons
  • Drawing ellipses
  • Drawing with different colors
  • Drawing filled and unfilled shapes