Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BasicStroke;
- import java.awt.Graphics;
- import java.awt.Polygon;
- import java.awt.Rectangle;
- import java.awt.Graphics2D;
- import java.awt.Color;
- import java.awt.RenderingHints;
- import java.awt.Stroke;
- import java.awt.geom.Ellipse2D;
- import java.awt.geom.Line2D;
- import java.awt.geom.Rectangle2D;
- import java.io.Writer;
- import java.io.OutputStreamWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Scanner;
- import org.apache.batik.svggen.SVGGraphics2D;
- import org.apache.batik.dom.GenericDOMImplementation;
- import org.w3c.dom.Document;
- import org.w3c.dom.DOMImplementation;
- import org.*;
- public class TestSVGGenerator {
- public TestSVGGenerator(ArrayList<String> coords)
- {
- this.coordsArrayList = coords;
- }
- public ArrayList<String> coordsArrayList = new ArrayList<String>();
- public static void FillList(ArrayList<String> coords)
- {
- for (int i = 0; i < coords.size(); i++) {
- }
- }
- public static String IsPointInsideTheShape(String item)
- {
- String [] pos = item.split("\\s+");
- float y = Float.parseFloat(pos[0]);
- float x = Float.parseFloat(pos[1]);
- String position = null;
- if ((y*10==12.5*10 &&(x*10 >=8.5*10 && x*10<=13.5*10)) ||
- (y*10==17.5*10 &&(x*10 >=8.5*10 && x*10<=13.5*10)) ||
- (x*10==8.5*10 &&(y*10>=12.5*10 && y*10 <=17.5*10)) ||
- (x*10==13.5*10 &&(y*10>=12.5*10 && y*10 <=17.5*10)) ||
- ((x*10<13.5*10 && x*10>8.5*10)&&(y*10>12.5*10 && y*10<17.5*10))) {
- position = "Inside";
- }
- if ( (y*10==20*10 &&(x*10 >=8.5*10 && x*10<=13.5*10)) ||
- (y*10==22.5*10 &&(x*10 >=8.5*10 && x*10<=13.5*10)) ||
- (x*10==8.5*10 &&(y*10>=20*10 && y*10 <=22.5*10)) ||
- (x*10==13.5*10 &&(y*10>=20*10 && y*10 <=22.5*10)) ||
- ((x*10<13.5*10 && x*10>8.5*10)&&(y*10>20*10 && y*10<22.5*10))) {
- position = "Inside";
- }
- //Triangle
- double area = Math.abs(12.5*10*(8.5*10-3.5*10) + 22.5*10*(3.5*10-8.5*10) + 17.5*(8.5*10-8.5*10))*0.5;
- double apc = Math.abs(12.5*10*(x*10-3.5*10) + y*10*(3.5*10-8.5*10) + 17.5*10*(8.5*10-x*10))*0.5;
- double abp = Math.abs(12.5*10*(8.5*10-x*10) + 22.5*10*(x*10-8.5*10) + y*10*(8.5*10-8.5*10))*0.5;
- double bcp = Math.abs(22.5*10*(3.5*10-x*10) + 17.5*10*(x*10-8.5*10) + y*10*(8.5*10-3.5*10))*0.5;
- if (apc+abp+bcp==area) {
- position = "Inside";
- }
- if (position == null) {
- position = "Outside";
- }
- return position;
- }
- public void paint(Graphics g) {
- Graphics2D g2d =(Graphics2D) g;
- // First polygon
- Rectangle2D rect1 = new Rectangle2D.Float(125f,85f,50,50);
- g2d.setPaint(Color.GRAY);
- g2d.fill(rect1);
- g2d.setColor(Color.BLUE);
- g2d.draw(rect1);
- //Second Polygon
- Rectangle2D rect2 = new Rectangle2D.Float(200f,85f,25,50);
- g2d.setPaint(Color.GRAY);
- g2d.fill(rect2);
- g2d.draw(rect2);
- g2d.setColor(Color.BLUE);
- g2d.draw(rect2);
- // Draw triangle
- Polygon polygon = null;
- int [] xCoord = new int []{125,225,175};
- int [] yCoord = new int []{85,85,35};
- polygon = new Polygon(xCoord, yCoord, xCoord.length);
- g2d.setStroke(new BasicStroke(1.0f));
- g2d.setPaint(Color.GRAY);
- g2d.fill(polygon);
- g2d.draw(polygon);
- g2d.setColor(Color.BLUE);
- g2d.draw(polygon);
- //Vertical Lines
- Line2D line2d = null;
- int x1 = 100;
- int y1 = 30;
- int x2 = 100;
- int y2 = 165;
- for (int i = 0; i < 6; i++) {
- Stroke drawingStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{2}, 0);
- line2d = new Line2D.Float(x1,y1,x2,y2);
- g2d.setColor(Color.BLUE);
- g2d.setStroke(drawingStroke);
- g2d.draw(line2d);
- x1+=25;
- x2+=25;
- }
- //Horizontal Lines
- Line2D line1d = null;
- int h1 = 95;
- int v1 = 35;
- int h2 = 230;
- int v2 = 35;
- for (int i = 0; i < 6; i++) {
- Stroke drawingStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{2}, 0);
- line1d = new Line2D.Float(h1,v1,h2,v2);
- g2d.setColor(Color.BLUE);
- g2d.setStroke(drawingStroke);
- g2d.draw(line1d);
- v1+=25;
- v2+=25;
- }
- //Vertical text
- int t1 = 70;
- int z1 = 40;
- float value = 3.5f;
- for (int i = 0; i < 6; i++) {
- String coord = Float.toString(value);
- g2d.setColor(Color.BLACK);
- g2d.drawString(coord, t1, z1);
- value+=2.5f;
- z1+=25;
- }
- //Horizontal text
- t1 = 90;
- z1 = 25;
- value = 10f;
- for (int i = 0; i < 6; i++) {
- String coord = Float.toString(value);
- g2d.setColor(Color.BLACK);
- g2d.drawString(coord, t1, z1);
- value+=2.5f;
- t1+=25;
- }
- // Draw circles
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- Ellipse2D.Float circle = new Ellipse2D.Float();
- for (String item : coordsArrayList) {
- String position = IsPointInsideTheShape(item);
- String [] vals= item.split("\\s+");
- float x = Float.parseFloat(vals[0]);
- float y = Float.parseFloat(vals[1]);
- circle.width = 2;
- circle.height = 2;
- circle.x = x*10;
- circle.y = y*10;
- if (position == "Inside") {
- g2d.setColor(Color.BLACK);
- g2d.fill(circle);
- g2d.draw(circle);
- }
- if (position == "Outside") {
- g2d.setColor(Color.RED);
- g2d.fill(circle);
- g2d.draw(circle);
- }
- }
- }
- public static void main(String [] args) throws IOException {
- Scanner consoleScanner = new Scanner(System.in);
- ArrayList<String> coords = new ArrayList<String>();
- int lines = consoleScanner.nextInt();
- consoleScanner.nextLine();
- // Fill the list
- for (int i = 0; i < lines; i++) {
- String input = consoleScanner.nextLine();
- coords.add(input);
- }
- TestSVGGenerator houseSVG = new TestSVGGenerator(coords);
- // Get a DOMImplementation
- DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
- String svgNamespaceURI = "http://www.w3.org/2000/svg";
- // Create an instance of org.w3c.dom.Document
- Document document = domImpl.createDocument(svgNamespaceURI, "svg", null);
- // Create an instance of the SVG Generator
- SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
- // Render into the SVG Graphics2D implementation
- TestSVGGenerator test = new TestSVGGenerator(coords);
- test.paint(svgGenerator);
- // Finally, stream out SVG to the standard output using UTF-8
- // character to byte encoding
- boolean useCSS = true; // we want to use CSS style attribute
- Writer out = new OutputStreamWriter(System.out, "UTF-8");
- svgGenerator.stream(out, useCSS);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement