import java.awt.image.BufferedImage; import java.io.File;
import javax.imageio.ImageIO;
public class DecoratorPattern {
public static void main(String[] args) throws Exception {
Image colorImage = new ColorImage("Image.png");
decorationTest1(colorImage, "DecoratedImage1");
decorationTest2(colorImage, "DecoratedImage2");
decorationTest3(colorImage, "DecoratedImage3");
decorationTest4(colorImage, "DecoratedImage4");
}
static void decorationTest1(Image colorImage, String outputFileName) throws Exception {
BufferedImage image = colorImage.getImage();
ColorImage.saveToFile(image, outputFileName, "png");
System.out.println("Image is decorated. Saved as " + outputFileName + ".png");
}
static void decorationTest2(Image colorImage, String outputFileName) throws Exception {
Rectangle rect = new Rectangle(100, 100, 650, 500);
Image decoratedImage = new ImageGrayScaleDecorator(colorImage, rect);
BufferedImage image = decoratedImage.getImage();
ColorImage.saveToFile(image, outputFileName, "png");
System.out.println("Image is decorated. Saved as " + outputFileName + ".png");
}
static void decorationTest3(Image colorImage, String outputFileName) throws Exception {
int gridSize = 16;
Rectangle rect = new Rectangle(100, 100, 650, 500);
Image decoratedImage = new ImageMosaicDecorator(new ImageGrayScaleDecorator(colorImage, rect), gridSize);
BufferedImage image = decoratedImage.getImage();
ColorImage.saveToFile(image, outputFileName, "png");
System.out.println("Image is decorated. Saved as " + outputFileName + ".png");
}
static void decorationTest4(Image colorImage, String outputFileName) throws Exception {
int gridSize = 16;
Rectangle rect = new Rectangle(100, 100, 650, 750);
Image decoratedImage = new ImageMosaicDecorator(colorImage, rect, gridSize);
BufferedImage image = decoratedImage.getImage();
ColorImage.saveToFile(image, outputFileName, "png");
System.out.println("Image is decorated. Saved as " + outputFileName + ".png");
}
}
I am struggling about Decorator design pattern. In this code i take an image and saved it as it is first. Then, i tried to make it grey. After, mozaic on it. For other parts, there's nothing wrong (utilities to change image). But because i am new to that concept, i dont have enough understanding this pattern.
Can you please help me how to do that?
interface Image {
BufferedImage getImage();
}
class ColorImage implements Image {
private BufferedImage image;
ColorImage(int width, int height) {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
ColorImage(String fileName) throws Exception {
File imageFile = new File(fileName);
image = ImageIO.read(imageFile);
}
static void saveToFile(BufferedImage image, String fileName, String extension) throws Exception {
File outputfile = new File(fileName + "." + extension);
ImageIO.write(image, extension, outputfile);
}
@Override
public BufferedImage getImage() {
return image;
}
}
class ImageGrayScaleDecorator implements Image {
public ImageGrayScaleDecorator(Image colorImage, Rectangle rect) {
image=new BufferedImage(colorImage, rect);
}
@Override
public BufferedImage getImage() {
BufferedImage rect = null;
return rect;
}
}
class ImageMosaicDecorator implements Image {
public ImageMosaicDecorator(Image colorImage, Rectangle rect, int gridSize) {
}
public ImageMosaicDecorator(ImageGrayScaleDecorator imageGrayScaleDecorator, int gridSize) {
}
@Override
public BufferedImage getImage() {
return null;
}
}
Aucun commentaire:
Enregistrer un commentaire