Creating and Drawing Geometric Objects |
Here are some details that can best be understood by looking at the program MyGraphicsDemo.
/** * 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.
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.
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 Geometric Objects |