From 110784a530b5d5436b4abc5abf85c6ddcf5ff25b Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 8 Apr 2026 14:47:42 +0200 Subject: [PATCH] Begin SVG. --- src/ecoparasite/Application.java | 33 +++++++- src/ecoparasite/representation/ValeursXY.java | 39 ++++++++++ src/ecoparasite/svg/Coordonnees.java | 27 +++++++ src/ecoparasite/svg/SVGFactory.java | 76 +++++++++++++++++++ src/ecoparasite/svg/elements/Circle.java | 49 ++++++++++++ src/ecoparasite/svg/elements/Element.java | 31 ++++++++ .../svg/elements/ElementsFactory.java | 68 +++++++++++++++++ src/ecoparasite/svg/elements/Line.java | 76 +++++++++++++++++++ src/ecoparasite/svg/elements/Text.java | 62 +++++++++++++++ tests/ecoparasite/svg/SVGFactoryTest.java | 15 ++++ 10 files changed, 474 insertions(+), 2 deletions(-) create mode 100644 src/ecoparasite/representation/ValeursXY.java create mode 100644 src/ecoparasite/svg/Coordonnees.java create mode 100644 src/ecoparasite/svg/SVGFactory.java create mode 100644 src/ecoparasite/svg/elements/Circle.java create mode 100644 src/ecoparasite/svg/elements/Element.java create mode 100644 src/ecoparasite/svg/elements/ElementsFactory.java create mode 100644 src/ecoparasite/svg/elements/Line.java create mode 100644 src/ecoparasite/svg/elements/Text.java create mode 100644 tests/ecoparasite/svg/SVGFactoryTest.java diff --git a/src/ecoparasite/Application.java b/src/ecoparasite/Application.java index 5fb2a40..05c83e6 100644 --- a/src/ecoparasite/Application.java +++ b/src/ecoparasite/Application.java @@ -1,7 +1,36 @@ package ecoparasite; +import ecoparasite.completion.Completion; +import ecoparasite.input.InputFactory; +import ecoparasite.input.InputFileException; +import ecoparasite.input.RawData; +import ecoparasite.input.RawDataOverflow; +import ecoparasite.nettoyage.Nettoyage; +import ecoparasite.poisson.MackerelSerra; +import ecoparasite.poisson.Poisson; +import ecoparasite.representation.ValeursXY; + +import java.util.HashSet; +import java.util.function.BiConsumer; +import java.util.function.Function; + public class Application { - public static void main(String[] args) { - System.out.println("Hello World"); + public static void main(String[] args) throws InputFileException, RawDataOverflow { + + RawData rawMackerel = InputFactory.readData("test2.csv", ","); + + HashSet mackerelSet = MackerelSerra.parse(rawMackerel); + + System.out.println( mackerelSet ); + + Function getLength = Poisson::getLength; + Function getInfes = Poisson::getInfestation; + BiConsumer setInfes = Poisson::setInfestation; + + mackerelSet = Completion.completeColumnsLinear( mackerelSet, getLength, getInfes, setInfes ); + mackerelSet = Nettoyage.nettoieColumnsLinear( mackerelSet, getLength, getInfes, setInfes, false ); + + HashSet mackerelXY = ValeursXY.convertToXY( mackerelSet, getLength, getInfes ); + } } \ No newline at end of file diff --git a/src/ecoparasite/representation/ValeursXY.java b/src/ecoparasite/representation/ValeursXY.java new file mode 100644 index 0000000..db33d12 --- /dev/null +++ b/src/ecoparasite/representation/ValeursXY.java @@ -0,0 +1,39 @@ +package ecoparasite.representation; + +import java.util.HashSet; +import java.util.function.Function; + +public class ValeursXY { + + private double x; + private double y; + + public ValeursXY(double x, double y){ + this.x = x; + this.y = y; + } + + public double getX() { + return x; + } + public void setX(double x) { + this.x = x; + } + public double getY() { + return y; + } + public void setY(double y) { + this.y = y; + } + + public static HashSet convertToXY(HashSet list, Function getX, Function getY){ + HashSet xy = new HashSet(); + for(T item : list){ + if(getX.apply(item) != null && getY.apply(item) != null){ + xy.add( new ValeursXY(getX.apply(item).doubleValue(), getY.apply(item).doubleValue())); + } + } + return xy; + } + +} diff --git a/src/ecoparasite/svg/Coordonnees.java b/src/ecoparasite/svg/Coordonnees.java new file mode 100644 index 0000000..de24e2e --- /dev/null +++ b/src/ecoparasite/svg/Coordonnees.java @@ -0,0 +1,27 @@ +package ecoparasite.svg; + +public class Coordonnees { + + private double x; + private double y; + + public Coordonnees(double x, double y) { + this.x = x; + this.y = y; + } + + public double getX() { + return x; + } + public void setX(double x) { + this.x = x; + } + public double getY() { + return y; + } + public void setY(double y) { + this.y = y; + } + + +} diff --git a/src/ecoparasite/svg/SVGFactory.java b/src/ecoparasite/svg/SVGFactory.java new file mode 100644 index 0000000..0c695d5 --- /dev/null +++ b/src/ecoparasite/svg/SVGFactory.java @@ -0,0 +1,76 @@ +package ecoparasite.svg; + +import ecoparasite.svg.elements.Element; + +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.UUID; + +public class SVGFactory { + + static final private String EXPORT_PATH = "export/"; + static final private String EXTENSION = ".svg"; + + public static boolean createSVG(ArrayList mesElements){ + + String code = createSVGCode(mesElements); + + try { + createFile(code); + } catch (Exception e) { + return false; + } + + return true; + } + + public static boolean createSVG(ArrayList mesElements, String filename) { + + String code = createSVGCode(mesElements); + + try { + createFile(code,filename); + } catch (Exception e) { + return false; + } + + return true; + } + + public static String createSVGCode(ArrayList mesElements){ + + String code = ""; + + for (Element e : mesElements){ + + code += e.toSVG(); + + } + + code += ""; + + return code; + } + + public static void createFile(String data) throws IOException { + String id = UUID.randomUUID().toString(); + createFile(data,id); + } + + public static void createFile(String data, String filename) throws IOException { + + // create a FileWriter object with the file name + FileWriter writer = new FileWriter(EXPORT_PATH + filename + EXTENSION); + + // write the string to the file + writer.write(data); + + // close the writer + writer.close(); + + System.out.println("Successfully wrote text to file."); + + } + +} diff --git a/src/ecoparasite/svg/elements/Circle.java b/src/ecoparasite/svg/elements/Circle.java new file mode 100644 index 0000000..c76a724 --- /dev/null +++ b/src/ecoparasite/svg/elements/Circle.java @@ -0,0 +1,49 @@ +package ecoparasite.svg.elements; + +import ecoparasite.svg.Coordonnees; + +public class Circle extends Element { + + private int rayon; + private String color; + + public Circle(Coordonnees coordonnees, int rayon, String color) { + super(coordonnees); + this.rayon = rayon; + this.color = color; + } + + public Circle(Coordonnees coordonnees, int rayon) { + super(coordonnees); + this.rayon = rayon; + this.color = ElementsFactory.COLOR_RED; + } + + public int getRayon() { + return rayon; + } + + public void setRayon(int rayon) { + this.rayon = rayon; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + @Override + public String toSVG() { + StringBuilder svg = new StringBuilder(); + svg.append(""); + + return svg.toString(); + } +} diff --git a/src/ecoparasite/svg/elements/Element.java b/src/ecoparasite/svg/elements/Element.java new file mode 100644 index 0000000..dd240ba --- /dev/null +++ b/src/ecoparasite/svg/elements/Element.java @@ -0,0 +1,31 @@ +package ecoparasite.svg.elements; + +import ecoparasite.svg.Coordonnees; + +abstract public class Element { + + protected Coordonnees coordonnees; + + public Element(Coordonnees coordonnees) { + this.coordonnees = coordonnees; + } + + public Coordonnees getCoordonnees() { + return coordonnees; + } + + public void setCoordonnees(Coordonnees coordonnees) { + this.coordonnees = coordonnees; + } + + public void setCoordonnees(int x, int y) { + this.coordonnees = new Coordonnees(x, y); + } + + /** + * Méthode abstraite qui va permettre de transformer notre élément en SVG. + * @return La String SVF + */ + abstract public String toSVG(); + +} diff --git a/src/ecoparasite/svg/elements/ElementsFactory.java b/src/ecoparasite/svg/elements/ElementsFactory.java new file mode 100644 index 0000000..baf5c3f --- /dev/null +++ b/src/ecoparasite/svg/elements/ElementsFactory.java @@ -0,0 +1,68 @@ +package ecoparasite.svg.elements; + +import ecoparasite.svg.Coordonnees; + +import java.util.ArrayList; +import java.util.HashSet; + +public class ElementsFactory { + + final public static int SVG_SIZE = 800; + final public static int SVG_OFFSET = 50; + + final public static int AXES_TEXT_SIZE = 10; + + final public static String COLOR_RED = "red"; + final public static String COLOR_BLUE = "blue"; + final public static String COLOR_BLACK = "black"; + + /** + * Permet de générer les éléments axes du fichier SVG. + * @return + */ + public static ArrayList SVGAxes(String HName, String VName ){ + + final int begin = SVG_OFFSET + AXES_TEXT_SIZE + (AXES_TEXT_SIZE / 2); + final int end = SVG_SIZE - SVG_OFFSET - AXES_TEXT_SIZE - (AXES_TEXT_SIZE / 2); + + Element V = new Line( + new Coordonnees( begin, begin ), + new Coordonnees( begin, SVG_SIZE - SVG_OFFSET ), + COLOR_BLACK, + 2 + ); + + Element H = new Line( + new Coordonnees( begin, SVG_SIZE - SVG_OFFSET ), + new Coordonnees( end, SVG_SIZE - SVG_OFFSET ), + COLOR_BLACK, + 2 + ); + + Element VLabel = new Text( + new Coordonnees( SVG_OFFSET, SVG_OFFSET ), + VName, + COLOR_BLACK, + AXES_TEXT_SIZE + ); + Element HLabel = new Text( + new Coordonnees( SVG_SIZE - SVG_OFFSET - AXES_TEXT_SIZE, SVG_SIZE - SVG_OFFSET - AXES_TEXT_SIZE ), + HName, + COLOR_BLACK, + AXES_TEXT_SIZE + ); + + ArrayList SVGAxes = new ArrayList<>(); + SVGAxes.add(V); + SVGAxes.add(H); + SVGAxes.add(VLabel); + SVGAxes.add(HLabel); + return SVGAxes; + + } + + public static ArrayList SVGAxes(){ + return SVGAxes( "None", "None" ); + } + +} \ No newline at end of file diff --git a/src/ecoparasite/svg/elements/Line.java b/src/ecoparasite/svg/elements/Line.java new file mode 100644 index 0000000..6562367 --- /dev/null +++ b/src/ecoparasite/svg/elements/Line.java @@ -0,0 +1,76 @@ +package ecoparasite.svg.elements; + +import ecoparasite.svg.Coordonnees; + +public class Line extends Element { + + private Coordonnees coordonneesB; + private String color; + private int lineWidth; + + public Line(Coordonnees coordonneesA, Coordonnees coordonneesB, String color, int lineWidth) { + super(coordonneesA); + this.coordonneesB = coordonneesB; + this.color = color; + this.lineWidth = lineWidth; + } + + public Line(Coordonnees coordonneesA, Coordonnees coordonneesB) { + super(coordonneesA); + this.coordonneesB = coordonneesB; + this.color = ElementsFactory.COLOR_RED; + this.lineWidth = 1; + } + + public Coordonnees getCoordonneesA() { + return coordonnees; + } + + public void setCoordonneesA(Coordonnees coordonnees) { + this.coordonnees = coordonnees; + } + + public Coordonnees getCoordonneesB() { + return coordonneesB; + } + + public void setCoordonneesB(Coordonnees coordonnees) { + this.coordonneesB = coordonnees; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public int getLineWidth() { + return lineWidth; + } + + public void setLineWidth(int lineWidth) { + this.lineWidth = lineWidth; + } + + @Override + public String toSVG() { + StringBuilder svg = new StringBuilder(); + svg.append("" ); + + return svg.toString(); + } + +} diff --git a/src/ecoparasite/svg/elements/Text.java b/src/ecoparasite/svg/elements/Text.java new file mode 100644 index 0000000..9850dcf --- /dev/null +++ b/src/ecoparasite/svg/elements/Text.java @@ -0,0 +1,62 @@ +package ecoparasite.svg.elements; + +import ecoparasite.svg.Coordonnees; + +public class Text extends Element { + + private String text; + private String color; + private int size; + + public Text(Coordonnees coordonnees, String text, String color, int size) { + super(coordonnees); + this.text = text; + this.color = color; + this.size = size; + } + + public Text(Coordonnees coordonnees, String text) { + super(coordonnees); + this.text = text; + this.color = ElementsFactory.COLOR_BLACK; + this.size = ElementsFactory.AXES_TEXT_SIZE; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + @Override + public String toSVG() { + StringBuilder svg = new StringBuilder(); + svg.append(""); + svg.append( this.text ); + svg.append(""); + return svg.toString(); + } + +} diff --git a/tests/ecoparasite/svg/SVGFactoryTest.java b/tests/ecoparasite/svg/SVGFactoryTest.java new file mode 100644 index 0000000..986afe6 --- /dev/null +++ b/tests/ecoparasite/svg/SVGFactoryTest.java @@ -0,0 +1,15 @@ +package ecoparasite.svg; + +import ecoparasite.svg.elements.ElementsFactory; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class SVGFactoryTest { + + @Test + public void generateSVGAxes(){ + SVGFactory.createSVG( ElementsFactory.SVGAxes() ); + } + +} \ No newline at end of file -- 2.39.5