Advertisement
bbbbas

PaintAHouse

May 15th, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. package problem10;
  2.  
  3. import java.awt.*;
  4. import java.io.*;
  5.  
  6. import java.util.Scanner;
  7.  
  8. import org.apache.batik.svggen.SVGGraphics2D;
  9. import org.apache.batik.dom.svg.SVGDOMImplementation;
  10. import org.w3c.dom.DOMImplementation;
  11. import org.w3c.dom.svg.SVGDocument;
  12.  
  13. /**
  14. *
  15. * @author bas
  16. */
  17.  
  18. public class PaintAHouse {
  19.  
  20. private static int[] dot = new int[2];
  21. private static boolean dotIsInside;
  22.  
  23. public void paint(Graphics2D g2d) {
  24.  
  25. // Assume this is already initialized
  26. g2d.setStroke(new BasicStroke(3));
  27.  
  28. g2d.drawString("10", 55, 20);
  29. g2d.drawString("12.5", 115, 20);
  30. g2d.drawString("15", 195, 20);
  31. g2d.drawString("17.5", 255, 20);
  32. g2d.drawString("20", 335, 20);
  33. g2d.drawString("22.5", 395, 20);
  34.  
  35. g2d.drawString("3.5", 23, 70);
  36. g2d.drawString("6", 23, 140);
  37. g2d.drawString("8.5", 23, 210);
  38. g2d.drawString("11", 23, 280);
  39. g2d.drawString("13.5", 23, 350);
  40. g2d.drawString("16", 23, 420);
  41.  
  42. int[] x = new int[] { 280, 420, 140 };
  43. int[] y = new int[] { 60, 200, 200 };
  44. int n = 3; // count of points
  45.  
  46. g2d.setColor(Color.BLACK);
  47. // Make a triangle
  48. g2d.drawPolygon(x, y, n);
  49. g2d.drawRect(140, 200, 140, 140);
  50. g2d.drawRect(350, 200, 70, 140);
  51. g2d.setColor(Color.GRAY);
  52. g2d.fillPolygon(x, y, n);
  53. g2d.fillRect(140, 200, 140, 140);
  54. g2d.fillRect(350, 200, 70, 140);
  55.  
  56. float dash1[] = { 1.0f };
  57. BasicStroke dashed = new BasicStroke(0.3f, BasicStroke.CAP_BUTT,
  58. BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f);
  59. g2d.setColor(Color.GRAY);
  60. g2d.setStroke(dashed);
  61. g2d.drawLine(53, 60, 440, 60);
  62. g2d.drawLine(53, 130, 440, 130);
  63. g2d.drawLine(53, 200, 440, 200);
  64. g2d.drawLine(53, 270, 440, 270);
  65. g2d.drawLine(53, 340, 440, 340);
  66. g2d.drawLine(53, 410, 440, 410);
  67.  
  68. g2d.drawLine(70, 30, 70, 440);
  69. g2d.drawLine(140, 30, 140, 440);
  70. g2d.drawLine(210, 30, 210, 440);
  71. g2d.drawLine(280, 30, 280, 440);
  72. g2d.drawLine(350, 30, 350, 440);
  73. g2d.drawLine(420, 30, 420, 440);
  74.  
  75. g2d.setStroke(new BasicStroke(3));
  76. setDot();
  77. while (dot[0] != -1) {
  78.  
  79. if (dotIsInside) {
  80. g2d.setColor(Color.BLACK);
  81. } else {
  82. g2d.setColor(Color.GRAY);
  83. }
  84. g2d.drawOval((int) dot[0], (int) dot[1], 5, 5);
  85. setDot();
  86. }
  87. }
  88.  
  89. public void setDot() {
  90. System.out.println("Dot:");
  91. Scanner input = new Scanner(System.in);
  92. double x1 = input.nextDouble();
  93. if (x1 == -1) {
  94. dot[0] = -1;
  95. } else {
  96. double y1 = input.nextDouble();
  97. double x = 67.5 + (70 * ((x1 - 10) / 2.5));
  98. double y = 57.5 + (70 * ((y1 - 3.5) / 2.5));
  99. dotIsInside = isInside(x1, y1);
  100. dot[0] = (int) x;
  101. dot[1] = (int) y;
  102. }
  103. }
  104.  
  105. public boolean isInside(double x, double y) {
  106. boolean isInside = false;
  107.  
  108. if (((x >= 12.5 && x <= 17.5) || (x >= 20 && x <= 22.5))
  109. && (y >= 8.5 && y <= 13.5)) {
  110. isInside = true;
  111. } else if ((x >= 12.5 && x <= 22.5) && (y >= 3.5 && y <= 8.5)
  112. && (-x - y + 21 <= 0) && (x - y - 14 <= 0)) {
  113. isInside = true;
  114. } else {
  115. isInside = false;
  116. }
  117.  
  118. return isInside;
  119. }
  120.  
  121. public static void main(String[] args) throws IOException {
  122.  
  123. // Get a DOMImplementation.
  124. DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
  125.  
  126. // Create an instance of org.w3c.dom.Document.
  127. String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
  128. SVGDocument document = (SVGDocument) domImpl.createDocument(svgNS,
  129. "svg", null);
  130.  
  131. // Create an instance of the SVG Generator.
  132. SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
  133.  
  134. // Ask the test to render into the SVG Graphics2D implementation.
  135. PaintAHouse test = new PaintAHouse();
  136. test.paint(svgGenerator);
  137.  
  138. // Finally, stream out SVG to the standard output using
  139. // UTF-8 encoding.
  140. boolean useCSS = true; // we want to use CSS style attributes
  141. OutputStream ostream = new FileOutputStream("house.html");
  142. Writer out = new OutputStreamWriter(ostream, "UTF-8");
  143. svgGenerator.stream(out, useCSS);
  144.  
  145. }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement