diff --git a/src/main/java/image/AbstractImage.java b/src/main/java/image/AbstractImage.java new file mode 100644 index 0000000000000000000000000000000000000000..8eefeff7676a9ecc02823cf92072ff6de38f6279 --- /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 0000000000000000000000000000000000000000..bf69d2096ed35481487e4c7874dfb6e2290dccc8 --- /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 0000000000000000000000000000000000000000..671a5cd2b6c9f6dc4caf38b0454a40986e820fa5 --- /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 0000000000000000000000000000000000000000..6f00bc3ced10d9439e5f3c8d252ec19c0ec93d41 --- /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 0000000000000000000000000000000000000000..c15fbb14ce950519ac3be31a025e2a9790b967c0 --- /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 68ceaaac38e00336a9980325625bc3bd5d7abecc..c8fd1e03cf98813d79ad518efff1cd514d5c9509 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 0000000000000000000000000000000000000000..b4401aaea4f8bb85017263ec338266b56820f9aa --- /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 0000000000000000000000000000000000000000..d337d8aa3f1a16219679827f0f9b922c6f7c62e4 --- /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 0000000000000000000000000000000000000000..4057d6acf57c7fd720b182432c2fc153612dc889 --- /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 0000000000000000000000000000000000000000..4e7bcd6395758e844f352babd7f67f8e2a8dcc94 --- /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 0000000000000000000000000000000000000000..917c30f2caf434b15cbf77cfd6f3a6388e726b32 --- /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 0000000000000000000000000000000000000000..c10478cf59bdaac8c5e809613bd167f74ab3c138 --- /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 0000000000000000000000000000000000000000..a53495ccca5e814c1bf35ef82648d0eacd648f08 --- /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 a77b7b1dc86c61f22f846698d49544296f0f1b44..7efeffdad95bdb7c743ec0ee3c1610fe47b9da9a 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();