From a07ddff5ce7ec899e146fa27a10ebe207bccd53c Mon Sep 17 00:00:00 2001 From: arnaudlabourel <arnaud.labourel@univ-amu.fr> Date: Fri, 17 Sep 2021 16:47:35 +0200 Subject: [PATCH] Correction finale --- src/main/java/image/AbstractImage.java | 41 ++++++++++++++++ src/main/java/image/BruteRasterImage.java | 31 ++++++++++++ src/main/java/image/LogoLISFactory.java | 27 +++++++++++ .../java/image/NotSupportedException.java | 10 ++++ src/main/java/image/PaletteRasterImage.java | 46 ++++++++++++++++++ src/main/java/image/Point.java | 3 +- src/main/java/image/RasterFlagFactory.java | 43 +++++++++++++++++ src/main/java/image/RasterImage.java | 48 +++++++++++++++++++ src/main/java/image/RasterImageType.java | 8 ++++ .../java/image/RasterUniformImageFactory.java | 32 +++++++++++++ src/main/java/image/Rectangle.java | 33 +++++++++++++ src/main/java/image/SparseRasterImage.java | 38 +++++++++++++++ src/main/java/image/VectorImage.java | 27 +++++++++++ src/main/java/viewer/Display.java | 9 ++-- 14 files changed, 389 insertions(+), 7 deletions(-) create mode 100644 src/main/java/image/AbstractImage.java create mode 100644 src/main/java/image/BruteRasterImage.java create mode 100644 src/main/java/image/LogoLISFactory.java create mode 100644 src/main/java/image/NotSupportedException.java create mode 100644 src/main/java/image/PaletteRasterImage.java create mode 100644 src/main/java/image/RasterFlagFactory.java create mode 100644 src/main/java/image/RasterImage.java create mode 100644 src/main/java/image/RasterImageType.java create mode 100644 src/main/java/image/RasterUniformImageFactory.java create mode 100644 src/main/java/image/Rectangle.java create mode 100644 src/main/java/image/SparseRasterImage.java create mode 100644 src/main/java/image/VectorImage.java diff --git a/src/main/java/image/AbstractImage.java b/src/main/java/image/AbstractImage.java new file mode 100644 index 0000000..8eefeff --- /dev/null +++ b/src/main/java/image/AbstractImage.java @@ -0,0 +1,41 @@ +package image; + +import javafx.scene.paint.Color; + +/** + * Created by Arnaud Labourel on 23/11/2018. + */ +public abstract class AbstractImage implements Image{ + private int width; + private int height; + + AbstractImage(int width, int height) { + this.width = width; + this.height = height; + } + + AbstractImage() { + this(0,0); + } + + @Override + public abstract Color getPixelColor(int x, int y); + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + + void setWidth(int width) { + this.width = width; + } + + void setHeight(int height) { + this.height = height; + } +} diff --git a/src/main/java/image/BruteRasterImage.java b/src/main/java/image/BruteRasterImage.java new file mode 100644 index 0000000..bf69d20 --- /dev/null +++ b/src/main/java/image/BruteRasterImage.java @@ -0,0 +1,31 @@ +package image; +import javafx.scene.paint.Color; + +public class BruteRasterImage extends RasterImage { + + private Color[][] pixels; + + BruteRasterImage(Color color, int width, int height){ + super(color, width, height); + } + + BruteRasterImage(Color[][] pixels) { + super(pixels); + this.pixels = pixels; + } + + @Override + public void createRepresentation() { + pixels = new Color[getWidth()][getHeight()]; + } + + @Override + public void setPixelColor(Color color, int x, int y) { + pixels[x][y] = color; + } + + @Override + public Color getPixelColor(int x, int y) { + return pixels[x][y]; + } +} diff --git a/src/main/java/image/LogoLISFactory.java b/src/main/java/image/LogoLISFactory.java new file mode 100644 index 0000000..671a5cd --- /dev/null +++ b/src/main/java/image/LogoLISFactory.java @@ -0,0 +1,27 @@ +package image; + +import javafx.scene.paint.Color; + +import java.util.ArrayList; +import java.util.List; + +public class LogoLISFactory implements ImageFactory{ + @Override + public Image makeImage() { + Color dark = Color.rgb(35,31,32); + Color greenblue = Color.rgb(113,208,199); + + int[] xCoordinates = {60, 720, 660, 400, 0, 400, 660}; + int[] yCoordinates = {0, 60, 140, 0, 0, 140, 0}; + int[] widths = {140, 140, 140, 60, 200, 60, 200}; + int[] heights = {280, 80, 140, 80, 340, 200, 340}; + Color[] colors = {Color.WHITE, Color.WHITE, Color.WHITE, greenblue, dark, dark, dark}; + + List<Shape> list = new ArrayList<>(); + + for (int i = 0; i<colors.length; i++) + list.add(new Rectangle(xCoordinates[i], yCoordinates[i], widths[i], heights[i], colors[i])); + + return new VectorImage(list, 860, 340); + } +} diff --git a/src/main/java/image/NotSupportedException.java b/src/main/java/image/NotSupportedException.java new file mode 100644 index 0000000..6f00bc3 --- /dev/null +++ b/src/main/java/image/NotSupportedException.java @@ -0,0 +1,10 @@ +package image; + +/** + * Created by Arnaud Labourel on 25/11/2018. + */ +class NotSupportedException extends RuntimeException { + NotSupportedException(String message) { + super(message); + } +} diff --git a/src/main/java/image/PaletteRasterImage.java b/src/main/java/image/PaletteRasterImage.java new file mode 100644 index 0000000..c15fbb1 --- /dev/null +++ b/src/main/java/image/PaletteRasterImage.java @@ -0,0 +1,46 @@ +package image; + +import javafx.scene.paint.Color; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Arnaud Labourel on 23/11/2018. + */ +public class PaletteRasterImage extends RasterImage { + + private List<Color> palette; + private int[][] indexesOfColors; + + PaletteRasterImage(Color color, int width, int height) { + super(color, width, height); + } + + PaletteRasterImage(Color[][] pixels) { + super(pixels); + } + + @Override + public void setPixelColor(Color color, int x, int y) { + int indexOfColor = palette.indexOf(color); + + if(indexOfColor < 0){ + palette.add(color); + indexOfColor = palette.indexOf(color); + } + + indexesOfColors[x][y] = indexOfColor; + } + + @Override + public void createRepresentation() { + palette = new ArrayList<>(); + indexesOfColors = new int[getWidth()][getHeight()]; + } + + @Override + public Color getPixelColor(int x, int y) { + return palette.get(indexesOfColors[x][y]); + } +} diff --git a/src/main/java/image/Point.java b/src/main/java/image/Point.java index 68ceaaa..c8fd1e0 100644 --- a/src/main/java/image/Point.java +++ b/src/main/java/image/Point.java @@ -16,7 +16,8 @@ public class Point { @Override public final boolean equals(Object o) { if (this == o) return true; - if (!(o instanceof Point point)) return false; + if (!(o instanceof Point)) return false; + Point point = (Point) o; return x == point.x && y == point.y; } diff --git a/src/main/java/image/RasterFlagFactory.java b/src/main/java/image/RasterFlagFactory.java new file mode 100644 index 0000000..b4401aa --- /dev/null +++ b/src/main/java/image/RasterFlagFactory.java @@ -0,0 +1,43 @@ +package image; + +import javafx.scene.paint.Color; + +public class RasterFlagFactory implements ImageFactory { + + private int width; + private int height; + private Color leftColor; + private Color middleColor; + private Color rightColor; + private RasterImageType rasterImageType; + + public RasterFlagFactory(int width, int height, Color leftColor, Color middleColor, Color rightColor, RasterImageType rasterImageType) { + this.width = width; + this.height = height; + this.leftColor = leftColor; + this.middleColor = middleColor; + this.rightColor = rightColor; + this.rasterImageType = rasterImageType; + } + + @Override + public Image makeImage() { + Color[][] colors = new Color[width][height]; + + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + colors[x][y] = (x < width / 3) ? leftColor : ((x > 2 * width / 3) ? rightColor : middleColor); + } + } + switch (rasterImageType){ + case BRUTE: + return new BruteRasterImage(colors); + case SPARSE: + return new SparseRasterImage(colors); + case PALETTE: + return new PaletteRasterImage(colors); + default: + throw new NotSupportedException(rasterImageType + " is not supported"); + } + } +} diff --git a/src/main/java/image/RasterImage.java b/src/main/java/image/RasterImage.java new file mode 100644 index 0000000..d337d8a --- /dev/null +++ b/src/main/java/image/RasterImage.java @@ -0,0 +1,48 @@ +package image; + +import javafx.scene.paint.Color; +import util.Matrices; + +/** + * Created by Arnaud Labourel on 23/11/2018. + */ +public abstract class RasterImage extends AbstractImage { + + RasterImage(Color[][] colors) { + + Matrices.requiresNonNull(colors); + Matrices.requiresNonZeroDimensions(colors); + Matrices.requiresRectangularMatrix(colors); + + setWidth(Matrices.getRowCount(colors)); + setHeight(Matrices.getColumnCount(colors)); + + createRepresentation(); + setPixelsColor(colors); + } + + RasterImage(Color color, int width, int height) { + super(width, height); + + createRepresentation(); + setPixelsColor(color); + } + + public abstract void setPixelColor(Color color, int x , int y); + + public abstract void createRepresentation(); + + private void setPixelsColor(Color[][] pixels){ + for(int x = 0; x<getWidth(); x++) + for(int y = 0; y<getHeight(); y++){ + setPixelColor(pixels[x][y], x, y); + } + } + + private void setPixelsColor(Color color){ + for(int x = 0; x<getWidth(); x++) + for(int y = 0; y<getHeight(); y++){ + setPixelColor(color, x, y); + } + } +} diff --git a/src/main/java/image/RasterImageType.java b/src/main/java/image/RasterImageType.java new file mode 100644 index 0000000..4057d6a --- /dev/null +++ b/src/main/java/image/RasterImageType.java @@ -0,0 +1,8 @@ +package image; + +/** + * Created by Arnaud Labourel on 25/11/2018. + */ +public enum RasterImageType { + BRUTE, PALETTE, SPARSE +} diff --git a/src/main/java/image/RasterUniformImageFactory.java b/src/main/java/image/RasterUniformImageFactory.java new file mode 100644 index 0000000..4e7bcd6 --- /dev/null +++ b/src/main/java/image/RasterUniformImageFactory.java @@ -0,0 +1,32 @@ +package image; + +import javafx.scene.paint.Color; + + +public class RasterUniformImageFactory implements ImageFactory { + private int width; + private int height; + private Color color; + private RasterImageType rasterImageType; + + public RasterUniformImageFactory(int width, int height, Color color, RasterImageType rasterImageType) { + this.width = width; + this.height = height; + this.color = color; + this.rasterImageType = rasterImageType; + } + + @Override + public Image makeImage() { + switch (rasterImageType){ + case PALETTE: + return new PaletteRasterImage(color, width, height); + case SPARSE: + return new SparseRasterImage(color, width, height); + case BRUTE: + return new BruteRasterImage(color, width, height); + default: + throw new NotSupportedException(rasterImageType + " is not supported"); + } + } +} diff --git a/src/main/java/image/Rectangle.java b/src/main/java/image/Rectangle.java new file mode 100644 index 0000000..917c30f --- /dev/null +++ b/src/main/java/image/Rectangle.java @@ -0,0 +1,33 @@ +package image; + +import javafx.scene.paint.Color; + +/** + * Created by Arnaud Labourel on 23/11/2018. + */ +public class Rectangle implements Shape { + private int x; + private int y; + private int width; + private int height; + Color color; + + Rectangle(int x, int y, int width, int height, Color color) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + this.color = color; + } + + @Override + public boolean contains(Point point) { + return x <= point.x && point.x <= x+width + && y <= point.y && point.y <= y+height; + } + + @Override + public Color getColor() { + return color; + } +} diff --git a/src/main/java/image/SparseRasterImage.java b/src/main/java/image/SparseRasterImage.java new file mode 100644 index 0000000..c10478c --- /dev/null +++ b/src/main/java/image/SparseRasterImage.java @@ -0,0 +1,38 @@ +package image; + +import javafx.scene.paint.Color; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Arnaud Labourel on 23/11/2018. + */ +public class SparseRasterImage extends RasterImage{ + + private Map<Point,Color> pixelsMap; + + SparseRasterImage(Color color, int width, int height) { + super(color, width, height); + } + + SparseRasterImage(Color[][] pixels) { + super(pixels); + } + + @Override + public Color getPixelColor(int x, int y) { + return pixelsMap.getOrDefault(new Point(x,y), Color.WHITE); + } + + @Override + public void setPixelColor(Color color, int x, int y) { + if (!color.equals(Color.WHITE)) + pixelsMap.put(new Point(x,y), color); + } + + @Override + public void createRepresentation() { + pixelsMap = new HashMap<>(); + } +} diff --git a/src/main/java/image/VectorImage.java b/src/main/java/image/VectorImage.java new file mode 100644 index 0000000..a53495c --- /dev/null +++ b/src/main/java/image/VectorImage.java @@ -0,0 +1,27 @@ +package image; + +import javafx.scene.paint.Color; + +import java.util.List; + +/** + * Created by Arnaud Labourel on 23/11/2018. + */ +public class VectorImage extends AbstractImage { + + private List<Shape> shapes; + + VectorImage(List<Shape> shapes, int width, int height) { + super(width, height); + this.shapes = shapes; + } + + @Override + public Color getPixelColor(int x, int y) { + for(Shape shape : shapes){ + if(shape.contains(new Point(x,y))) + return shape.getColor(); + } + return Color.WHITE; + } +} diff --git a/src/main/java/viewer/Display.java b/src/main/java/viewer/Display.java index a77b7b1..7efeffd 100644 --- a/src/main/java/viewer/Display.java +++ b/src/main/java/viewer/Display.java @@ -1,13 +1,12 @@ package viewer; -import image.BlankImageFactory; -import image.Image; -import image.ImageFactory; +import image.*; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.PixelWriter; +import javafx.scene.paint.Color; import java.net.URL; import java.util.ResourceBundle; @@ -20,12 +19,10 @@ public class Display implements Initializable { private Canvas canvas; private Image image; - private ImageFactory imageFactory; @Override public void initialize(URL location, ResourceBundle resources) { - imageFactory = new BlankImageFactory(); - // TODO : changer la fabrique d'image pour construire des images. + ImageFactory imageFactory = new RasterFlagFactory(900, 600, Color.BLUE, Color.WHITE, Color.RED, RasterImageType.BRUTE); this.image = imageFactory.makeImage(); -- GitLab