Skip to content

Instantly share code, notes, and snippets.

@Dysta
Created September 19, 2019 10:49
Show Gist options
  • Save Dysta/80098749541b5ac186c3f4c87f3e2b08 to your computer and use it in GitHub Desktop.
Save Dysta/80098749541b5ac186c3f4c87f3e2b08 to your computer and use it in GitHub Desktop.
TD2 de PO - UB
package fr.ubordeaux.ao;
public class Circle extends Shape {
private int radius;
public int getRadius() {
return this.radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public Circle(int x, int y, int radius) {
super(x, y);
this.setRadius(radius);
}
public String toSVG() {
return String.format("<circle cx=\"%d\" cy=\"%d\" r=\"%d\" fill=\"black\" />",
this.getX(), this.getY(), this.getRadius());
}
}
package fr.ubordeaux.ao;
public class Ellipse extends Circle {
private int radiusY;
public int getRadiusY() {
return this.radiusY;
}
public void setRadiusY(int radiusY) {
this.radiusY = radiusY;
}
public Ellipse (int x, int y, int rx, int ry) {
super(x, y, rx);
this.setRadiusY(ry);
}
public String toSVG() {
return String.format("<ellipse cx=\"%d\" cy=\"%d\" rx=\"%d\" ry=\"%d\" fill=\"black\" />",
this.getX(), this.getY(), this.getRadius(), this.getRadiusY());
}
}
package fr.ubordeaux.ao;
public class Line extends Shape {
private int y2;
private int x2;
public int getY2() {
return this.y2;
}
public void setY2(int y2) {
this.y2 = y2;
}
public int getX2() {
return this.x2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public Line(int x, int y, int x2, int y2) {
super(x, y);
this.setX2(x2);
this.setY2(y2);
}
public String toSVG() {
return String.format("<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke=\"black\" stroke-width=\"2\" />",
this.getX(), this.getY(), this.getX2(), this.getY2());
}
}
package main.java.fr.ubordeaux.ao; // idk why j'ai du mettre main.java devant fr pour compiler
import java.io.IOException;
import fr.ubordeaux.ao.*;
public class Main {
public static void main(String[] args) {
Rectangle r = new Rectangle(0, 0, 100, 100);
Circle c = new Circle(20, 150, 20);
Ellipse e = new Ellipse(60, 250, 20, 60);
SVG bg = new SVG(500, 500);
bg.addShape(r);
bg.addShape(c);
bg.addShape(e);
try {
bg.export("sex.html");
} catch (IOException err) {
System.out.println(err.getMessage());
}
}
}
package fr.ubordeaux.ao;
public class Rectangle extends Shape {
private int width;
private int height;
public int getWidth() {
return this.width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return this.height;
}
public void setHeight(int height) {
this.height = height;
}
public Rectangle(int x, int y, int width, int height) {
super(x, y);
this.setWidth(width);
this.setHeight(height);
}
public String toSVG() {
return String.format("<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" fill=\"black\" />",
this.getX(), this.getY(), this.getWidth(), this.getHeight());
}
}
package fr.ubordeaux.ao;
public abstract class Shape {
private int x;
private int y;
public int getX() {
return this.x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return this.y;
}
public void setY(int y) {
this.y = y;
}
public Shape(int x, int y) {
this.setX(x);
this.setY(y);
}
abstract String toSVG();
}
package fr.ubordeaux.ao;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class SVG {
private int width;
private int heigth;
private ArrayList<Shape> shapeList;
public int getWidth() {
return this.width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeigth() {
return this.heigth;
}
public void setHeigth(int heigth) {
this.heigth = heigth;
}
public SVG(int width, int heigth) {
this.setWidth(width);
this.setHeigth(heigth);
this.shapeList = new ArrayList<Shape>();
}
public void addShape(Shape s) {
this.shapeList.add(s);
}
private String generateHeader(String title) {
return String.format("<!DOCTYPE html><html><head><title>%s</title><body><h1>%s</h1><svg width=\"%d\" height=\"%d\">",
title, title, this.getWidth(), this.getHeigth());
}
private String generateFooter() {
return "</svg></body></html>";
}
public void export(String filename) throws IOException {
FileWriter fw = new FileWriter(filename);
String header = this.generateHeader(filename);
String footer = this.generateFooter();
fw.write(header);
for (Shape s : shapeList) {
fw.write(s.toSVG());
}
fw.write(footer);
fw.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment