Computer Science 252
Problem Solving with Java

Spring 2014, The College of Saint Rose

StdGraphicsDemo Demo

A working demo of StdGraphicsDemo will appear below. Click inside the applet to interact with it.



StdGraphicsDemo BlueJ Project

Click here to download a BlueJ project for StdGraphicsDemo.


StdGraphicsDemo Source Code

The Java source code for StdGraphicsDemo is below. Click on a file name to download it.


StdGraphicsDemo.java

import java.awt.*;
import java.awt.geom.*;
import javax.swing.JApplet;
/*
 * Example StdGraphicsDemo: from Bruce, Danyluk, Murtagh, Figure
 * D.3, p. 666.
 *
 * Demonstration of non-objectdraw graphics in Java.
 *
 * Slight modifications by
 * Jim Teresco, The College of Saint Rose, CSC 252, Spring 2014
 *
 * $Id: StdGraphicsDemo.java 2351 2014-05-01 01:51:25Z terescoj $
 */

public class StdGraphicsDemo extends JApplet {
    
    public void paint(Graphics g) {
    
        Graphics2D g2 = (Graphics2D) g;
        int gridWidth = getWidth()/4;
        
        int x = 5;
        int y = 7;
        int rectWidth = gridWidth - 2*x;
        int stringY = getHeight() - 3;
        int rectHeight = stringY - y - 20;
        
        // Draw a Rectangle2D.Double
        Rectangle2D.Double rect = new Rectangle2D.Double(x, y, rectWidth, rectHeight);
        g2.draw(rect);
        g2.drawString("Rectangle2D", x, stringY);
        x += gridWidth;
        
        // Draw Line2D.Double
        Line2D.Double line = new Line2D.Double(x, y+rectHeight-1, x+rectWidth, y);
        g2.draw(line);
        g2.drawString("Line2D", x, stringY);
        x += gridWidth;
        
        // Draw Ellipse2D.Double
        g2.draw(new Ellipse2D.Double(x, y, rectWidth, rectHeight));
        g2.drawString("Ellipse2D", x, stringY);
        x += gridWidth;
        
        // a filled 2DRectangle.Double in red
        g2.setPaint(Color.red);
        rect.setFrame(x, y, rectWidth, rectHeight);
        g2.fill(rect);
        
        // and one more string
        g2.setPaint(Color.black);
        g2.drawString("Filled Rectangle2D", x, stringY);
    }
 
}