예제 #1
0
  // @Test
  public void experimentGraphic() throws IOException, ParameterException, InvalidStyle {
    JFrame frame = new JFrame();
    frame.setTitle("StrokeTest");

    // Get the JFrame’s ContentPane.
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new BorderLayout());

    // Create an instance of DisplayJAI.
    DisplayJAI dj = new DisplayJAI();

    System.out.println(dj.getColorModel());

    fts = new Style(null, "src/test/resources/org/orbisgis/core/renderer/se/strokes.se");

    // 1)
    /*try {
    	JAXBContext jaxbContext = JAXBContext.newInstance(Style.class);
    	Marshaller marshaller = jaxbContext.createMarshaller();
    	marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
              ByteArrayOutputStream oStream = new ByteArrayOutputStream();

    	//marshaller.marshal(fts.getJAXBElement(), oStream);
    } catch (Exception ex) {
              Services.getErrorManager().error("Unable to marshall", ex);
              ex.printStackTrace(System.err);
              assertTrue(false);
    }*/

    PointSymbolizer ps =
        (PointSymbolizer) fts.getRules().get(0).getCompositeSymbolizer().getSymbolizerList().get(0);
    GraphicCollection collec = ps.getGraphicCollection();

    MapTransform mt = new MapTransform();
    double width = Uom.toPixel(270, Uom.MM, mt.getDpi(), null, null);
    double height = Uom.toPixel(160, Uom.MM, mt.getDpi(), null, null);

    // Rectangle2D.Double dim = new Rectangle2D.Double(-width/2, -height/2, width, height);
    BufferedImage img = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D rg = img.createGraphics();
    rg.setRenderingHints(mt.getRenderingHints());

    collec.draw(rg, null, false, mt, AffineTransform.getTranslateInstance(width / 2, height / 2));

    rg.setStroke(new BasicStroke(1));
    rg.setPaint(Color.BLACK);
    rg.drawLine(0, (int) height / 2, (int) width, (int) height / 2);
    rg.drawLine((int) width / 2, 0, (int) width / 2, (int) height);

    dj.setBounds(0, 0, (int) width, (int) height);
    // dj.setBounds((int)rg.getMinX(), (int)rg.getMinY(), (int)rg.getWidth(), (int)rg.getHeight());

    // RenderedImage r = rg.createRendering(mt.getCurrentRenderContext());

    dj.set(img, 0, 0);

    File file = new File("/tmp/stroke.png");
    ImageIO.write(img, "png", file);

    // Add to the JFrame’s ContentPane an instance of JScrollPane
    // containing the DisplayJAI instance.
    // contentPane.add(new JScrollPane(dj), BorderLayout.CENTER);
    contentPane.add(dj, BorderLayout.CENTER);

    // Set the closing operation so the application is finished.
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize((int) width, (int) height + 24); // adjust the frame size.
    frame.setVisible(true); // show the frame.

    try {
      Thread.sleep(5000);
    } catch (InterruptedException ex) {
      Logger.getLogger(StrokeTest.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
예제 #2
0
  @Override
  public void draw(
      Graphics2D g2,
      Map<String, Object> map,
      Shape shape,
      boolean selected,
      MapTransform mt,
      double offset)
      throws ParameterException, IOException {

    List<Shape> shapes;

    if (!this.isOffsetRapport() && Math.abs(offset) > 0.0) {
      shapes = ShapeHelper.perpendicularOffset(shape, offset);
      // Setting offset to 0.0 let be sure the offset will never been applied twice!
      offset = 0.0;
    } else {
      shapes = new ArrayList<Shape>();
      // TODO : Extract holes as separate shape !
      shapes.add(shape);
    }

    double gWidth = getGraphicWidth(map, mt);
    for (Shape shp : shapes) {
      double segLength = getNaturalLength(map, shp, mt);
      double lineLength = ShapeHelper.getLineLength(shp);

      if (segLength > lineLength) {
        segLength = lineLength;
      }

      RelativeOrientation rOrient = this.getRelativeOrientation();
      List<Shape> segments = null;

      double nbSegments;

      // int nbToDraw;

      if (this.isLengthRapport()) {
        nbSegments = (int) ((lineLength / segLength) + 0.5);
        segments = ShapeHelper.splitLine(shp, (int) nbSegments);
        // segLength = lineLength / nbSegments;
        // nbToDraw = (int) nbSegments;
      } else {
        nbSegments = lineLength / segLength;
        if (nbSegments == 0 && getParent() instanceof StrokeElement) {
          nbSegments = 1;
        }
        if (nbSegments > 0) {
          // TODO remove half of extra space at the beginning of the line
          // shp = ShapeHelper.splitLine(shp, (nbSegments - nbToDraw)/2.0).get(1);
          segments = ShapeHelper.splitLineInSeg(shp, segLength);
        }
      }

      if (segments != null) {
        for (Shape seg : segments) {
          List<Shape> oSegs;
          if (this.isOffsetRapport() && Math.abs(offset) > 0.0) {
            oSegs = ShapeHelper.perpendicularOffset(seg, offset);
          } else {
            oSegs = new ArrayList<Shape>();
            oSegs.add(seg);
          }

          for (Shape oSeg : oSegs) {
            if (oSeg != null) {
              double realSegLength = ShapeHelper.getLineLength(oSeg);
              // Is there enough space on the real segment ?  otherwise is the graphic part of a
              // compound stroke ?
              if (realSegLength > 0.9 * segLength
                  || (getParent() instanceof StrokeElement && segLength == 0.0)) {
                Point2D.Double pt;
                double relativePos = 0.5;

                if (relativePosition != null) {
                  relativePos = relativePosition.getValue(map);
                }

                if (segLength < MIN_LENGTH) {
                  pt = ShapeHelper.getPointAt(oSeg, 0);
                } else {
                  // TODO Replace with relative position !
                  pt = ShapeHelper.getPointAt(oSeg, realSegLength * relativePos);
                }
                AffineTransform at = AffineTransform.getTranslateInstance(pt.x, pt.y);

                if (rOrient != RelativeOrientation.PORTRAYAL) {
                  Point2D.Double ptA;
                  Point2D.Double ptB;

                  if (segLength < MIN_LENGTH) {
                    ptA = pt;
                    ptB = ShapeHelper.getPointAt(oSeg, gWidth);
                  } else {
                    ptA =
                        ShapeHelper.getPointAt(oSeg, relativePos * realSegLength - (gWidth * 0.5));
                    ptB =
                        ShapeHelper.getPointAt(oSeg, relativePos * realSegLength + (gWidth * 0.5));
                  }

                  double theta = Math.atan2(ptB.y - ptA.y, ptB.x - ptA.x);
                  switch (rOrient) {
                    case LINE:
                      theta += 0.5 * Math.PI;
                      break;
                    case NORMAL_UP:
                      if (theta < -Math.PI / 2 || theta > Math.PI / 2) {
                        theta += Math.PI;
                      }
                      break;
                  }
                  at.concatenate(AffineTransform.getRotateInstance(theta));
                }

                graphic.draw(g2, map, selected, mt, at);
              }
            }
          }
        }
      }
    }
  }