Advertisement
radoslav89

Untitled

May 13th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. import java.awt.BasicStroke;
  2. import java.awt.Graphics;
  3. import java.awt.Polygon;
  4. import java.awt.Rectangle;
  5. import java.awt.Graphics2D;
  6. import java.awt.Color;
  7. import java.awt.RenderingHints;
  8. import java.awt.Stroke;
  9. import java.awt.geom.Ellipse2D;
  10. import java.awt.geom.Line2D;
  11. import java.awt.geom.Rectangle2D;
  12. import java.io.Writer;
  13. import java.io.OutputStreamWriter;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.Scanner;
  17.  
  18. import org.apache.batik.svggen.SVGGraphics2D;
  19. import org.apache.batik.dom.GenericDOMImplementation;
  20.  
  21. import org.w3c.dom.Document;
  22. import org.w3c.dom.DOMImplementation;
  23. import org.*;
  24.  
  25.  
  26. public class TestSVGGenerator {
  27.  
  28.  
  29. public TestSVGGenerator(ArrayList<String> coords)
  30. {
  31. this.coordsArrayList = coords;
  32. }
  33.  
  34. public ArrayList<String> coordsArrayList = new ArrayList<String>();
  35.  
  36. public static void FillList(ArrayList<String> coords)
  37. {
  38. for (int i = 0; i < coords.size(); i++) {
  39.  
  40. }
  41. }
  42.  
  43. public static String IsPointInsideTheShape(String item)
  44. {
  45.  
  46. String [] pos = item.split("\\s+");
  47. float y = Float.parseFloat(pos[0]);
  48. float x = Float.parseFloat(pos[1]);
  49.  
  50. String position = null;
  51.  
  52. if ((y*10==12.5*10 &&(x*10 >=8.5*10 && x*10<=13.5*10)) ||
  53. (y*10==17.5*10 &&(x*10 >=8.5*10 && x*10<=13.5*10)) ||
  54. (x*10==8.5*10 &&(y*10>=12.5*10 && y*10 <=17.5*10)) ||
  55. (x*10==13.5*10 &&(y*10>=12.5*10 && y*10 <=17.5*10)) ||
  56. ((x*10<13.5*10 && x*10>8.5*10)&&(y*10>12.5*10 && y*10<17.5*10))) {
  57.  
  58. position = "Inside";
  59. }
  60.  
  61. if ( (y*10==20*10 &&(x*10 >=8.5*10 && x*10<=13.5*10)) ||
  62. (y*10==22.5*10 &&(x*10 >=8.5*10 && x*10<=13.5*10)) ||
  63. (x*10==8.5*10 &&(y*10>=20*10 && y*10 <=22.5*10)) ||
  64. (x*10==13.5*10 &&(y*10>=20*10 && y*10 <=22.5*10)) ||
  65. ((x*10<13.5*10 && x*10>8.5*10)&&(y*10>20*10 && y*10<22.5*10))) {
  66.  
  67. position = "Inside";
  68. }
  69.  
  70. //Triangle
  71. 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;
  72. 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;
  73. 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;
  74. 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;
  75. if (apc+abp+bcp==area) {
  76. position = "Inside";
  77. }
  78.  
  79. if (position == null) {
  80. position = "Outside";
  81. }
  82.  
  83. return position;
  84. }
  85.  
  86. public void paint(Graphics g) {
  87.  
  88. Graphics2D g2d =(Graphics2D) g;
  89.  
  90.  
  91. // First polygon
  92. Rectangle2D rect1 = new Rectangle2D.Float(125f,85f,50,50);
  93. g2d.setPaint(Color.GRAY);
  94. g2d.fill(rect1);
  95. g2d.setColor(Color.BLUE);
  96. g2d.draw(rect1);
  97.  
  98. //Second Polygon
  99. Rectangle2D rect2 = new Rectangle2D.Float(200f,85f,25,50);
  100. g2d.setPaint(Color.GRAY);
  101. g2d.fill(rect2);
  102. g2d.draw(rect2);
  103. g2d.setColor(Color.BLUE);
  104. g2d.draw(rect2);
  105.  
  106. // Draw triangle
  107. Polygon polygon = null;
  108. int [] xCoord = new int []{125,225,175};
  109. int [] yCoord = new int []{85,85,35};
  110. polygon = new Polygon(xCoord, yCoord, xCoord.length);
  111. g2d.setStroke(new BasicStroke(1.0f));
  112. g2d.setPaint(Color.GRAY);
  113. g2d.fill(polygon);
  114. g2d.draw(polygon);
  115. g2d.setColor(Color.BLUE);
  116. g2d.draw(polygon);
  117.  
  118. //Vertical Lines
  119. Line2D line2d = null;
  120. int x1 = 100;
  121. int y1 = 30;
  122. int x2 = 100;
  123. int y2 = 165;
  124. for (int i = 0; i < 6; i++) {
  125.  
  126. Stroke drawingStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{2}, 0);
  127. line2d = new Line2D.Float(x1,y1,x2,y2);
  128. g2d.setColor(Color.BLUE);
  129. g2d.setStroke(drawingStroke);
  130. g2d.draw(line2d);
  131. x1+=25;
  132. x2+=25;
  133. }
  134.  
  135. //Horizontal Lines
  136. Line2D line1d = null;
  137. int h1 = 95;
  138. int v1 = 35;
  139. int h2 = 230;
  140. int v2 = 35;
  141. for (int i = 0; i < 6; i++) {
  142.  
  143. Stroke drawingStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{2}, 0);
  144. line1d = new Line2D.Float(h1,v1,h2,v2);
  145. g2d.setColor(Color.BLUE);
  146. g2d.setStroke(drawingStroke);
  147. g2d.draw(line1d);
  148. v1+=25;
  149. v2+=25;
  150. }
  151.  
  152. //Vertical text
  153. int t1 = 70;
  154. int z1 = 40;
  155. float value = 3.5f;
  156. for (int i = 0; i < 6; i++) {
  157. String coord = Float.toString(value);
  158. g2d.setColor(Color.BLACK);
  159. g2d.drawString(coord, t1, z1);
  160. value+=2.5f;
  161. z1+=25;
  162. }
  163.  
  164. //Horizontal text
  165. t1 = 90;
  166. z1 = 25;
  167. value = 10f;
  168. for (int i = 0; i < 6; i++) {
  169. String coord = Float.toString(value);
  170. g2d.setColor(Color.BLACK);
  171. g2d.drawString(coord, t1, z1);
  172. value+=2.5f;
  173. t1+=25;
  174. }
  175.  
  176. // Draw circles
  177. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  178.  
  179. Ellipse2D.Float circle = new Ellipse2D.Float();
  180.  
  181. for (String item : coordsArrayList) {
  182.  
  183. String position = IsPointInsideTheShape(item);
  184. String [] vals= item.split("\\s+");
  185. float x = Float.parseFloat(vals[0]);
  186. float y = Float.parseFloat(vals[1]);
  187. circle.width = 2;
  188. circle.height = 2;
  189. circle.x = x*10;
  190. circle.y = y*10;
  191. if (position == "Inside") {
  192. g2d.setColor(Color.BLACK);
  193. g2d.fill(circle);
  194. g2d.draw(circle);
  195. }
  196. if (position == "Outside") {
  197. g2d.setColor(Color.RED);
  198. g2d.fill(circle);
  199. g2d.draw(circle);
  200. }
  201. }
  202. }
  203.  
  204. public static void main(String [] args) throws IOException {
  205.  
  206.  
  207. Scanner consoleScanner = new Scanner(System.in);
  208.  
  209. ArrayList<String> coords = new ArrayList<String>();
  210.  
  211. int lines = consoleScanner.nextInt();
  212. consoleScanner.nextLine();
  213.  
  214. // Fill the list
  215. for (int i = 0; i < lines; i++) {
  216. String input = consoleScanner.nextLine();
  217. coords.add(input);
  218. }
  219.  
  220. TestSVGGenerator houseSVG = new TestSVGGenerator(coords);
  221.  
  222. // Get a DOMImplementation
  223. DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
  224. String svgNamespaceURI = "http://www.w3.org/2000/svg";
  225.  
  226. // Create an instance of org.w3c.dom.Document
  227. Document document = domImpl.createDocument(svgNamespaceURI, "svg", null);
  228.  
  229. // Create an instance of the SVG Generator
  230. SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
  231.  
  232. // Render into the SVG Graphics2D implementation
  233. TestSVGGenerator test = new TestSVGGenerator(coords);
  234. test.paint(svgGenerator);
  235.  
  236. // Finally, stream out SVG to the standard output using UTF-8
  237. // character to byte encoding
  238. boolean useCSS = true; // we want to use CSS style attribute
  239. Writer out = new OutputStreamWriter(System.out, "UTF-8");
  240. svgGenerator.stream(out, useCSS);
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement