ClayGraphics clay.canvas package

The ClayGraphics clay.canvas package consists of 1 Java Interfaces and 1 Java Class which implement the interface. Additionally, 1 Java Main Class is crafted to test the Java Class.

Demos

Code

/*
 * ClayCanvasADT.java
 *
 * Created on June 6, 2006, 2:22 PM
 */

package clay.canvas;

import clay.math.ClayPoint;
import clay.shapes.ClayCircle;
import clay.shapes.ClayRectangle;
import clay.shapes.ClaySquare;
import java.awt.Color;

/**
 *
 * @author blue
 */
public interface ClayCanvasADT {
    public int height();
    public int width();
    public ClayPoint center();
    public void paint(Color c);
    public void paint(ClayPoint point, ClayCircle circle, Color color);
    public void paint(ClayPoint point, ClaySquare square, Color color);
    public void paint(ClayPoint point, ClayRectangle rectangle, Color color);
    public void setStrokeSize(int s);
    public void draw(ClayPoint point, ClayCircle circle, Color color);
    public void draw(ClayPoint point, ClaySquare square, Color color);
    public void draw(ClayPoint point, ClayRectangle rectangle, Color color);
    public void draw(String s, ClayPoint p, Color c);
}


/*
 * ClayCanvas.java
 *
 * Created on June 5, 2006, 8:05 PM
 */

package clay.canvas;

/**
 *
 * @author blue
 */
import clay.math.ClayPoint;
import clay.shapes.ClayCircle;
import clay.shapes.ClaySquare;
import clay.shapes.ClayRectangle;
import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

public class ClayCanvas extends Canvas implements ClayCanvasADT
{
    BufferedImage image = null; 
    Graphics2D gir = null;

    int xCenter;
    int yCenter;

    int strokeSize = 1;

    public ClayCanvas()
    {
        super();
    }

    public int width()
    {
        return this.getWidth();
    }

    public int height()
    {
        return this.getHeight();
    }

    public ClayPoint center()
    {
        return new ClayPoint(xCenter,yCenter);
    }

    public void initializeImage()
    {
        establishCenterPoint();
        establishImage();
        paint(Color.white);
    }

    public void validateImage()
    {
        establishCenterPoint();
        establishImage();
    }

    private void establishCenterPoint()
    {
        int w = this.getWidth();
        int h = this.getHeight();
        xCenter = w / 2;
        yCenter = h / 2;
    }

    private void establishImage()
    {
        int w = this.getWidth();
        int h = this.getHeight();
        image = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
        gir = image.createGraphics();
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void paint(Graphics g)
    {
        Graphics2D g2 = (Graphics2D)g;
        if ( image == null ) { initializeImage(); };
        g2.drawImage(image,0,0,this);
    }

    private void setPaint(Color c)
    {
        gir.setPaint(c);
    }

    public void paint(Color c)
    {
        validateImage();
        Point p = new Point(0,0);
        Dimension d = new Dimension(this.getWidth(),this.getHeight());
        Rectangle r = new Rectangle(p,d);
        gir.setPaint(c);
        gir.fill(r);
        repaint();
    }

    public void paint(ClayPoint p, ClayCircle circle, Color c)
    {
        double x = p.x()-circle.radius();
        double y = p.y()-circle.radius();
        double w = circle.radius()*2;
        double h = circle.radius()*2;
        gir.setPaint(c);
        gir.setStroke(new BasicStroke((int)circle.radius()));
        gir.fillOval((int)x,(int)y,(int)w,(int)h);
        Graphics pg = getGraphics();
        update(pg);
    }

    public void paint(ClayPoint p, ClaySquare s, Color c)
    {
        double x = p.x()-(s.side()/2.0);
        double y = p.y()-(s.side()/2.0);
        double w = s.side();
        double h = s.side();
        Dimension d = new Dimension((int)s.side(),(int)s.side());
        Point pt = new Point((int)x,(int)y);
        Rectangle r = new Rectangle(pt,d);
        gir.setPaint(c);
        gir.setStroke(new BasicStroke(strokeSize));
        gir.fill(r);
        Graphics pg = getGraphics();
        update(pg);
    }

    public void paint(ClayPoint p, ClayRectangle r, Color c)
    {
        double x = p.x()-(r.width()/2.0);
        double y = p.y()-(r.height()/2.0);
        double w = r.width();
        double h = r.height();
        Dimension d = new Dimension((int)r.width(),(int)r.height());
        Point pt = new Point((int)x,(int)y);
        Rectangle re = new Rectangle(pt,d);
        gir.setPaint(c);
        gir.setStroke(new BasicStroke(strokeSize));
        gir.fill(re);
        Graphics pg = getGraphics();
        update(pg);
    }

    public void setStrokeSize(int s)
    {
        strokeSize = s;
    }

    public void draw(ClayPoint p, ClayCircle circle, Color c)
    {
        double x = p.x()-circle.radius();
        double y = p.y()-circle.radius();
        double w = circle.radius()*2;
        double h = circle.radius()*2;
        gir.setStroke(new BasicStroke(strokeSize));
        gir.setPaint(c);
        gir.drawOval((int)x,(int)y,(int)w,(int)h);
        Graphics pg = getGraphics();
        update(pg);
    }

    public void draw(ClayPoint p, ClaySquare square, Color c)
    {
        double x = p.x()-square.side()/2.0;
        double y = p.y()-square.side()/2.0;
        double w = square.side()*2;
        double h = square.side()*2;
        Dimension d = new Dimension((int)square.side(),(int)square.side());
        Point pt = new Point((int)x,(int)y);
        Rectangle re = new Rectangle(pt,d);
        gir.setPaint(c);
        gir.setStroke(new BasicStroke(strokeSize));
        gir.draw(re);
        Graphics pg = getGraphics();
        update(pg);
    }
    
    public void draw(ClayPoint point, ClayRectangle rectangle, Color color) {
        double x = point.x()-(rectangle.width()/2.0);
        double y = point.y()-(rectangle.height()/2.0);
        double w = rectangle.width();
        double h = rectangle.height();
        Dimension d = new Dimension((int)rectangle.width(),(int)rectangle.height());
        Point pt = new Point((int)x,(int)y);
        Rectangle re = new Rectangle(pt,d);
        gir.setPaint(color);
        gir.setStroke(new BasicStroke(strokeSize));
        gir.draw(re);
        Graphics pg = getGraphics();
        update(pg);
    }

    public void draw(String s, ClayPoint p, Color c)
    {
        double x = p.x();
        double y = p.y();
        FontRenderContext frc = gir.getFontRenderContext();
        Rectangle2D bounds = gir.getFont().getStringBounds(s,frc);
        double width = (double)bounds.getWidth();
        gir.setPaint(c);
        gir.drawString(s,(int)(x - (width/2)), (int)y);
        Graphics pg = getGraphics();
        update(pg);
    }

}


/*
 * ClayCanvasTest.java
 *
 * Created on June 6, 2006, 2:33 PM
 */

package testers;

import clay.canvas.ClayCanvas;
import clay.math.ClayPoint;
import clay.shapes.ClayCircle;
import clay.shapes.ClayRectangle;
import clay.shapes.ClaySquare;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 *
 * @author blue
 */
public class ClayCanvasTest {
    
    public ClayCanvasTest() {
    }
    
    public static void main(String[] args) {
        ClayCanvasTestFrame cc = new ClayCanvasTestFrame("ClayCanvas Test");
        cc.setVisible(true);
        cc.canvas().initializeImage();
        
        cc.canvas().paint(new ClayPoint(cc.width()/4,cc.height()/4),new ClayCircle(30),Color.red);
        
        cc.canvas().paint(new ClayPoint(cc.width()/4*2,cc.height()/4),new ClaySquare(30),Color.blue);
        
        cc.canvas().paint(new ClayPoint(cc.width()/4*3,cc.height()/4),new ClayRectangle(100,30),Color.cyan);
        
        cc.canvas().setStrokeSize(5);
        ClayCircle circle = new ClayCircle(30);
        cc.canvas().draw(new ClayPoint(cc.width()/4,cc.height()/4*2),circle,Color.red);
        circle.expand(10);
        cc.canvas().draw(new ClayPoint(cc.width()/4,cc.height()/4*2),circle,Color.red);
        
        cc.canvas().setStrokeSize(3);
        ClaySquare square = new ClaySquare(30);
        cc.canvas().draw(new ClayPoint(cc.width()/4*2,cc.height()/4*2),square,Color.blue);
        square.shrink(10);
        cc.canvas().draw(new ClayPoint(cc.width()/4*2,cc.height()/4*2),square,Color.blue);
        
        cc.canvas().setStrokeSize(1);
        ClayPoint cp = new ClayPoint(cc.width()/4*3,cc.height()/4*2);
        cc.canvas().draw(cp,new ClayRectangle(100,30),Color.cyan);
        cp.moveTo(cp.x()+10,cp.y()+10);
        cc.canvas().draw(cp,new ClayRectangle(100,30),Color.cyan);
        
        cc.canvas().draw("THIS IS A TEST!", new ClayPoint(cc.width()/4*2,cc.height()/4*3),Color.black);
    }
    
}

class ClayCanvasTestFrame extends Frame {
    
    private ClayCanvas canvas = new ClayCanvas();  

    public ClayCanvas canvas()
    {
        return canvas;
    }
    

    public int width() {
        return canvas.width();
    }
    
    public int height() {
        return canvas.height();
    }


    public ClayCanvasTestFrame(String title)
    {
        super(title);
        setSize(500,500);
        setLayout(new GridLayout(1,1));
        add(canvas);
        addWindowListener(new CloseWindow());
    }  
    
    class CloseWindow extends WindowAdapter {
        public CloseWindow() {
        }
    
        public void windowClosing(WindowEvent event) {
            System.exit(0);
        }
    }


}


<Questions>

  1. Which methods in the class override methods of the Canvas class?
  2. Describe what each of the noninherited public methods does in this class.