Advertisement
nadiahristova

Probl10_PaintAHouseAsSVG

Jan 24th, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1. import java.util.Locale;
  2. import java.util.Scanner;
  3. import java.awt.BasicStroke;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.Polygon;
  7. import java.awt.Graphics2D;
  8. import java.awt.Color;
  9. import java.awt.geom.Line2D;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12.  
  13. import org.apache.batik.svggen.SVGGraphics2D;
  14. import org.apache.batik.dom.GenericDOMImplementation;
  15. import org.w3c.dom.Document;
  16. import org.w3c.dom.DOMImplementation;
  17.  
  18. //HTML doc is generated with the help of the Apache Batik library for Java
  19. //u can download it here: http://xmlgraphics.apache.org/batik/download.html or here: http://apache.cbox.biz/xmlgraphics/batik/
  20. //For the drawing of the picture is used Java's Graphics2D class and it's options
  21.  
  22. public class Probl10_PaintAHouseAsSVG {
  23.    
  24.     private int picZeroX = 250;
  25.     private int picZeroY = 200;
  26.    
  27.     public void paint(Graphics2D g2d) {
  28.         Scanner input = new Scanner(System.in);
  29.         Locale.setDefault(Locale.ROOT);
  30.         System.out.print("Please type in the number and the coordinates of the dots u want to visualize in the picture:");
  31.         int inputP = input.nextInt();
  32.         Point[] leftRec = new Point[]{new Point(12.5, 8.5),new Point(12.5, 13.5),
  33.                 new Point(17.5, 13.5), new Point(17.5, 8.5)};
  34.         Point[] rigthRec = new Point[]{new Point(20, 8.5),new Point(20, 13.5),
  35.                 new Point(22.5, 13.5), new Point(22.5, 8.5)};
  36.         Point[] triangle = new Point[]{new Point(12.5, 8.5),new Point(22.5, 8.5),
  37.                 new Point(17.5, 3.5)};
  38.        
  39.         drawBackground(g2d);
  40.         drawFigures(g2d);      
  41.         for (int i = 0; i < inputP; i++) {
  42.             Point givenPoint= new Point(input.nextDouble(), input.nextDouble());
  43.             boolean isInside = Point.isInsideFig(leftRec,givenPoint) || Point.isInsideFig(rigthRec,givenPoint) ||
  44.                      Point.isInsideFig(triangle,givenPoint);
  45.             drawPoints(g2d, isInside, givenPoint);
  46.         }      
  47.       }
  48.    
  49.     private void drawPoints(Graphics2D g2d, boolean isInside, Point currPoint) {
  50.         Graphics2D g2 = (Graphics2D) g2d;
  51.        
  52.         int x = (int)(picZeroX + (currPoint.getX() - 10)*125/2.5);
  53.         int y = (int)(picZeroY + (currPoint.getY() - 3.5)*125/2.5);
  54.        
  55.         Color purp = new Color(0.5f, 0.5f, 0.5f,0.5f);
  56.         g2.setStroke(new BasicStroke(0.5f));
  57.         g2.setColor(Color.black);
  58.         g2.drawOval(x-7, y-7, 14, 14);     
  59.         if (!isInside) {
  60.             g2.setColor(purp);
  61.         }
  62.         g2.fillOval(x-7, y-7, 14, 14);     
  63.     }
  64.  
  65.     private void drawFigures(Graphics2D g2d) {
  66.         Graphics2D g2 = (Graphics2D) g2d;
  67.        
  68.         Color purp = new Color(0.5f, 0.5f, 0.5f,0.5f);
  69.         Color diffBlue = new Color(0,51,120);
  70.        
  71.         int[] x = new int[]{picZeroX + 125,picZeroX + 5*125, picZeroX + 3*125};
  72.         int[] y = new int[]{picZeroY + 2*125,picZeroY + 2*125, picZeroY};
  73.         Polygon p = new Polygon(x,y,3);
  74.        
  75.         g2.setStroke(new BasicStroke(3));
  76.         g2.setColor(purp);
  77.         g2.fillRect(picZeroX+125, picZeroY+2*125, 2*125, 2*125);
  78.         g2.fillRect(picZeroX+4*125, picZeroY+2*125, 125, 2*125);
  79.         g2.fillPolygon(p);
  80.        
  81.         g2.setPaint(diffBlue);
  82.         g2.drawRect(picZeroX+125, picZeroY+2*125, 2*125, 2*125);       
  83.         g2.drawRect(picZeroX+4*125, picZeroY+2*125, 125, 2*125);
  84.         g2.drawPolygon(p);
  85.     }
  86.  
  87.     private void drawBackground(Graphics2D g2d) {
  88.        
  89.         Graphics2D g2 = (Graphics2D) g2d;        
  90.        
  91.         float dash1[] = {1.5f};
  92.         BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
  93.                             1.5f, dash1, 1.5f);
  94.         g2.setStroke(dashed);
  95.         Font f = new Font("Axis", Font.PLAIN, 30);
  96.         g2.setFont(f);     
  97.        
  98.         float x = picZeroX;
  99.         float y = picZeroY-50;
  100.         float axisX = 10f;
  101.         float axisY = 3.5f;
  102.        
  103.         for (int i = 0; i <6; i++) {
  104.             g2.drawString(Float.toString(axisX).replaceAll("\\.?0*$", ""), x-28, y-25);
  105.             g2.draw(new Line2D.Float(x, y, x, y + 5*125+100));
  106.             x += 125;
  107.             axisX +=2.5f;
  108.         }
  109.        
  110.         x = picZeroX-30 ;
  111.         y = picZeroY;
  112.        
  113.         for (int i = 0; i <6; i++) {
  114.             g2.drawString(String.format("%4.1f", axisY).replaceAll("\\.?0*$", ""), x-75, y+9);
  115.             g2.draw(new Line2D.Float(x, y, x+ 5*125+60, y));
  116.             y += 125;
  117.             axisY +=2.5f;
  118.         }              
  119.     }
  120.  
  121.     public static void main(String[] args) throws IOException {
  122.  
  123.         DOMImplementation domImpl =
  124.           GenericDOMImplementation.getDOMImplementation();
  125.         String svgNS = "http://www.w3.org/2000/svg";
  126.         Document document = domImpl.createDocument(svgNS, "svg", null);    
  127.         SVGGraphics2D svgGenerator = new SVGGraphics2D(document);      
  128.        
  129.         Dimension size=new Dimension(1000,1000);
  130.         Probl10_PaintAHouseAsSVG prob = new Probl10_PaintAHouseAsSVG();
  131.         svgGenerator.setSVGCanvasSize(size);
  132.         prob.paint(svgGenerator);      
  133.         boolean useCSS = true;      
  134.         svgGenerator.stream(new FileWriter("/home/house.html"), useCSS);    
  135.     }
  136.  
  137. }
  138.  
  139. class Point{
  140.     private double x;
  141.     private double y;
  142.    
  143.     public Point(double x, double y){
  144.         this.x = x;
  145.         this.y = y;
  146.     }
  147.    
  148.     public double getX(){
  149.         return this.x;
  150.     }
  151.    
  152.     public void setX(double x){
  153.         this.x = x;
  154.     }
  155.    
  156.     public double getY(){
  157.         return this.y;
  158.     }
  159.    
  160.     public void setY(double y){
  161.         this.y = y;
  162.     }
  163.    
  164.     public static boolean isInsideFig(Point[] fig, Point pointInQ){
  165.         boolean isIn = true;
  166.        
  167.         for (int i = 0; i < fig.length; i++) {
  168.             int index = i+1;
  169.             if (i == fig.length-1) {
  170.                 index =0;
  171.             }
  172.             double findSign = (fig[index].x - fig[i].x)*(pointInQ.y - fig[i].y) -  
  173.                     (fig[index].y - fig[i].y)*(pointInQ.x - fig[i].x);
  174.             if (findSign > 0) {
  175.                 isIn = false;
  176.             }
  177.         }
  178.         return isIn;
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement