Guest User

Untitled

a guest
Jan 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.85 KB | None | 0 0
  1. package gfx;
  2.  
  3. /**
  4. * Welcome to the beginning of your very own graphics package!
  5. * This graphics package will be used in most of your assignments from
  6. * now on.
  7. *
  8. * This should look A LOT like the code you've seen in lecture (HINT HINT).
  9. * At first glance this class looks really dense, but don't worry most of
  10. * the methods you have to fill in wont be very long.
  11. *
  12. * REMEMBER most of the code you will write here will be code you've seen
  13. * before (WINK WINK).
  14. *
  15. * All of the accessor methods return a dummy value so that this file can
  16. * be compiled from the start.
  17. *
  18. * Feel free to add other functionality, but keep in mind this is a shape
  19. * and shouldn't have capabilities that are more specific to say
  20. * bees or something.
  21. *
  22. * Some bells and whistles you might want to add:
  23. * - set transparency (look at java.awt.Color in the docs)
  24. * - anti aliasing (getting rid of jagged edges..)
  25. *
  26. * @author
  27. */
  28. public abstract class Shape implements cs015.prj.Shape.ShapeInterface {
  29.  
  30. /** Used to store some geometric data for this shape. */
  31. private java.awt.geom.RectangularShape _shape;
  32.  
  33. /** Reference to containing subclass of JPanel. */
  34. private javax.swing.JPanel _container;
  35.  
  36. /** Border and Fill Colors. */
  37. private java.awt.Color _borderColor, _fillColor;
  38.  
  39. /** Rotation (must be in radians). */
  40. private double _rotationAngle;
  41.  
  42. /** Border Width */
  43. private int _borderWidth;
  44.  
  45. /** Indicates whether or not the shape should wrap. */
  46. private boolean _wrapping;
  47.  
  48. /** Whether or not the shape should paint itself. */
  49. private boolean _isVisible;
  50.  
  51. private double _shapeX, _shapeY;
  52. private double _dpWidth, _dpHeight;
  53.  
  54. private java.awt.Transparency _isTransparent;
  55. //private int _isTransparent;
  56.  
  57. private java.awt.RenderingHints RenderingHints;
  58.  
  59. /**
  60. * Initialize all instance variables here. You'll need to store the
  61. * containing subclass of JPanel to deal with wrapping and some of the
  62. * extra credit stuff.
  63. */
  64. public Shape(javax.swing.JPanel container,
  65. java.awt.geom.RectangularShape s) {
  66.  
  67. _shape = s;
  68. }
  69.  
  70. /**
  71. * Should return the x location of the top left corner of
  72. * shape's bounding box.
  73. */
  74. public double getX() {
  75. return _shape.getX();
  76. }
  77.  
  78. /**
  79. * Should return the y location of the top left corner of
  80. * shape's bounding box.
  81. */
  82. public double getY() {
  83. return _shape.getY();
  84. }
  85.  
  86. /** Should return height of shape's bounding box. */
  87. public double getHeight() {
  88. return _shape.getHeight();
  89. }
  90.  
  91. /** Should return width of shape's bounding box. */
  92. public double getWidth() {
  93. return _shape.getWidth();
  94. }
  95.  
  96. /** Should return the border color you are storing. */
  97. public java.awt.Color getBorderColor() {
  98. return _borderColor;
  99. }
  100.  
  101. /** Should return the fill color you are storing. */
  102. public java.awt.Color getFillColor() {
  103. return _fillColor;
  104. }
  105.  
  106. /** Should return the rotation you are storing. */
  107. public double getRotation() {
  108. return _rotationAngle;
  109. }
  110.  
  111. /**
  112. * Should return the width of the brush stroke for
  113. * the outline of your shape.
  114. */
  115. public int getBorderWidth() {
  116. return _borderWidth;
  117. }
  118.  
  119. /** Should return whether or not the shape is wrapping. */
  120. public boolean getWrapping() {
  121. return _wrapping;
  122. }
  123.  
  124. /** Should return whether or not the shape is visible. */
  125. public boolean getVisible() {
  126. return _isVisible;
  127. }
  128.  
  129. /**
  130. * Set the location of shape. Make sure to wrap if the wrap
  131. * boolean is true. Refer to the help session slides to see
  132. * how wrapping is done.
  133. */
  134. public void setLocation(double x, double y) {
  135.  
  136.  
  137. if (_wrapping == true){
  138. _dpHeight = this.getHeight();
  139. _dpWidth = this.getWidth();
  140.  
  141. double newX = ((x % _dpWidth) + _dpWidth) % _dpWidth;
  142. double newY = ((y % _dpHeight) + _dpHeight) % _dpHeight; //calculates mod
  143. //this.setLocation(newX, newY);
  144. // _shapeX = newX;
  145. // _shapeY = newY;
  146. }
  147. _shape.setFrame(x, y, _shape.getWidth(), _shape.getHeight());
  148. }
  149.  
  150. /** Set the size of shape. */
  151. public void setSize(double width, double height) {
  152. _shape.setFrame(_shape.getX(), _shape.getY(), width, height);
  153. }
  154.  
  155. /** Set the border color. */
  156. public void setBorderColor(java.awt.Color c) {
  157. _borderColor = c;
  158. }
  159.  
  160. /** Set the fill color. */
  161. public void setFillColor(java.awt.Color c) {
  162. _fillColor = c;
  163. }
  164.  
  165. /** Set the color of the whole shape. */
  166. public void setColor(java.awt.Color c) {
  167. _borderColor = c;
  168. _fillColor = c;
  169. }
  170.  
  171. /**
  172. * Set the rotation of the shape. Refer to the lecture to see
  173. * how this should be done
  174. */
  175. public void setRotation(double degrees) {
  176. _rotationAngle = degrees;
  177. }
  178.  
  179. /** Set how thick the shapes outline will be. */
  180. public void setBorderWidth(int width) {
  181. _borderWidth = width;
  182. }
  183.  
  184. /** Set whether or not the shape should wrap. */
  185. public void setWrapping(boolean wrap) {
  186. _wrapping = wrap;
  187. }
  188.  
  189. /** Set whether or not the shape should paint itself. */
  190. public void setVisible(boolean visible) {
  191. _isVisible = visible;
  192. }
  193.  
  194. //public static final int TRANSLUCENT(){
  195. //return _isTransparent.TRANSLUCENT;
  196. //}
  197. //public void getTransparency(int transparent){
  198. //_isTransparent.getAlpha(.5);
  199. //return _isTransparent;
  200.  
  201. //= _fillColor.getAlpha();
  202. //_borderColor.getAlpha();
  203. //}
  204.  
  205. /*public void setTransparency(int transparent){
  206. _isTransparent = transparent;
  207. }*/
  208.  
  209. /*public void add(RenderingHints hints){
  210. _antiAlias = hints;
  211. //VALUE_ANTIALIAS_ON;
  212. }*/
  213.  
  214. /**
  215. * This method is best explained in pseudocode:
  216. * If shape is visible
  217. * rotate graphics
  218. * set the brush stroke (width) of the graphics
  219. * set the color of the graphics to the fill color of the shape
  220. * fill the shape
  221. * set the color of the graphics to the border color of the shape
  222. * draw the shape
  223. * un-rotate the graphics
  224. */
  225. public void paint(java.awt.Graphics2D brush) {
  226. if (_isVisible == true){
  227. brush.rotate(_rotationAngle, _shape.getCenterX(), _shape.getCenterY());
  228.  
  229. System.out.println("border: " + _borderWidth);
  230. //brush.setBorderWidth(_borderWidth);
  231. //brush.setStroke(_borderWidth);
  232. brush.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  233. RenderingHints.VALUE_ANTIALIAS_ON);
  234. //brush.getTransparency(_isTransparent);
  235. //brush.setStroke(new getBorderWidth(_borderWidth));
  236. brush.setColor(_fillColor);
  237. brush.fill(_shape);
  238. brush.setColor(_borderColor);
  239. brush.draw(_shape);
  240.  
  241. brush.rotate(-_rotationAngle, _shape.getCenterX(), _shape.getCenterY());
  242. }
  243.  
  244.  
  245. }
  246.  
  247. /*public void setLocation(double x, double y) {
  248. if (_wrapping == true){
  249. _dpHeight = this.getHeight();
  250. _dpWidth = this.getWidth();
  251.  
  252. double newX = ((x % _dpWidth) + _dpWidth) % _dpWidth;
  253. double newY = ((y % _dpHeight) + _dpHeight) % _dpHeight; //calculates mod
  254. this.setLocation(newX, newY);
  255. _shapeX = newX;
  256. _shapeY = newY;
  257. }
  258.  
  259. }*/
  260. /**
  261. * Should return true if the point is within the shape.
  262. * There's a special case for when the shape is rotated,
  263. * which we have handled for you.
  264. *
  265. * YOU DO NOT NEED TO TOUCH THIS METHOD.
  266. */
  267. public boolean contains(java.awt.Point p) {
  268. if (0 != _rotationAngle) {
  269. double x = _shape.getCenterX();
  270. double y = _shape.getCenterY();
  271. java.awt.geom.AffineTransform trans = java.awt.geom.AffineTransform.getRotateInstance(_rotationAngle, x, y);
  272. java.awt.Shape s = trans.createTransformedShape(_shape);
  273. return s.contains(p);
  274. }
  275. return _shape.contains(p);
  276. }
  277.  
  278. /**
  279. * This should be called when the shape is clicked.
  280. * You'll want to overwrite this in subclasses to do something useful.
  281. * Should stay empty in this class
  282. */
  283. public void react() {}
  284. }
Add Comment
Please, Sign In to add comment