Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gfx;
- /**
- * Welcome to the beginning of your very own graphics package!
- * This graphics package will be used in most of your assignments from
- * now on.
- *
- * This should look A LOT like the code you've seen in lecture (HINT HINT).
- * At first glance this class looks really dense, but don't worry most of
- * the methods you have to fill in wont be very long.
- *
- * REMEMBER most of the code you will write here will be code you've seen
- * before (WINK WINK).
- *
- * All of the accessor methods return a dummy value so that this file can
- * be compiled from the start.
- *
- * Feel free to add other functionality, but keep in mind this is a shape
- * and shouldn't have capabilities that are more specific to say
- * bees or something.
- *
- * Some bells and whistles you might want to add:
- * - set transparency (look at java.awt.Color in the docs)
- * - anti aliasing (getting rid of jagged edges..)
- *
- * @author
- */
- public abstract class Shape implements cs015.prj.Shape.ShapeInterface {
- /** Used to store some geometric data for this shape. */
- private java.awt.geom.RectangularShape _shape;
- /** Reference to containing subclass of JPanel. */
- private javax.swing.JPanel _container;
- /** Border and Fill Colors. */
- private java.awt.Color _borderColor, _fillColor;
- /** Rotation (must be in radians). */
- private double _rotationAngle;
- /** Border Width */
- private int _borderWidth;
- /** Indicates whether or not the shape should wrap. */
- private boolean _wrapping;
- /** Whether or not the shape should paint itself. */
- private boolean _isVisible;
- private double _shapeX, _shapeY;
- private double _dpWidth, _dpHeight;
- private java.awt.Transparency _isTransparent;
- //private int _isTransparent;
- private java.awt.RenderingHints RenderingHints;
- /**
- * Initialize all instance variables here. You'll need to store the
- * containing subclass of JPanel to deal with wrapping and some of the
- * extra credit stuff.
- */
- public Shape(javax.swing.JPanel container,
- java.awt.geom.RectangularShape s) {
- _shape = s;
- }
- /**
- * Should return the x location of the top left corner of
- * shape's bounding box.
- */
- public double getX() {
- return _shape.getX();
- }
- /**
- * Should return the y location of the top left corner of
- * shape's bounding box.
- */
- public double getY() {
- return _shape.getY();
- }
- /** Should return height of shape's bounding box. */
- public double getHeight() {
- return _shape.getHeight();
- }
- /** Should return width of shape's bounding box. */
- public double getWidth() {
- return _shape.getWidth();
- }
- /** Should return the border color you are storing. */
- public java.awt.Color getBorderColor() {
- return _borderColor;
- }
- /** Should return the fill color you are storing. */
- public java.awt.Color getFillColor() {
- return _fillColor;
- }
- /** Should return the rotation you are storing. */
- public double getRotation() {
- return _rotationAngle;
- }
- /**
- * Should return the width of the brush stroke for
- * the outline of your shape.
- */
- public int getBorderWidth() {
- return _borderWidth;
- }
- /** Should return whether or not the shape is wrapping. */
- public boolean getWrapping() {
- return _wrapping;
- }
- /** Should return whether or not the shape is visible. */
- public boolean getVisible() {
- return _isVisible;
- }
- /**
- * Set the location of shape. Make sure to wrap if the wrap
- * boolean is true. Refer to the help session slides to see
- * how wrapping is done.
- */
- public void setLocation(double x, double y) {
- if (_wrapping == true){
- _dpHeight = this.getHeight();
- _dpWidth = this.getWidth();
- double newX = ((x % _dpWidth) + _dpWidth) % _dpWidth;
- double newY = ((y % _dpHeight) + _dpHeight) % _dpHeight; //calculates mod
- //this.setLocation(newX, newY);
- // _shapeX = newX;
- // _shapeY = newY;
- }
- _shape.setFrame(x, y, _shape.getWidth(), _shape.getHeight());
- }
- /** Set the size of shape. */
- public void setSize(double width, double height) {
- _shape.setFrame(_shape.getX(), _shape.getY(), width, height);
- }
- /** Set the border color. */
- public void setBorderColor(java.awt.Color c) {
- _borderColor = c;
- }
- /** Set the fill color. */
- public void setFillColor(java.awt.Color c) {
- _fillColor = c;
- }
- /** Set the color of the whole shape. */
- public void setColor(java.awt.Color c) {
- _borderColor = c;
- _fillColor = c;
- }
- /**
- * Set the rotation of the shape. Refer to the lecture to see
- * how this should be done
- */
- public void setRotation(double degrees) {
- _rotationAngle = degrees;
- }
- /** Set how thick the shapes outline will be. */
- public void setBorderWidth(int width) {
- _borderWidth = width;
- }
- /** Set whether or not the shape should wrap. */
- public void setWrapping(boolean wrap) {
- _wrapping = wrap;
- }
- /** Set whether or not the shape should paint itself. */
- public void setVisible(boolean visible) {
- _isVisible = visible;
- }
- //public static final int TRANSLUCENT(){
- //return _isTransparent.TRANSLUCENT;
- //}
- //public void getTransparency(int transparent){
- //_isTransparent.getAlpha(.5);
- //return _isTransparent;
- //= _fillColor.getAlpha();
- //_borderColor.getAlpha();
- //}
- /*public void setTransparency(int transparent){
- _isTransparent = transparent;
- }*/
- /*public void add(RenderingHints hints){
- _antiAlias = hints;
- //VALUE_ANTIALIAS_ON;
- }*/
- /**
- * This method is best explained in pseudocode:
- * If shape is visible
- * rotate graphics
- * set the brush stroke (width) of the graphics
- * set the color of the graphics to the fill color of the shape
- * fill the shape
- * set the color of the graphics to the border color of the shape
- * draw the shape
- * un-rotate the graphics
- */
- public void paint(java.awt.Graphics2D brush) {
- if (_isVisible == true){
- brush.rotate(_rotationAngle, _shape.getCenterX(), _shape.getCenterY());
- System.out.println("border: " + _borderWidth);
- //brush.setBorderWidth(_borderWidth);
- //brush.setStroke(_borderWidth);
- brush.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- //brush.getTransparency(_isTransparent);
- //brush.setStroke(new getBorderWidth(_borderWidth));
- brush.setColor(_fillColor);
- brush.fill(_shape);
- brush.setColor(_borderColor);
- brush.draw(_shape);
- brush.rotate(-_rotationAngle, _shape.getCenterX(), _shape.getCenterY());
- }
- }
- /*public void setLocation(double x, double y) {
- if (_wrapping == true){
- _dpHeight = this.getHeight();
- _dpWidth = this.getWidth();
- double newX = ((x % _dpWidth) + _dpWidth) % _dpWidth;
- double newY = ((y % _dpHeight) + _dpHeight) % _dpHeight; //calculates mod
- this.setLocation(newX, newY);
- _shapeX = newX;
- _shapeY = newY;
- }
- }*/
- /**
- * Should return true if the point is within the shape.
- * There's a special case for when the shape is rotated,
- * which we have handled for you.
- *
- * YOU DO NOT NEED TO TOUCH THIS METHOD.
- */
- public boolean contains(java.awt.Point p) {
- if (0 != _rotationAngle) {
- double x = _shape.getCenterX();
- double y = _shape.getCenterY();
- java.awt.geom.AffineTransform trans = java.awt.geom.AffineTransform.getRotateInstance(_rotationAngle, x, y);
- java.awt.Shape s = trans.createTransformedShape(_shape);
- return s.contains(p);
- }
- return _shape.contains(p);
- }
- /**
- * This should be called when the shape is clicked.
- * You'll want to overwrite this in subclasses to do something useful.
- * Should stay empty in this class
- */
- public void react() {}
- }
Add Comment
Please, Sign In to add comment