Creating and Drawing ImagesTopCreating and Drawing Geometric Objects

Creating and Drawing Geometric Objects

Here are some details that can best be understood by looking at the program MyGraphicsDemo.

  1. The classes defined below are found in packages java.awt and java.awt.geom. Thus you will need to include statements to import java.awt.* and java.awt.geom.*.
  2. paint(g) is called automatically when a window is created or uncovered in a window. It can also be forced to execute by sending a repaint() message to a component. That method schedules a call to an update(g) method as soon as possible.
  3. The inherited update(g) method erases the component and then calls paint(g). The Graphics object g for the component is automatically provided by the system.
  4. The user overrides paint(g) to actually draw something on the component. All drawing commands are either in this method or in a method called from paint.
  5. The Graphics object should be thought of like a pen that is responsible for doing the actual drawing. The newer Java graphics actually use a class called Graphics2D, which extends Graphics, and that is the one actually passed to the paint method. Unfortunately, for compatibility with old code, the parameter type is still Graphics. To use the newer method described below, you must begin by casting the graphics context to Graphics2D:
        /**
         * Draw figures on the window that has graphics g
         * @param g - the graphics context of the current window
         */
        public void paint(Graphics g) {
           Graphics2D g2 = (Graphics2D)g;
           g2.draw(...);
           ...
        }
    
    The draw message is sent to the Graphics2D object to draw various framed graphics objects, as shown in the sample code. The method fill is used to draw filled objects. The message drawString is used to write a string on the screen. The message setPaint(aColor) changes the color of the pen. It stays that color until changed again.
  6. The graphics objects can be used with either type float or double for coordinates and dimensions. We will always use the double versions. The graphics classes that you should use are thus Their rectangle constructor takes the following parameters:
            myRect = new Rectangle2D.Double(x, y, width, height);
        
    The others are similar. Details can be found in the javadoc documentation for the Java 5classes, which may be found from a link on the Documents and Handouts page of the course web page. For those of you used to the objectdraw library, you should note that there is no canvas parameter. Note also that this creates the object, but does not actually draw it anywhere. If g2 is a Graphics2D object, then you can actually draw the object using one of the two following methods:
            g2.draw(myRect);
            g2.fill(myRect);
        
    The first draws a framed version of the object, while the second draws a filled version. Every time repaint is called or a window is uncovered the paint method will be executed. As a result, you should make sure that everything that you want to appear on the screen will be redrawn by the paint method.
  7. You can draw on virtually any component in a window, including a JApplet, JFrame, or JPanel. Normally in this class we will be drawing on a JFrame or JPanel. Suppose you just want to pop up a window and write on its surface. The kind of window we will be using in Java is called a JFrame.1 A program that just pops up a window and draws something on it will have the following form:
        public class GraphicsExample extends JFrame {
            private static final int WINDOW_WIDTH = ...;
            private static final int WINDOW_HEIGHT = ...;
                
            // instance variable declarations
                
            /**
             * Create the window
             * @ param title -- the text to be show on the title bar
             */
            public GraphicsExample(String title) {
               super(title);
               ...
            }
            
            /**
              * Draw figures on the window that has graphics g
              * Called by repaint() or whenever the screen needs to be
              * refreshed
              * @param g - the graphics context of the current window
              */
            public paint(Graphics g) {
               Graphics2D g2 = (Graphics) g;
               // commands using g2
            }
            
            // Create the window of the desired size and display it.
            public static void main(String[] args) {
               GraphicsExample f = new GraphicsExample("StdGraphicsDemo");
               f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               f.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
               f.setVisible(true);
            }
        }
        

The class extends JFrame, so when it is created it will pop up a window. The call to the super constructor in the constructor for GraphicsExample ensures that the string in title shows in the title bar of the window. The paint method casts the graphics context to be of type Graphics2D so the above commands will work.

The main method constructs an object of the class, which will result in a window being popped up. The next three commands make sure that clicking in the red "go-away box" at the top of the window will result in the program terminating, set the window size at whatever values are held in the constants, and make the window visible. When the window is made visible for the first time, the repaint method will automatically be called, which will erase the window and eventually call paint with the graphics context of the window.


Creating and Drawing ImagesTopCreating and Drawing Geometric Objects