I dont have a problem doing 1 but I am having trouble actual
I don\'t have a problem doing #1, but I am having trouble actually coding these classes in Java so it can be converted to svg.
For the SvgMaker class this is what I have so far. I don\'t know if it\'s correct.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class SvgMaker
{
public int imWidth;
public int imHeight;
public String fileName;
private PrintWriter fp;
public void setFp(PrintWriter fp_in)
{
fp = fp_in;
}
public PrintWriter getFp()
{
return fp;
}
public SvgMaker(int imWidth_in, int imHeight_in, String fileName_in)
{
// Capture incoming member variables
imWidth = imWidth_in;
imHeight = imHeight_in;
fileName = fileName_in;
// Open File
fileName = \"C:/Users/mvanc/OneDrive/Documents/svg.txt\";
PrintWriter outFile = null;
// Write opening svg tag to the file
try
{
outFile = new PrintWriter(new File(fileName));
outFile.print(\"
outFile.print(\" xmlns = \'http://www.w3.org/2000/svg\'\ \");
outFile.print(\" xmlns:xlink = \'http://www.w3.org/1999/xlink\'\ \");
outFile.print(\" version = \'1.1\'\ \");
outFile.print(\" width = \" + \"\'\" + imWidth + \"\'\" + \"height = \" + \"\'\" + imHeight + \"\'>\ \");
}
catch(FileNotFoundException ex)
{
System.out.print(\"\ Error opening file: \" + fileName);
}
}
public void drawCircle(Circle c, Fill fc, Stroke sc)
{
}
public void drawRectangle(Rectangle r, Fill fr, Stroke sr)
{
}
public void drawRoundedRectangle(RoundedRectangle rr, Fill frr, Stroke srr)
{
}
public void drawLine(Line l, Stroke sl)
{
}
public void drawPath(Path p, Fill fp, Stroke sp)
{
}
public void close()
{
// Write closing svg tag to file
fileName = \"C:/Users/mvanc/OneDrive/Documents/svg.txt\";
PrintWriter outFile = null;
try
{
outFile = new PrintWriter(new File(fileName));
outFile.print(\" />\");
outFile.print(\"\");
}
catch(FileNotFoundException ex)
{
System.out.print(\"\ Error opening file: \" + fileName);
}
// Close file
outFile.flush();
outFile.close();
}
}
Circle class:
import java.io.File;
import java.io.PrintWriter;
public class Circle
{
int cx;
int cy;
int r;
public Circle(int cx_in, int cy_in, int r_in)
{
cx = cx_in;
cy = cy_in;
r = r_in;
PrintWriter outFile = null;
outFile.print(\"
outFile.print(\" r = \" + \"\'\" + r + \"\'\ \");
}
}
Dashed Stroke Class:
import java.io.PrintWriter;
public class DashedStroke extends Stroke // Child class of Stroke
{
int d1; // First dash
int d2; // Second dash
public DashedStroke(String s_in, int sw_in, int d1, int d2)
{
// Call parent constructor
super (s_in, sw_in);
this.d1 = d1;
this.d2 = d2;
PrintWriter outFile = null;
outFile.print(\" stroke = \'\" + s + \"\' stroke-width = \'\" + sw + \"\'\ \");
outFile.print(\" stroke-dasharray = \'\" + d1 + \",\" + d2 + \"\'\ \");
}
}
Fill Class:
import java.io.PrintWriter;
public class Fill // rgb function
{
int r; // Red
int g; // Green
int b; // Blue
public Fill(int r_in, int g_in, int b_in)
{
r = r_in;
g = g_in;
b = b_in;
PrintWriter outFile = null;
outFile.print(\" fill = \'rgb(\" + r + \",\" + g + \",\" + b + \")\'\ \");
}
}
Line Class:
import java.io.PrintWriter;
public class Line
{
int x1;
int y1;
int x2;
int y2;
public Line(int x1_in, int y1_in, int x2_in, int y2_in)
{
x1 = x1_in;
y1 = y1_in;
x2 = x2_in;
y2 = y2_in;
PrintWriter outFile = null;
outFile.print(\"<line x1=\"" + x1 + "\" y1=\"" + y1 + "\" \ \");
outFile.print(\" x2 = \'\" + x2 + \"\' y2 = \'\" + y2 + \"\'\ \");
}
}
Path Class:
import java.io.PrintWriter;
public class Path
{
int x1;
int y1;
int x2;
int y2;
int x3;
int y3;
public Path(int x1_in, int y1_in, int x2_in, int y2_in, int x3_in, int y3_in)
{
x1 = x1_in;
y1 = y1_in;
x2 = x2_in;
y2 = y2_in;
x3 = x3_in;
y3 = y3_in;
PrintWriter outFile = null;
outFile.print(\"<path d=\"M" + x1 + " " + y1 + "L" + x2 + " " + y2 + "L" + x3 + " " + y3 + "z\" \ \");
}
Rectangle Class:
import java.io.PrintWriter;
public class Rectangle // Parent Class to RoundedRectangle
{
int x;
int y;
int width;
int height;
public Rectangle(int x_in, int y_in, int width_in, int height_in)
{
x = x_in;
y = y_in;
width = width_in;
height = height_in;
PrintWriter outFile = null;
outFile.print(\"<rect x=\"" + x + "\" y=\"" + y + "\" \ \");
outFile.print(\" width = \'\" + width + \"\' height = \'\"+ height + \"\'\ \");
}
}
RoundedRectangle Class:
import java.io.PrintWriter;
public class RoundedRectangle extends Rectangle // Child Class of Rectangle
{
int r;
public RoundedRectangle(int x_in, int y_in, int width_in, int height_in, int r)
{
// Call parent constructor
super (x_in, y_in, width_in, height_in);
this.r = r;
PrintWriter outFile = null;
outFile.print(\"<rect x=\"" + x + "\" y=\"" + y + "\" \ \");
outFile.print(\" width = \'\" + width + \"\' height = \'\"+ height + \"\'\ \");
outFile.print(\" r = \'\" + r + \"\'\ \");
}
}
Stroke Class:
import java.io.PrintWriter;
public class Stroke // Parent Class to DashedStroke
{
String s; // Stroke
int sw; // Stroke-width
public Stroke(String s_in, int sw_in)
{
s = s_in;
sw = sw_in;
PrintWriter outFile = null;
outFile.print(\" stroke = \'\" + s + \"\' stroke-width = \'\" + sw + \"\'\ \");
}
}
Test Class:
public class TestClass
{
public static void main(String[] args)
{
SvgMaker svg = new SvgMaker(300, 300, \"C:/Users/mvanc/OneDrive/Documents/svg.txt\");
Circle c = new Circle(2, 1, 6);
Fill fc = new Fill(0,0, 255);
Stroke sc = new Stroke(\"black\", 3);
Rectangle r = new Rectangle(10, 20, 5, 5);
Fill fr = new Fill(255, 0, 0);
Stroke sr = new Stroke(\"blue\", 2);
RoundedRectangle rr = new RoundedRectangle(10, 20, 5, 5, 2);
Fill frr = new Fill(255, 0, 255);
Stroke srr = new Stroke(\"red\", 7);
Line l = new Line(3, 6, 5, 10);
Stroke sl = new Stroke(\"blue\", 2);
Path p = new Path(50, 50, 250, 50, 100, 200);
Fill fp = new Fill(255, 255, 255);
Stroke sp = new Stroke(\"black\", 1);
}
}
1. Create a UML diagram for the following classes: Fill Stroke Line Circle Rectangle SvgMaker The svgMaker class has a member variable of the file pointer (a iter object), the image width, the image height, and all the drawing methods, such as drawLine, etc. 2. Create the following child classes: Rounded Rectangle Dashed stroke 3. Code the classes in Java and create a Testclass to test out your code. 4. (a) Use your classes to create the algorithmic image shown below (b) Modify the algorithm in some interesting way or else create your own interesting algorithm and create another image.Solution
TestClass.java
import java.util.ArrayList;
public class TestClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
SvgMaker svg = new SvgMaker(\"Quiz4.svg\",750,750);
RoundedRectangle background = new RoundedRectangle(100,100,300,400,50);
Rectangle BaseLeft = new Rectangle(150,350,25,75);
Rectangle BaseRight = new Rectangle(375,350,25,75);
Rectangle LegLeft = new Rectangle(175,150,200,25);
Rectangle LegRight = new Rectangle(400,150,200,25);
Line LeftAngle = new Line(190,165,310,295);
Line RightAngle = new Line(415,165,290,295);
Line BorderTop = new Line(100,100,500,100);
Line BorderBottom = new Line(100,400,500,400);
Line BorderRight = new Line(100,100,100,400);
Line BorderLeft = new Line(500,100,500,400);
Stroke AngleStroke = new Stroke(255,255,0,25);
Fill BLUE = new Fill(0,0,255,1.0);
Fill BLACK = new Fill(0,0,0,1.0);
Fill YELLOW = new Fill(255,255,0,1.0);
Stroke NO_STROKE = new Stroke(0,0,0,0);
DashedStroke BLACK_DASH = new DashedStroke(0,0,0,5,10,10);
svg.drawRoundedRectangle(background, NO_STROKE, BLUE);
svg.drawRectangle(BaseLeft, NO_STROKE, YELLOW);
svg.drawRectangle(BaseRight, NO_STROKE, YELLOW);
svg.drawRectangle(LegLeft, NO_STROKE, YELLOW);
svg.drawRectangle(LegRight, NO_STROKE, YELLOW);
svg.drawLine(LeftAngle, AngleStroke);
svg.drawLine(RightAngle, AngleStroke);
svg.drawDashedLine(BorderTop, BLACK_DASH);
svg.drawDashedLine(BorderBottom, BLACK_DASH);
svg.drawDashedLine(BorderLeft, BLACK_DASH);
svg.drawDashedLine(BorderRight, BLACK_DASH);
svg.close();
}
private void ArrayListExample()
{
ArrayList<Integer> L;
L = new ArrayList<Integer>();
for(int i=0;i<100;i++)
{
L.add(i,i);
}
ArrayList<Fill> myList = new ArrayList<Fill>();
for(int i=0;i<10;i++)
{
Fill z = new Fill(255,255,255,1.0);
z.randomRGB(1.0);
myList.add(z);
}
for(int i=0;i<myList.size();i++)
{
System.out.println(myList.get(i).getRGB());
}
}
}
Circle.java
public class Circle {
private int x;
private int y;
private int radius;
Circle(int x_in, int y_in, int radius_in)
{
x= x_in;
y = y_in;
radius = radius_in;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getRadius() {
return radius;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setRadius(int radius) {
this.radius = radius;
}
}
SvgMaker.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class SvgMaker {
private PrintWriter outFile;
private int imWidth;
private int imHeight;
public SvgMaker(String fileName, int imWidth_in, int imHeight_in)
{
String output;
imWidth = imWidth_in;
imHeight = imHeight_in;
try
{
outFile = new PrintWriter(new File(fileName));
outFile.print(\"<?xml version=\'1.0\' standalone=\'no\'?>\");
output = \"\ <svg xmlns=\\\"http://www.w3.org/2000/svg\\\" xmlns:xlink = \\\"http://www.w3.org/1999/xlink\\\" \";
output += \"version = \\\"1.1\\\" width = \\\"\"
+ imWidth
+ \"\\\" height = \\\"\"
+ imHeight
+ \"\\\">\";
outFile.print(output);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
System.out.print(\"\ Error opening file: \" + fileName);
}
}
public void drawRectangle(Rectangle rectangle, Stroke stroke, Fill fill)
{
String output;
output = \"\ <rect \";
output += stringAdd(\"x\",rectangle.getxPosition());
output += stringAdd(\"y\",rectangle.getyPosition());
output += stringAdd(\"width\",rectangle.getWidth());
output += stringAdd(\"height\",rectangle.getHeight());
output += stringAdd(\"stroke\",stroke.getRGB());
output += stringAdd(\"stroke-width\",stroke.getWidth());
output += stringAdd(\"fill\",fill.getRGB());
output += stringAdd(\"opacity\",fill.getOpacity());
output += \"/>\";
outFile.print(output);
}
public void drawCircle(Circle circle, Stroke stroke, Fill fill)
{
String output;
output = \"\ <circle \";
output += stringAdd(\"cx\",circle.getX());
output += stringAdd(\"cy\",circle.getY());
output += stringAdd(\"r\",circle.getRadius());
output += stringAdd(\"stroke\",stroke.getRGB());
output += stringAdd(\"stroke-width\",stroke.getWidth());
output += stringAdd(\"fill\",fill.getRGB());
output += stringAdd(\"opacity\",fill.getOpacity());
output += \"/>\";
outFile.print(output);
}
public void drawLine(Line line, Stroke stroke)
{
String output;
output = \"\ <line \";
output += stringAdd(\"x1\",line.getX1());
output += stringAdd(\"y1\",line.getY1());
output += stringAdd(\"x2\",line.getX2());
output += stringAdd(\"y2\",line.getY2());
output += stringAdd(\"stroke\",stroke.getRGB());
output += stringAdd(\"stroke-width\",stroke.getWidth());
output += \"/>\";
outFile.print(output);
}
public void drawRoundedRectangle(RoundedRectangle rectangle, Stroke stroke, Fill fill)
{
String output;
output = \"\ <rect \";
output += stringAdd(\"x\",rectangle.getxPosition());
output += stringAdd(\"y\",rectangle.getyPosition());
output += stringAdd(\"rx\",rectangle.getRadius());
output += stringAdd(\"ry\",rectangle.getRadius());
output += stringAdd(\"width\",rectangle.getWidth());
output += stringAdd(\"height\",rectangle.getHeight());
output += stringAdd(\"stroke\",stroke.getRGB());
output += stringAdd(\"stroke-width\",stroke.getWidth());
output += stringAdd(\"fill\",fill.getRGB());
output += stringAdd(\"opacity\",fill.getOpacity());
output += \"/>\";
outFile.print(output);
}
public void drawDashedLine(Line line, DashedStroke stroke)
{
String output;
output = \"\ <line \";
output += stringAdd(\"x1\",line.getX1());
output += stringAdd(\"y1\",line.getY1());
output += stringAdd(\"x2\",line.getX2());
output += stringAdd(\"y2\",line.getY2());
output += stringAdd(\"stroke\",stroke.getRGB());
output += stringAdd(\"stroke-width\",stroke.getWidth());
output += stringAdd(\"stroke-dasharray\",stroke.getDashArray());
output += \"/>\";
outFile.print(output);
}
public void close()
{
outFile.println(\"\ </svg>\");
outFile.flush();
outFile.close();
}
private String stringAdd(String argument, String value)
{
String output;
output = argument + \" = \\\'\" + value + \"\\\' \";
return output;
}
private String stringAdd(String argument, int value)
{
String output;
output = argument + \" = \\\'\" + value + \"\\\' \";
return output;
}
private String stringAdd(String argument, double value)
{
String output;
output = argument + \" = \\\'\" + value + \"\\\' \";
return output;
}
}
RoundedRectangle.java
public class RoundedRectangle extends Rectangle{
private int radius;
RoundedRectangle(int x, int y, int height_in, int width_in, int radius_in) {
super(x, y, height_in, width_in);
radius = radius_in;
// TODO Auto-generated constructor stub
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return (super.getArea() - (4-Math.PI)*radius*radius);
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
}
Rectangle.java
public class Rectangle {
private int height;
private int width;
private int xPosition;
private int yPosition;
Rectangle(int x_in, int y_in, int height_in, int width_in)
{
xPosition = x_in;
yPosition = y_in;
height = height_in;
width = width_in;
}
public double getArea()
{
return height*width;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public int getxPosition() {
return xPosition;
}
public int getyPosition() {
return yPosition;
}
public void setHeight(int height) {
this.height = height;
}
public void setWidth(int width) {
this.width = width;
}
public void setxPosition(int xPosition) {
this.xPosition = xPosition;
}
public void setyPosition(int yPosition) {
this.yPosition = yPosition;
}
}
Stroke.java
public class Stroke {
private int width;
private int red;
private int blue;
private int green;
Stroke(int red_in, int green_in, int blue_in, int width_in)
{
red = red_in;
green = green_in;
blue = blue_in;
width = width_in;
}
public int getWidth() {
return width;
}
public int getRed() {
return red;
}
public int getBlue() {
return blue;
}
public int getGreen() {
return green;
}
public void setWidth(int width) {
this.width = width;
}
public void setRed(int red) {
this.red = red;
}
public void setBlue(int blue) {
this.blue = blue;
}
public void setGreen(int green) {
this.green = green;
}
public String getRGB()
{
String output;
output = \"rgb(\" + red + \",\"
+ green + \",\"
+ blue + \")\";
return output;
}
}
Line.java
public class Line {
private int x1;
private int y1;
private int x2;
private int y2;
Line(int x1_in, int y1_in, int x2_in, int y2_in)
{
x1 = x1_in;
x2 = x2_in;
y1 = y1_in;
y2 = y2_in;
}
public int getX1() {
return x1;
}
public int getY1() {
return y1;
}
public int getX2() {
return x2;
}
public int getY2() {
return y2;
}
public void setX1(int x1) {
this.x1 = x1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public void setX2(int x2) {
this.x2 = x2;
}
public void setY2(int y2) {
this.y2 = y2;
}
}
Fill.java
public class Fill {
private int red;
private int green;
private int blue;
private double opacity;
Fill(int red_in, int green_in, int blue_in, double opacity_in)
{
red = red_in;
green = green_in;
blue = blue_in;
opacity = opacity_in;
}
void setRGBA(int red_in, int green_in, int blue_in, double opacity_in)
{
red = red_in;
green = green_in;
blue = blue_in;
opacity = opacity_in;
}
void randomRGB(double opacity_in)
{
red = (int)Math.floor(255*Math.random());
green = (int)Math.floor(255*Math.random());
blue = (int)Math.floor(255*Math.random());
opacity= opacity_in;
}
public int getRed() {
return red;
}
public int getGreen() {
return green;
}
public int getBlue() {
return blue;
}
public double getOpacity() {
return opacity;
}
public void setRed(int red) {
this.red = red;
}
public void setGreen(int green) {
this.green = green;
}
public void setBlue(int blue) {
this.blue = blue;
}
public void setOpacity(double opacity) {
this.opacity = opacity;
}
public String getRGB()
{
String output;
output = \"rgb(\" + red + \",\"
+ green + \",\"
+ blue + \")\";
return output;
}
}
DashedStroke.java
public class DashedStroke extends Stroke {
private int dashline;
private int dashspace;
DashedStroke(int red_in, int green_in, int blue_in, int width_in, int dashline_in, int dashspace_in) {
super(red_in, green_in, blue_in, width_in);
dashline = dashline_in;
dashspace = dashspace_in;
}
public String getDashArray()
{
String output;
output = dashline + \",\" + dashspace;
return output;
}
}

















