Advertisement
SUni2020

FIGURI V PROEKT GPI

Jun 6th, 2021
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. // abstrakten klass za obshtite elementi na figurite
  2. //An abstract class that implement the common methods between all shapes
  3.  
  4. public abstract class MyShape implements Shape, Cloneable{
  5.  
  6. //common parameters between shapes
  7. private Map<String, Double> properties = new HashMap<>();
  8. private Point position = new Point(13, 12);
  9. private String name;
  10. private Color color = Color.black;
  11. private Color fillColor = color.white;
  12.  
  13. @Override
  14. //common
  15. public void setPosition(Point position) {
  16. this.position = position;
  17. }
  18.  
  19. @Override
  20. //common
  21. public Point getPosition() {
  22. return this.position;
  23. }
  24.  
  25. @Override
  26. //common
  27. public void setColor(Color color) {
  28.  
  29. this.color = color;
  30. }
  31.  
  32. @Override
  33. //common
  34. public Color getColor() {
  35. return this.color;
  36. }
  37.  
  38. @Override
  39. //common
  40. public void setProperties(Map<String, Double> properties) {
  41.  
  42. }
  43.  
  44. @Override
  45. //common
  46. public Map<String, Double> getProperties() {
  47. return null;
  48. }
  49.  
  50. @Override
  51. //common
  52. public void setFillColor(Color color) {
  53. this.fillColor = color;
  54.  
  55. }
  56.  
  57. @Override
  58. //common
  59. public Color getFillColor() {
  60. return this.fillColor;
  61. }
  62.  
  63. @Override
  64. //not common
  65. public void draw(java.awt.Graphics canvas){
  66.  
  67. }
  68.  
  69. @Override
  70. //not common
  71. public Object clone() throws CloneNotSupportedException{
  72. return null;
  73.  
  74. }
  75.  
  76. }
  77.  
  78.  
  79. // prawoygylnik
  80. public class Rectangle extends MyShape {
  81. private Map<String, Double> properties = new HashMap<>();
  82. public static final String LENGTH_KEY = "xAxis";
  83. public static final String WIDTH_KEY = "yAxis";
  84.  
  85. public Rectangle() {
  86.  
  87. setColor(this.getColor());
  88. // center of mass
  89. setPosition(this.getPosition());
  90. this.properties.put("stroke",(double) 3.0f);
  91. this.properties.put(LENGTH_KEY, 0.0);
  92. this.properties.put(WIDTH_KEY, 0.0);
  93. setProperties(this.properties);
  94. setFillColor(this.getFillColor());
  95. }
  96. @Override
  97. public void setProperties(Map<String, Double> properties) {
  98. this.properties = properties;
  99. }
  100.  
  101. @Override
  102. //common
  103. public Map<String, Double> getProperties() {
  104. return this.properties;
  105. }
  106.  
  107. @Override
  108. public void draw(Graphics canvas) {
  109. Graphics2D g2 = (Graphics2D) canvas;
  110. double stroke = this.properties.get("stroke");
  111. g2.setStroke(new BasicStroke((float) stroke));
  112. Point position = getPosition();
  113. double length = getProperties().get(LENGTH_KEY);
  114. double width = getProperties().get(WIDTH_KEY);
  115. g2.setColor(getColor());
  116. g2.drawRect(position.x, position.y, (int)width, (int)length);
  117. g2.setColor(getFillColor());
  118. g2.fillRect(position.x, position.y, (int)width, (int)length);
  119. }
  120.  
  121. @Override
  122. public Object clone() throws CloneNotSupportedException{
  123. Shape clonedShape = new Rectangle();
  124. clonedShape.setColor(this.getColor());
  125. clonedShape.setFillColor(this.getFillColor());
  126. clonedShape.setPosition(this.getPosition());
  127. Map<String, Double> newprop = new HashMap<String,Double>();
  128. for (Map.Entry s: this.properties.entrySet()){
  129. String key = (String) s.getKey();
  130. Double value = (Double) s.getValue();
  131. newprop.put(key, value);
  132. }
  133. clonedShape.setProperties(newprop);
  134. return clonedShape;
  135. }
  136. }
  137.  
  138. // figura kwadrat
  139. public class Square extends Rectangle{
  140. public static final String LENGTH_KEY = "length";
  141.  
  142. public Square(){
  143. super();
  144. }
  145.  
  146. @Override
  147. public Object clone() throws CloneNotSupportedException{
  148. Shape clonedShape = new Rectangle();
  149. clonedShape.setColor(this.getColor());
  150. clonedShape.setFillColor(this.getFillColor());
  151. clonedShape.setPosition(this.getPosition());
  152. Map<String, Double> newprop = new HashMap<String,Double>();
  153. for (Map.Entry s: this.getProperties().entrySet()){
  154. String key = (String) s.getKey();
  155. Double value = (Double) s.getValue();
  156. newprop.put(key, value);
  157. }
  158. clonedShape.setProperties(newprop);
  159. return clonedShape;
  160. }
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement