The Department of Computer Science & Engineering
cse@buffalo
STUART C. SHAPIRO: CSE 115 C

CSE 115
Introduction To Computer Science for Majors I
Lecture C
Lecture Notes #19
Stuart C. Shapiro
Spring, 2007


Summary of Graphics with Swing

We will use Containers, Components, and EventHandlers
java.awt.Container is a subclass of java.awt.Component, so every container is also a component.

Every component must go inside a container.
Every container has or inherits the public Component add(Component comp) method.

javax.swing.JFrame

java.awt.LayoutManager
is an interface that specifies methods that know how to arrange components in containers.
Using LayoutManagers can be complicated, so we provide two Containers that have simple LayoutManagers predefined:

  • containers.Column
    Components added to a Column are added in order, top-down.
    Create a Column with new containers.Column()

  • containers.Row
    Components added to a Row are added in order, left-right.
    Create a Row with new containers.Row()
Components
  • javax.swing.JLabel
    Constructor: javax.swing.JLabel("Label")

  • javax.swing.JButton
    Constructor: javax.swing.JButton("Title")

  • javax.swing.JSlider
    Constructor: javax.swing.JSlider(int min, int max, int value)

  • Drawing Canvas
    A DrawingCanvas is a Container, and therefore also a Component. It is the only Container we will be using that allows a Component to be drawn into it at a specific x,y location.

    Among the properties of a DrawingCanvas are a color and a dimension.

    The color should be drawn from the static variables of java.awt.Color, for example java.awt.Color.green

    The dimension must be an instance of java.awt.Dimension. You should create an instance of java.awt.Dimension using the constructor java.awt.Dimension(int width, int height), where the width and height are measured in pixels. The default width and height of a new DrawingCanvas are both zero.

    Constructor: graphics.DrawingCanvas()
    Some Methods

    voidsetColor(java.awt.Color c)
    java.awt.ColorgetColor()
    voidsetDimension(java.awt.Dimension d)
    java.awt.DimensiongetDimension()
    For example,
    graphics.DrawingCanvas canvas = graphics.DrawingCanvas();
    canvas.setDimension(new java.awt.Dimension(300,500));
    canvas.setColor(java.awt.Color.green);
    

  • Rectangles
    A graphics.Rectangle is a graphical object that looks like a rectangle, has a Color and Dimension, and can be added to a DrawingCanvas at a particular Location. The color and dimension of a rectangle are like those for a DrawingCanvas.

    The Location of a rectangle is relative to its containing DrawingCanvas, and must be an instance of java.awt.Point. You should create an instance of java.awt.Point using the constructor java.awt.Point(int x, int y), where x and y are measured in pixels from the origin, (0,0). The origin is at the top left-hand corner of the DrawingCanvas. x is the distance from the orgin toward the right. y is the distance from the origin down the screen. The default location of a new Rectangle is (0,0).

    Constructor: graphics.Rectangle()
    Some Methods

    voidsetColor(java.awt.Color c)
    java.awt.ColorgetColor()
    voidsetDimension(java.awt.Dimension d)
    java.awt.DimensiongetDimension()
    voidsetLocation(java.awt.point d)
    java.awt.pointgetLocation()

  • Ellipses
    A graphics.Ellipse is a graphical object that looks like an ellipse, has a Color and Dimension, and can be added to a DrawingCanvas at a particular Location. The color, dimension, and location of a rectangle are like those for a Rectangle.

    Constructor: graphics.Ellipse()
    Some Methods

    voidsetColor(java.awt.Color c)
    java.awt.ColorgetColor()
    voidsetDimension(java.awt.Dimension d)
    java.awt.DimensiongetDimension()
    voidsetLocation(java.awt.point d)
    java.awt.pointgetLocation()

First Previous Next

Copyright © 2007 by Stuart C. Shapiro. All rights reserved.

Last modified: Sat Nov 3 14:36:22 EDT 2007
Stuart C. Shapiro <shapiro@cse.buffalo.edu>