Java Graphics  «Prev  Next»

Lesson 8

Java Graphics Fundamentals Conclusion

This module explored the fundamentals of graphics and how they are used in Java. You began the module by becoming acquainted with the Java AWT (Advanced Windowing Toolkit) and, more specifically, the Graphics class. You learned how to use the Graphics class to draw graphics primitives and text. You also were introduced to images and how they fit into Java applets. You wrapped up the module by building a slide show applet that demonstrated how to load and draw images.

Below are the modern equivalents in JavaFX for the terms you listed, which were originally used in Java AWT around Java 1.1 (circa 1997). Each term from AWT is matched with its JavaFX counterpart, along with an explanation.
  1. Abstract Windowing Toolkit (AWT)
    AWT in Java 1.1:
    • Definition: The Abstract Windowing Toolkit (AWT) was the original GUI toolkit for Java. It provided basic GUI components (like buttons, text fields, and canvases) and was dependent on the native GUI components of the operating system, making its components heavyweight.
    • Characteristics:
      • Relied on native OS components.
      • Limited set of GUI widgets.
      • Platform-dependent look and feel.

    JavaFX Equivalent:
    • Equivalent: JavaFX Framework
    • Explanation:
      • JavaFX is the modern, fully-featured GUI toolkit for Java, intended to replace both AWT and Swing.
      • Characteristics:
        • Provides a rich set of GUI components and controls.
        • Uses a Scene Graph for rendering, where each element is a node in a hierarchical tree.
        • Components are lightweight and rendered using hardware-accelerated graphics.
        • Offers advanced features like CSS styling, FXML (an XML-based UI markup language), and support for animations and effects.
        • Platform-Independent Look and Feel: JavaFX components render consistently across platforms

    Summary: The AWT toolkit's role in JavaFX is fulfilled by the entire JavaFX framework, which provides a modern, comprehensive set of tools and components for building GUI applications.

  2. Graphics Class in AWT:
    • Definition: The `Graphics` class in AWT is an abstract base class that provides methods for drawing shapes, text, and images onto components.
    • Usage:
      • Used for custom drawing by overriding the `paint(Graphics g)` method in components.
      • Methods include `drawLine`, `drawRect`, `drawOval`, `drawString`, etc.


    JavaFX Equivalent:
    • Equivalent: GraphicsContext Class and Scene Graph Nodes
    • Explanation:
      • GraphicsContext:
        • Used in conjunction with the Canvas class for immediate mode rendering.
        • Provides methods similar to the AWT Graphics class for drawing shapes, text, and images.
        • Methods include strokeLine, fillRect, strokeOval, fillText, etc.
    • Scene Graph Nodes:
      • JavaFX encourages the use of scene graph nodes (Shape classes) for rendering.
      • Nodes like Rectangle, Circle, Text, and ImageView are added to the scene graph and automatically rendered by the JavaFX framework.
      • This approach leverages JavaFX's retained mode rendering, where the framework manages rendering optimizations.

    Summary: The Graphics class in AWT is replaced by the GraphicsContext class for canvas drawing and by scene graph nodes for standard rendering in JavaFX.
  3. Graphics Context:
    Graphics Context in AWT:
    • Definition: The graphics context in AWT refers to the environment in which drawing operations are performed. It encapsulates information about how to render (e.g., color, font, clipping area).
    • Usage:
      • Encapsulated by the `Graphics` object passed to the `paint` method.
      • Maintains state for drawing operations.

    JavaFX Equivalent:
    • Equivalent: `GraphicsContext` and Rendering Context of Nodes
    • Explanation:
      • GraphicsContext:
        • When using a Canvas, the GraphicsContext object provides the drawing environment.
        • Maintains state such as stroke and fill styles, line widths, and transformations.
      • Node Rendering Context:
        • Each node in the JavaFX scene graph has its own rendering properties.
        • Nodes maintain state through properties (e.g., `fill`, `stroke`, `opacity`, transformations).
        • The rendering context is managed by the JavaFX framework, reducing the need for manual state management.

    • Summary: The concept of a graphics context in AWT is handled in JavaFX by the `GraphicsContext` for canvas drawing and by node properties within the scene graph.

  4. Graphics Primitives:
    • Graphics Primitives in AWT:
      • Definition: Basic drawing operations provided by the `Graphics` class, such as drawing lines, rectangles, ovals, arcs, polygons, and text.
      • Usage: Methods like `drawLine`, `drawRect`, `drawOval`, `drawArc`, `drawPolygon`, and `drawString`.
    • JavaFX Equivalent:
      • Equivalent: Shape Classes and `GraphicsContext` Methods
      • Explanation:
        • Shape Classes:
          • JavaFX provides a variety of shape classes in the `javafx.scene.shape` package.
          • Examples include `Line`, `Rectangle`, `Circle`, `Ellipse`, `Arc`, `Polygon`, and `Text`.
          • Shapes are nodes that can be added to the scene graph and styled using CSS
        • `GraphicsContext` Methods:
          • For immediate mode drawing on a `Canvas`, `GraphicsContext` provides methods like `strokeLine`, `strokeRect`, `strokeOval`, `fillPolygon`, and `fillText`.
          • Supports advanced features like setting line caps, joins, and dashes.

    • Summary: Graphics primitives in AWT are replaced by shape nodes in the JavaFX scene graph and by drawing methods in the `GraphicsContext` class for canvas rendering.

  5. Joint Photographic Experts Group (JPEG)
    • JPEG in AWT:
      • Definition: A commonly used method of lossy compression for digital images.
      • Usage in AWT:
        • Images in JPEG format could be loaded and displayed using the `Image` class and `Toolkit` methods.
        • Limited support for image manipulation.
    • JavaFX Equivalent:
      • Equivalent: `Image` and `ImageView` Classes
      • Explanation:
        • `Image` Class:
          • Used to load images from files, URLs, or input streams.
          • Supports JPEG format along with PNG, GIF, and BMP.
        • `ImageView` Class:
          • A node that displays an `Image`.
          • Can be added to the scene graph like any other node.
          • Supports properties like `fitWidth`, `fitHeight`, `preserveRatio`, and effects.
        • Enhanced Capabilities:
          • JavaFX provides better support for image transformations, effects, and animations.
          • Images can be manipulated using effects like blurs, shadows, and color adjustments.

    Summary: JPEG images are handled in JavaFX using the `Image` class for loading and `ImageView` for display, offering more features and flexibility than AWT.

  6. Graphics Interchange Format (GIF)
    GIF in AWT:
    • Definition: A bitmap image format supporting up to 256 colors and animation frames.
    • Usage in AWT:
      • GIF images could be loaded and displayed using the `Image` class.
      • Limited or no support for animated GIFs.

    JavaFX Equivalent:
    • Equivalent: `Image` and `ImageView` Classes with GIF Support
    • Explanation:
      • `Image` Class: Capable of loading GIF images, including animated GIFs.
      • `ImageView` Class: Displays the `Image` and automatically handles the animation of GIFs.
      • Automatic Animation: JavaFX handles the timing and frame display of animated GIFs without additional coding.
      • Enhanced Capabilities:
        • Supports transformations, effects, and blending modes on GIF images.
        • Can combine animations with other JavaFX animation APIs for complex effects.

    Summary: GIF images, including animations, are fully supported in JavaFX using the `Image` and `ImageView` classes, with additional capabilities for manipulation and effects.


Overall Comparison:
  • AWT Limitations:
    • Relied on native OS components leading to inconsistent behavior.
    • Limited GUI components and drawing capabilities.
    • Minimal support for image formats and manipulations.
  • JavaFX Advancements:
    • Provides a comprehensive set of GUI controls and shapes.
    • Uses a scene graph for efficient rendering and manipulation of nodes.
    • Supports modern image formats with advanced features like animations and effects.
    • Enables styling with CSS and layouts with flexible panes.
    • Offers hardware acceleration and modern graphics capabilities.

Conclusion: JavaFX serves as the modern equivalent of AWT, providing enhanced functionalities and a more powerful, flexible framework for building rich client applications. The concepts from AWT have evolved in JavaFX to accommodate modern GUI requirements, offering developers a robust set of tools for creating sophisticated user interfaces.


Java Graphics Glossary Terms

This module discussed how the following terms relate to Java:
  1. Abtract Windowing Toolkit (AWT): The Abstract Windowing Toolkit is a section of the Java API that supports basic windowing and GUI features.
  2. Graphics class: The Graphics class represents an abstract drawing surface called a graphics context, and is logically equivalent to a piece of paper. Java uses graphics contexts to help abstract the drawing process, which allows you to use the same drawing code to draw to different graphical devices such as monitors and printers.
  3. Graphics context: A graphics context is an abstract drawing surface that is logically equivalent to a piece of paper.
  4. Graphics primitives: Graphics primitives are basic geometric shapes such as lines, rectangles, squares, ovals, circles, polygons, and arcs.
  5. Joint Photographic Experts Group (JPEG): JPEG is an image format that provides a highly efficient means of storing photographic images; JPEG image files typically have a .jpg or .jpeg file extension.
  6. Graphics Interchange Format (GIF): GIF is an image format that is useful for storing non-photographic images such as illustrations and diagrams; GIF image files typically have a .gif file extension.

Java Graphics - Quiz

Click the Quiz link below to test what you have learned in this module.
Java Graphics - Quiz

SEMrush Software