Professional Documents
Culture Documents
//interface
public interface Shape {
void draw();
String description();
}
—------------------------------------------------------------------------------
@Override
public void draw() {
System.out.println("Shape: Rectangle is drawing");
}
@Override
public String description(){
return "This is a Rectangle ";
}
}
—----------------------------------------------------------------------------------
//concrete class2 Circle
@Override
public String description(){
return "This is a Circle ";
}
}
—-----------------------------------------------------------------------------------
// (main decorator)
@Override
public void draw() {
decoratedShape.draw();
setFillColor(bcolor);
}
@Override
public String description(){
return decoratedShape.description()+"with "+bcolor+" border color ";
}
—-----------------------------------------
*
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 ";
}
—---------------------------------------------------------------
//test class
public class test {
}
}
//Output
The end thanks for your time
import java.awt.BasicStroke;
import java.awt.Graphics2D;
@Override
public void draw(Graphics2D g) {
decoratedShape.draw(g);
setBorderColor(g);
}
@Override
public String description() {
return decoratedShape.description() + " with " + borderColor + " border color";
}
import javax.swing.*;
import java.awt.*;
getContentPane().add(panel);
}