Advertisement
n_stefanov

Ex10_PaintHouseSVG

May 13th, 2014
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.49 KB | None | 0 0
  1. package javaSyntax;
  2.  
  3. import java.awt.AlphaComposite;
  4. import java.awt.BasicStroke;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.util.Scanner;
  11.  
  12. import org.apache.batik.dom.GenericDOMImplementation;
  13. import org.apache.batik.svggen.SVGGraphics2D;
  14. import org.w3c.dom.DOMImplementation;
  15. import org.w3c.dom.Document;
  16.  
  17. public class Ex10_PaintHouseSVG {
  18.  
  19.     // Main class that calls the drawing class paint(); and exports it as svg/html;
  20.     public static void main(String[] args) throws IOException {
  21.         Ex10_PaintHouseSVG sv2Demo = new Ex10_PaintHouseSVG();
  22.         DOMImplementation domImpl = GenericDOMImplementation
  23.                 .getDOMImplementation();
  24.         Document doc = domImpl.createDocument(null, "svg", null);
  25.         SVGGraphics2D svg = new SVGGraphics2D(doc);
  26.         sv2Demo.paint(svg);
  27.         svg.stream(new FileWriter("house.html"), false);
  28.     }
  29.  
  30.    
  31.     public void paint(Graphics g) {
  32.  
  33.         // Input;
  34.         Scanner in = new Scanner(System.in);
  35.         double x = in.nextDouble();
  36.         double y = in.nextDouble();
  37.        
  38.         boolean isInside = false;
  39.  
  40.         // Checks if its isInside or outside and returns boolean variable isInside;
  41.         double x1 = 12.5, y1 = 8.5;
  42.         double x2 = 17.5, y2 = 3.5;
  43.         double x3 = 22.5, y3 = 8.5;
  44.         double abv = Math.abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
  45.         double abp = Math.abs(x1 * (y2 - y) + x2 * (y - y1) + x * (y1 - y2));
  46.         double apc = Math.abs(x1 * (y - y3) + x * (y3 - y1) + x3 * (y1 - y));
  47.         double pbc = Math.abs(x * (y2 - y3) + x2 * (y3 - y) + x3 * (y - y2));
  48.  
  49.         boolean isInTriangle = abp + apc + pbc == abv;
  50.         if ((x >= 12.5 && x <= 17.5) && (y <= 13.5 && y >= 8.5)) {
  51.             isInside = true;
  52.         } else if ((x >= 20 && x <= 22.5) && (y <= 13.5 && y >= 8.5)) {
  53.             isInside = true;
  54.         }
  55.  
  56.         else if (isInTriangle) {
  57.             isInside = true;
  58.         } else {
  59.             isInside = false;
  60.         }
  61.  
  62.         // Draws the dotted axes;
  63.         DrawAxes(g);
  64.         // Draws the "House" figure;
  65.         DrawFigure(g);
  66.  
  67.         double xInput = (x - 10) * 22 + 58;
  68.         double yInput = (y - 3.5) * 22 + 36;
  69.         DrawPoint(xInput, yInput, g, isInside);
  70.     }
  71.  
  72.     // Method for drawing the point according to the input;
  73.     private void DrawPoint(double x, double y, Graphics g, boolean dot) {
  74.  
  75.         if (dot == false) {
  76.             Graphics2D circleOut = (Graphics2D) g;
  77.             circleOut.setComposite(AlphaComposite.getInstance(
  78.                     AlphaComposite.SRC_OVER, 0.3f));
  79.             circleOut.drawOval((int) x, (int) y, 8, 8);
  80.             circleOut.fillOval((int) x, (int) y, 8, 8);
  81.         } else {
  82.             Graphics2D circleIn = (Graphics2D) g;
  83.             circleIn.setComposite(AlphaComposite.getInstance(
  84.                     AlphaComposite.SRC_OVER, 0.9f));
  85.             circleIn.drawOval((int) x, (int) y, 8, 8);
  86.             circleIn.fillOval((int) x, (int) y, 8, 8);
  87.         }
  88.  
  89.     }
  90.  
  91.     // Method for drawing the dotted axes;
  92.     public static void DrawAxes(Graphics g) {
  93.  
  94.         Graphics2D graph = (Graphics2D) g;
  95.  
  96.         float[] dash = { 1f, 1f, 1f };
  97.  
  98.         BasicStroke dotted = new BasicStroke(1, BasicStroke.CAP_BUTT,
  99.                 BasicStroke.JOIN_ROUND, 1.0f, dash, 2f);
  100.         graph.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
  101.                 0.3f));
  102.  
  103.         graph.setStroke(dotted);
  104.         graph.setFont(new Font("Arial", Font.PLAIN, 18));
  105.  
  106.         graph.drawLine(49, 40, 353, 40);
  107.         graph.drawLine(49, 95, 353, 95);
  108.         graph.drawLine(49, 150, 353, 150);
  109.         graph.drawLine(49, 205, 353, 205);
  110.         graph.drawLine(49, 260, 353, 260);
  111.         graph.drawLine(49, 315, 353, 315);
  112.  
  113.         graph.drawLine(62, 27, 62, 330);
  114.         graph.drawLine(117, 27, 117, 330);
  115.         graph.drawLine(172, 27, 172, 330);
  116.         graph.drawLine(227, 27, 227, 330);
  117.         graph.drawLine(282, 27, 282, 330);
  118.         graph.drawLine(337, 27, 337, 330);
  119.  
  120.         // Draws the numbers around the axes;
  121.         DrawNumbers(g);
  122.     }
  123.  
  124.     // Method for drawing the numbers around the axes;
  125.     private static void DrawNumbers(Graphics g) {
  126.  
  127.         Graphics2D nums = (Graphics2D) g;
  128.         nums.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
  129.                 0.9f));
  130.  
  131.         nums.drawString("10", 52, 20);
  132.         nums.drawString("12.5", 102, 20);
  133.         nums.drawString("15", 162, 20);
  134.         nums.drawString("17.5", 209, 20);
  135.         nums.drawString("20", 272, 20);
  136.         nums.drawString("22.5", 319, 20);
  137.  
  138.         nums.drawString("3.5", 20, 46);
  139.         nums.drawString("6", 35, 101);
  140.         nums.drawString("8.5", 20, 156);
  141.         nums.drawString("11", 27, 211);
  142.         nums.drawString("13.5", 11, 266);
  143.         nums.drawString("16", 26, 321);
  144.  
  145.     }
  146.  
  147.     // Method for drawing the "House";
  148.     public static void DrawFigure(Graphics g) {
  149.         // Draws the strokes;
  150.         drawStrokes(g);
  151.         // Draws the transparent fill;
  152.         drawShapes(g);
  153.     }
  154.  
  155.     // Method for drawing the strokes of the "House";
  156.     private static void drawStrokes(Graphics g) {
  157.         Graphics2D stroke = (Graphics2D) g;
  158.  
  159.         float[] dash = { 500f };
  160.  
  161.         BasicStroke dotted = new BasicStroke(1.5f, BasicStroke.CAP_BUTT,
  162.                 BasicStroke.JOIN_ROUND, 1.0f, dash, 2f);
  163.  
  164.         stroke.setStroke(dotted);
  165.  
  166.         stroke.drawRect(117, 150, 110, 110);
  167.         stroke.drawRect(282, 150, 55, 110);
  168.  
  169.         int xPoints[] = { 117, 227, 337 };
  170.         int yPoints[] = { 150, 40, 150 };
  171.         int nPoints = 3;
  172.  
  173.         stroke.drawPolygon(xPoints, yPoints, nPoints);
  174.     }
  175.    
  176.     // Method for drawing the trnsparent fill of the "House";
  177.         private static void drawShapes(Graphics g) {
  178.             Graphics2D fill = (Graphics2D) g;
  179.  
  180.             fill.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
  181.                     0.3f));
  182.             fill.fillRect(117, 150, 110, 110);
  183.  
  184.             fill.fillRect(282, 150, 55, 110);
  185.  
  186.             int xPoints[] = { 117, 227, 337 };
  187.             int yPoints[] = { 150, 40, 150 };
  188.             int nPoints = 3;
  189.  
  190.             fill.fillPolygon(xPoints, yPoints, nPoints);
  191.         }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement