Java Code Examples for org.apache.batik.gvt.GraphicsNode#getBounds()

The following examples show how to use org.apache.batik.gvt.GraphicsNode#getBounds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VehicleMapLayer.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a buffered image from a SVG graphics node.
 * @param svg the SVG graphics node.
 * @param width the width of the produced image.
 * @param length the length of the produced image.
 * @return the created buffered image.
 */
private BufferedImage createBufferedImage(GraphicsNode svg, double width, double length) {

	int imageWidth = (int) (width * scale);
	if (imageWidth <= 0) {
		imageWidth = 1;
	}
	int imageLength = (int) (length * scale);
	if (imageLength <= 0) {
		imageLength = 1;
	}
	BufferedImage bufferedImage = new BufferedImage(
		imageWidth, imageLength, 
		BufferedImage.TYPE_INT_ARGB
	);

	// Determine bounds.
	Rectangle2D bounds = svg.getBounds();

	// Determine transform information.
	double scalingWidth = width / bounds.getWidth() * scale;
	double scalingLength = length / bounds.getHeight() * scale;

	// Draw the SVG image on the buffered image.
	Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	svg.setTransform(AffineTransform.getScaleInstance(scalingWidth, scalingLength));
	svg.paint(g2d);

	// Cleanup and return image
	g2d.dispose();

	return bufferedImage;
}
 
Example 2
Source File: SVGDrawable.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public SVGDrawable( final GraphicsNode rootNode ) {
  if ( rootNode == null ) {
    throw new NullPointerException();
  }
  this.rootNode = rootNode;
  final Rectangle2D bounds = rootNode.getBounds();
  if ( bounds != null ) {
    this.width = bounds.getWidth();
    this.height = bounds.getHeight();
  }
}
 
Example 3
Source File: StructureMapLayer.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Draws a structure on the map.
  * @param isSVG true if using a SVG image.
  * @param g2d the graphics2D context.
  * @param xLoc the X location from center of settlement (meters).
  * @param yLoc the y Location from center of settlement (meters).
  * @param width the structure width (meters).
  * @param length the structure length (meters).
  * @param facing the structure facing (degrees from North clockwise).
  * @param svg the SVG graphics node.
  * @param patternSVG the pattern SVG graphics node (null if no pattern).
  * @param color the color to display the rectangle if no SVG image.
  */
 private void drawStructure(
         boolean isSVG, Graphics2D g2d, double xLoc, double yLoc,
         double width, double length, double facing, GraphicsNode svg,
         GraphicsNode patternSVG, Color color) {

     // Save original graphics transforms.
     AffineTransform saveTransform = g2d.getTransform();
     // Save original stroke
     Stroke oldStroke = g2d.getStroke();
     
     // Determine bounds.
     Rectangle2D bounds = null;
     if (isSVG) bounds = svg.getBounds();
     else bounds = new Rectangle2D.Double(0, 0, width, length);

     // Determine transform information.
     double scalingWidth = width / bounds.getWidth() * scale;
     double scalingLength = length / bounds.getHeight() * scale;
     double boundsPosX = bounds.getX() * scalingWidth;
     double boundsPosY = bounds.getY() * scalingLength;
     double centerX = width * scale / 2D;
     double centerY = length * scale / 2D;
     double translationX = (-1D * xLoc * scale) - centerX - boundsPosX;
     double translationY = (-1D * yLoc * scale) - centerY - boundsPosY;
     double facingRadian = facing / 180D * Math.PI;

     AffineTransform newTransform = new AffineTransform();
     AffineTransform newTransform1 = new AffineTransform();
     
     // Apply graphic transforms for structure.		
     newTransform.translate(translationX, translationY);
     newTransform.rotate(facingRadian, centerX + boundsPosX, centerY + boundsPosY);
 
     if (isSVG) {
         // Draw buffered image of structure.
         BufferedImage image = getBufferedImage(svg, width, length, patternSVG);
         if (image != null) {
             g2d.transform(newTransform);
             
             if (mapPanel != null) {              	
             	g2d.drawImage(image, 0, 0, mapPanel);      
             }
         }
     }
     else {
         // Else draw colored rectangle for construction site.

         // Draw filled rectangle.
         newTransform.scale(scalingWidth, scalingLength);
         g2d.transform(newTransform);
         g2d.setColor(color);
         g2d.fill(bounds);
         
     	if (color.equals(SELECTED_CONSTRUCTION_SITE_COLOR)) {
             // Draw the dashed border
             g2d.setPaint(SITE_BORDER_COLOR);
             g2d.setStroke(dashed);
             g2d.draw(bounds);
             g2d.setStroke(oldStroke);
     	}
     }

     if (selected) {
	
     	newTransform1.scale(scalingWidth, scalingLength);
         g2d.transform(newTransform1);
      
// Draw the dashed border over the selected building
g2d.setPaint(SELECTED_BUILDING_BORDER_COLOR);
g2d.setStroke(THICK_DASHES);                                           
g2d.draw(bounds);
// Restore the stroke
g2d.setStroke(oldStroke);
     }
     
     // Restore original graphic transforms.
     g2d.setTransform(saveTransform);
 }
 
Example 4
Source File: StructureMapLayer.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates a buffered image from a SVG graphics node.
 * @param svg the SVG graphics node.
 * @param width the structure width (meters).
 * @param length the structure length (meters).
 * @param patternSVG the pattern SVG graphics node (null if no pattern).
 * @return the created buffered image.
 */
private BufferedImage createBufferedImage(GraphicsNode svg, double width, double length,
        GraphicsNode patternSVG) {

    int imageWidth = (int) (width * scale);
    if (imageWidth <= 0) {
        imageWidth = 1;
    }
    int imageLength = (int) (length * scale);
    if (imageLength <= 0) {
        imageLength = 1;
    }
    BufferedImage bufferedImage = new BufferedImage(
            imageWidth, imageLength,
            BufferedImage.TYPE_INT_ARGB
            );

    // Determine bounds.
    Rectangle2D bounds = svg.getBounds();

    // Determine transform information.
    double scalingWidth = width / bounds.getWidth() * scale;
    double scalingLength = length / bounds.getHeight() * scale;

    // Draw the SVG image on the buffered image.
    Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    svg.setTransform(AffineTransform.getScaleInstance(scalingWidth, scalingLength));
    svg.paint(g2d);

    // Draw repeating pattern SVG image on the buffered image.
    if (patternSVG != null) {
        double patternScaling = 0D;
        double patternWidth = 0D;
        double patternLength = 0D;

        double originalProportions = bounds.getWidth() / bounds.getHeight();
        double finalProportions = width / length;
        Rectangle2D patternBounds = patternSVG.getBounds();
        if ((finalProportions / originalProportions) >= 1D) {
            patternScaling = scalingLength;
            patternLength = length * (patternBounds.getHeight() / bounds.getHeight());
            patternWidth = patternLength * (patternBounds.getWidth() / patternBounds.getHeight());
        }
        else {
            patternScaling = scalingWidth;
            patternWidth = width * (patternBounds.getWidth() / bounds.getWidth());
            patternLength = patternWidth * (patternBounds.getHeight() / patternBounds.getWidth());
        }

        AffineTransform patternTransform = new AffineTransform();
        patternTransform.scale(patternScaling, patternScaling);
        for (double x = 0D; x < length; x += patternLength) {
            patternTransform.translate(0D, x * bounds.getHeight());
            double y = 0D;
            for (; y < width; y += patternWidth) {
                patternTransform.translate(y * bounds.getWidth(), 0D);
                patternSVG.setTransform(patternTransform);
                patternSVG.paint(g2d);
                patternTransform.translate(y * bounds.getWidth() * -1D, 0D);
            }
            patternTransform.translate(0D, x * bounds.getHeight() * -1D);
        }
    }

    // Cleanup and return image
    g2d.dispose();

    return bufferedImage;
}
 
Example 5
Source File: VehicleMapLayer.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws a vehicle on the map.
 * @param isSVG true if using a SVG image.
 * @param g2d the graphics2D context.
 * @param xLoc the X location from center of settlement (meters).
 * @param yLoc the y Location from center of settlement (meters).
 * @param width the vehicle width (meters).
 * @param length the vehicle length (meters).
 * @param facing the vehicle facing (degrees from North clockwise).
 * @param svg the SVG graphics node.
 * @param color the color to display the rectangle if no SVG image.
 */
private void drawVehicle(boolean isSVG, Graphics2D g2d, double xLoc, double yLoc,
		double width, double length, double facing, GraphicsNode svg, Color color) {

	// Save original graphics transforms.
	AffineTransform saveTransform = g2d.getTransform();

	// Determine bounds.
	Rectangle2D bounds = null;
	if (isSVG) bounds = svg.getBounds();
	else bounds = new Rectangle2D.Double(0, 0, width, length);

	// Determine transform information.
	double scalingWidth = width / bounds.getWidth() * scale;
	double scalingLength = length / bounds.getHeight() * scale;
	double boundsPosX = bounds.getX() * scalingWidth;
	double boundsPosY = bounds.getY() * scalingLength;
	double centerX = width * scale / 2D;
	double centerY = length * scale / 2D;
	double translationX = (-1D * xLoc * scale) - centerX - boundsPosX;
	double translationY = (-1D * yLoc * scale) - centerY - boundsPosY;
	double facingRadian = facing / 180D * Math.PI;

	// Apply graphic transforms for vehicle.
	AffineTransform newTransform = new AffineTransform();
	newTransform.translate(translationX, translationY);
	newTransform.rotate(facingRadian, centerX + boundsPosX, centerY + boundsPosY);        

	if (isSVG) {
		// Draw SVG image.
		// newTransform.scale(scalingWidth, scalingLength);
		// svg.setTransform(newTransform);
		// svg.paint(g2d);

		// Draw buffered image of vehicle.
		BufferedImage image = getBufferedImage(svg, width, length);
		if (image != null) {
			g2d.transform(newTransform);
			g2d.drawImage(image, 0, 0, mapPanel);
		}
	}
	else {
		// Draw filled rectangle.
		newTransform.scale(scalingWidth, scalingLength);
		g2d.transform(newTransform);
		g2d.setColor(color);
		g2d.fill(bounds);
	}

	// Restore original graphic transforms.
	g2d.setTransform(saveTransform);
}
 
Example 6
Source File: VehicleMapLayer.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws an overlay for a vehicle on the map.
 * @param g2d the graphics2D context.
 * @param xLoc the X location from center of settlement (meters).
 * @param yLoc the y Location from center of settlement (meters).
 * @param width the vehicle width (meters).
 * @param length the vehicle length (meters).
 * @param facing the vehicle facing (degrees from North clockwise).
 * @param vehicleSvg the vehicle SVG graphics node.
 * @param overlaySvg the overlay SVG graphics node.
 */
private void drawVehicleOverlay(Graphics2D g2d, double xLoc, double yLoc,
		double vehicleWidth, double vehicleLength, double facing, GraphicsNode vehicleSvg, 
		GraphicsNode overlaySvg) {

	// Save original graphics transforms.
	AffineTransform saveTransform = g2d.getTransform();

	// Determine bounds.
	Rectangle2D partBounds = overlaySvg.getBounds();
	Rectangle2D vehicleBounds = vehicleSvg.getBounds();

	// Determine part width and length.
	double partWidth = (partBounds.getWidth() / vehicleBounds.getWidth()) * vehicleWidth;
	double partLength = (partBounds.getHeight() / vehicleBounds.getHeight()) * vehicleLength;

	// Determine transform information.
	double scalingWidth = partWidth / partBounds.getWidth() * scale;
	double scalingLength = partLength / partBounds.getHeight() * scale;
	double boundsPosX = partBounds.getX() * scalingWidth;
	double boundsPosY = partBounds.getY() * scalingLength;
	double centerX = partWidth * scale / 2D;
	double centerY = partLength * scale / 2D;
	double translationX = (-1D * xLoc * scale) - centerX - boundsPosX;
	double translationY = (-1D * yLoc * scale) - centerY - boundsPosY;
	double facingRadian = facing / 180D * Math.PI;

	// Apply graphic transforms for vehicle part.
	AffineTransform newTransform = new AffineTransform();
	newTransform.translate(translationX, translationY);
	newTransform.rotate(facingRadian, centerX + boundsPosX, centerY + boundsPosY);        

	// Draw buffered image of vehicle.
	BufferedImage image = getBufferedImage(overlaySvg, partWidth, partLength);
	if (image != null) {
		g2d.transform(newTransform);
		g2d.drawImage(image, 0, 0, mapPanel);
	}

	// Restore original graphic transforms.
	g2d.setTransform(saveTransform);
}