Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // abstrakten klass za obshtite elementi na figurite
- //An abstract class that implement the common methods between all shapes
- public abstract class MyShape implements Shape, Cloneable{
- //common parameters between shapes
- private Map<String, Double> properties = new HashMap<>();
- private Point position = new Point(13, 12);
- private String name;
- private Color color = Color.black;
- private Color fillColor = color.white;
- @Override
- //common
- public void setPosition(Point position) {
- this.position = position;
- }
- @Override
- //common
- public Point getPosition() {
- return this.position;
- }
- @Override
- //common
- public void setColor(Color color) {
- this.color = color;
- }
- @Override
- //common
- public Color getColor() {
- return this.color;
- }
- @Override
- //common
- public void setProperties(Map<String, Double> properties) {
- }
- @Override
- //common
- public Map<String, Double> getProperties() {
- return null;
- }
- @Override
- //common
- public void setFillColor(Color color) {
- this.fillColor = color;
- }
- @Override
- //common
- public Color getFillColor() {
- return this.fillColor;
- }
- @Override
- //not common
- public void draw(java.awt.Graphics canvas){
- }
- @Override
- //not common
- public Object clone() throws CloneNotSupportedException{
- return null;
- }
- }
- // prawoygylnik
- public class Rectangle extends MyShape {
- private Map<String, Double> properties = new HashMap<>();
- public static final String LENGTH_KEY = "xAxis";
- public static final String WIDTH_KEY = "yAxis";
- public Rectangle() {
- setColor(this.getColor());
- // center of mass
- setPosition(this.getPosition());
- this.properties.put("stroke",(double) 3.0f);
- this.properties.put(LENGTH_KEY, 0.0);
- this.properties.put(WIDTH_KEY, 0.0);
- setProperties(this.properties);
- setFillColor(this.getFillColor());
- }
- @Override
- public void setProperties(Map<String, Double> properties) {
- this.properties = properties;
- }
- @Override
- //common
- public Map<String, Double> getProperties() {
- return this.properties;
- }
- @Override
- public void draw(Graphics canvas) {
- Graphics2D g2 = (Graphics2D) canvas;
- double stroke = this.properties.get("stroke");
- g2.setStroke(new BasicStroke((float) stroke));
- Point position = getPosition();
- double length = getProperties().get(LENGTH_KEY);
- double width = getProperties().get(WIDTH_KEY);
- g2.setColor(getColor());
- g2.drawRect(position.x, position.y, (int)width, (int)length);
- g2.setColor(getFillColor());
- g2.fillRect(position.x, position.y, (int)width, (int)length);
- }
- @Override
- public Object clone() throws CloneNotSupportedException{
- Shape clonedShape = new Rectangle();
- clonedShape.setColor(this.getColor());
- clonedShape.setFillColor(this.getFillColor());
- clonedShape.setPosition(this.getPosition());
- Map<String, Double> newprop = new HashMap<String,Double>();
- for (Map.Entry s: this.properties.entrySet()){
- String key = (String) s.getKey();
- Double value = (Double) s.getValue();
- newprop.put(key, value);
- }
- clonedShape.setProperties(newprop);
- return clonedShape;
- }
- }
- // figura kwadrat
- public class Square extends Rectangle{
- public static final String LENGTH_KEY = "length";
- public Square(){
- super();
- }
- @Override
- public Object clone() throws CloneNotSupportedException{
- Shape clonedShape = new Rectangle();
- clonedShape.setColor(this.getColor());
- clonedShape.setFillColor(this.getFillColor());
- clonedShape.setPosition(this.getPosition());
- Map<String, Double> newprop = new HashMap<String,Double>();
- for (Map.Entry s: this.getProperties().entrySet()){
- String key = (String) s.getKey();
- Double value = (Double) s.getValue();
- newprop.put(key, value);
- }
- clonedShape.setProperties(newprop);
- return clonedShape;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement