You are on page 1of 9

Danya Shaban 201911811

//interface
public interface Shape {
void draw();
String description();
}
—------------------------------------------------------------------------------

//concrete class1 Rectangle


public class Rectangle implements Shape {

@Override
public void draw() {
System.out.println("Shape: Rectangle is drawing");

}
@Override
public String description(){
return "This is a Rectangle ";
}

}
—----------------------------------------------------------------------------------
//concrete class2 Circle

public class Circle implements Shape {


@Override
public void draw() {
System.out.println("Shape: Circle is drawing");
}

@Override
public String description(){
return "This is a Circle ";
}
}

—-----------------------------------------------------------------------------------
// (main decorator)

public abstract class ShapeDecorator implements Shape {


protected Shape decoratedShape;

public ShapeDecorator(Shape decoratedShape){


this.decoratedShape = decoratedShape;
}
@Override
public void draw(){
decoratedShape.draw();
}
}
—---------------------------------------------------------------------
//concrete decorators:
*
public class BorderColorDecorator extends ShapeDecorator {
private String bcolor="";
public BorderColorDecorator(Shape decoratedShape,String bcolor) {
super(decoratedShape);
this.bcolor=bcolor;
}

@Override
public void draw() {
decoratedShape.draw();
setFillColor(bcolor);
}
@Override
public String description(){
return decoratedShape.description()+"with "+bcolor+" border color ";
}

private void setFillColor(String bcolor){


System.out.println("Border Shape Color: "+bcolor);}}

—-----------------------------------------
*
public class BorderStyleDecorator extends ShapeDecorator {
private String bstyle="";
public BorderStyleDecorator(Shape decoratedShape,String bstyle) {
super(decoratedShape);
this.bstyle=bstyle;
}

@Override
public void draw() {
decoratedShape.draw();
setBorderStyle(bstyle);
}
@Override
public String description(){
return decoratedShape.description()+"with "+bstyle+" border style ";
}
private void setBorderStyle(String bstyle){
System.out.println("Border Shape Style: "+bstyle);
}}

—--------------------------------------------------------------
*
public class BorderThicknessDecorator extends ShapeDecorator {
private double bthickness=0.0;
public BorderThicknessDecorator(Shape decoratedShape,double bthickness) {
super(decoratedShape);
this.bthickness=bthickness;

@Override
public void draw() {
decoratedShape.draw();

setBorderThickness(bthickness);
}
@Override
public String description(){
return decoratedShape.description()+"with "+bthickness+" border thickness ";
}

private void setBorderThickness(double bthickness){


System.out.println("Border Shape Thickness: "+bthickness);
}
}

—---------------------------------------------------------------
//test class
public class test {

public static void main(String[] args) {

Shape circle = new Circle();

Shape redCircle = new FillColorDecorator(new Circle(),"Red");

Shape x = new BorderColorDecorator(


new BorderStyleDecorator(new BorderThicknessDecorator( new FillColorDecorator(new
Circle(), "Red"),2.0)
, "Dashed") , "black");
x.draw();
System.out.println(x.description()+".");

}
}

//Output
The end thanks for your time
import java.awt.BasicStroke;
import java.awt.Graphics2D;

public class BorderColorDecorator implements Shape {


private Shape decoratedShape;
private String borderColor;

public BorderColorDecorator(Shape decoratedShape, String borderColor) {


this.decoratedShape = decoratedShape;
this.borderColor = borderColor;
}

@Override
public void draw(Graphics2D g) {
decoratedShape.draw(g);
setBorderColor(g);
}

@Override
public String description() {
return decoratedShape.description() + " with " + borderColor + " border color";
}

private void setBorderColor(Graphics2D g) {


g.setColor(getAWTColor(borderColor));
// You can set additional properties for the border, e.g., stroke thickness
g.setStroke(new BasicStroke(2));
// Draw the border (you might want to adjust the coordinates based on your requirements)
g.drawRect(100, 100, 200, 200);
}

private java.awt.Color getAWTColor(String color) {


switch (color.toLowerCase()) {
case "red":
return java.awt.Color.RED;
case "green":
return java.awt.Color.GREEN;
case "blue":
return java.awt.Color.BLUE;
default:
return java.awt.Color.BLACK;
}
}
}

import javax.swing.*;
import java.awt.*;

public class testClass extends JFrame {


public testClass() {
setTitle("Shape Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);

Shape circle = new Circle();


Shape rectangle = new Rectangle();
// Create a rectangle with red border
Shape rectangleWithRedBorder = new BorderColorDecorator(new Rectangle(), "Red");

JPanel panel = new JPanel() {


@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// Cast to Graphics2D for advanced features


Graphics2D g2d = (Graphics2D) g;

// Draw the rectangle with red border


rectangleWithRedBorder.draw(g2d);

// // Cast to Graphics2D for advanced features


// Graphics2D g2d = (Graphics2D) g;
//
// // aw the circle
// circle.draw(g2d);
//
// // Draw the rectangle
// rectangle.draw(g2d);
}
};

getContentPane().add(panel);
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
testClass circleTest = new testClass();
circleTest.setVisible(true);
});
}
}

You might also like