下面列出了java.awt.geom.Dimension2D#getWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private double coverage(Dimension2D d, Dimension2D target) {
double wpercent = d.getWidth() / target.getWidth();
double hpercent = d.getHeight() / target.getHeight();
if (wpercent <= 1.0 && hpercent <= 1.0) {
return Math.max(wpercent, hpercent);
} else {
if (wpercent >= 1.0) {
if (hpercent >= 1.0) {
return Math.max(wpercent, hpercent);
} else {
return wpercent;
}
} else {
return hpercent; // don't think it will matter
}
}
}
public void setFitZoom(Dimension size) {
Dimension2D pageSize = getPageSize();
int pageCount = printable.getPageCount();
if (pageCount == 0)
return;
double xy = (pageSize.getWidth() + W_SPACE)
* (pageSize.getHeight() + W_SPACE) * (pageCount + 1);
double mxy = size.getWidth() * size.getHeight();
double zoom = Math.sqrt(mxy / xy);
int columnCount = (int) (size.getWidth() / ((pageSize.getWidth() + W_SPACE
/ zoom) * zoom));
if (columnCount <= 0)
columnCount = 1;
if (columnCount > pageCount)
columnCount = pageCount;
setup(columnCount, zoom);
}
/** @return A MultiResolution image created from nsImagePtr, or null. */
private Image toImage() {
if (ptr == 0) return null;
final Dimension2D size = nativeGetNSImageSize(ptr);
final int w = (int)size.getWidth();
final int h = (int)size.getHeight();
Dimension2D[] sizes
= nativeGetNSImageRepresentationSizes(ptr,
size.getWidth(), size.getHeight());
return sizes == null || sizes.length < 2 ?
new MultiResolutionCachedImage(w, h, (width, height)
-> toImage(w, h, width, height))
: new MultiResolutionCachedImage(w, h, sizes, (width, height)
-> toImage(w, h, width, height));
}
/** @return A MultiResolution image created from nsImagePtr, or null. */
private Image toImage() {
if (ptr == 0) return null;
final Dimension2D size = nativeGetNSImageSize(ptr);
final int w = (int)size.getWidth();
final int h = (int)size.getHeight();
Dimension2D[] sizes
= nativeGetNSImageRepresentationSizes(ptr,
size.getWidth(), size.getHeight());
return sizes == null || sizes.length < 2 ?
new MultiResolutionCachedImage(w, h, (width, height)
-> toImage(w, h, width, height))
: new MultiResolutionCachedImage(w, h, sizes, (width, height)
-> toImage(w, h, width, height));
}
/**
* Defines the element's minimum size.
*
* @param minimumSize
* the element's minimum size.
* @see ElementFactory#setMinimumWidth(Float)
* @see ElementFactory#setMinimumHeight(Float)
*/
public void setMinimumSize( final Dimension2D minimumSize ) {
if ( minimumSize == null ) {
this.minimumWidth = null;
this.minimumHeight = null;
} else {
this.minimumWidth = new Float( minimumSize.getWidth() );
this.minimumHeight = new Float( minimumSize.getHeight() );
}
}
/** @return A MultiResolution image created from nsImagePtr, or null. */
private Image toImage() {
if (ptr == 0) {
return null;
}
AtomicReference<Dimension2D> sizeRef = new AtomicReference<>();
execute(ptr -> {
sizeRef.set(nativeGetNSImageSize(ptr));
});
final Dimension2D size = sizeRef.get();
if (size == null) {
return null;
}
final int w = (int)size.getWidth();
final int h = (int)size.getHeight();
AtomicReference<Dimension2D[]> repRef = new AtomicReference<>();
execute(ptr -> {
repRef.set(nativeGetNSImageRepresentationSizes(ptr, size.getWidth(),
size.getHeight()));
});
Dimension2D[] sizes = repRef.get();
return sizes == null || sizes.length < 2 ?
new MultiResolutionCachedImage(w, h, (width, height)
-> toImage(w, h, width, height))
: new MultiResolutionCachedImage(w, h, sizes, (width, height)
-> toImage(w, h, width, height));
}
/**
* Writes the content of the panel to a PNG image, using Java's ImageIO.
*
* @param e the event.
*/
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
Resources.localString("JPG_FILE_FILTER_DESCRIPTION"), "jpg");
fileChooser.addChoosableFileFilter(filter);
fileChooser.setFileFilter(filter);
int option = fileChooser.showSaveDialog(this.panel);
if (option == JFileChooser.APPROVE_OPTION) {
String filename = fileChooser.getSelectedFile().getPath();
if (!filename.endsWith(".jpg")) {
filename = filename + ".jpg";
}
Dimension2D size = this.panel.getSize();
int w = (int) size.getWidth();
int h = (int) size.getHeight();
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
this.panel.getDrawable().draw(g2, new Rectangle(w, h));
try {
ImageIO.write(image, "jpeg", new File(filename));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
/** @return A MultiResolution image created from nsImagePtr, or null. */
private Image toImage() {
if (ptr == 0) {
return null;
}
AtomicReference<Dimension2D> sizeRef = new AtomicReference<>();
execute(ptr -> {
sizeRef.set(nativeGetNSImageSize(ptr));
});
final Dimension2D size = sizeRef.get();
if (size == null) {
return null;
}
final int w = (int)size.getWidth();
final int h = (int)size.getHeight();
AtomicReference<Dimension2D[]> repRef = new AtomicReference<>();
execute(ptr -> {
repRef.set(nativeGetNSImageRepresentationSizes(ptr, size.getWidth(),
size.getHeight()));
});
Dimension2D[] sizes = repRef.get();
return sizes == null || sizes.length < 2 ?
new MultiResolutionCachedImage(w, h, (width, height)
-> toImage(w, h, width, height))
: new MultiResolutionCachedImage(w, h, sizes, (width, height)
-> toImage(w, h, width, height));
}
/**
* Defines the top-right border-radius for this element. If the border radius has a non-zero width and height, the
* element's border will have a rounded top-right corner.
*
* @param borderRadius
* the defined border-radius for the top-right corner of this element or null, if this property should be
* undefined.
*/
public void setBorderTopRightRadius( final Dimension2D borderRadius ) {
if ( borderRadius == null ) {
this.borderTopRightRadiusWidth = null;
this.borderTopRightRadiusHeight = null;
} else {
this.borderTopRightRadiusWidth = new Float( borderRadius.getWidth() );
this.borderTopRightRadiusHeight = new Float( borderRadius.getHeight() );
}
}
/**
* Writes the content of the panel to a PNG image, using Java's ImageIO.
*
* @param e the event.
*/
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
Resources.localString("PNG_FILE_FILTER_DESCRIPTION"), "png");
fileChooser.addChoosableFileFilter(filter);
fileChooser.setFileFilter(filter);
int option = fileChooser.showSaveDialog(this.panel);
if (option == JFileChooser.APPROVE_OPTION) {
String filename = fileChooser.getSelectedFile().getPath();
if (!filename.endsWith(".png")) {
filename = filename + ".png";
}
Dimension2D size = this.panel.getSize();
int w = (int) size.getWidth();
int h = (int) size.getHeight();
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
this.panel.getDrawable().draw(g2, new Rectangle(w, h));
try {
ImageIO.write(image, "png", new File(filename));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
public void setOnePageZoom(Dimension size) {
Dimension2D pageSize = getPageSize();
int pageCount = printable.getPageCount();
if (pageCount == 0)
return;
double zoom = size.getWidth() / (pageSize.getWidth());
int columnCount = 1;
setup(columnCount, zoom);
}
@Override
public boolean isValid(Dimension2D value, ConstraintValidatorContext context) {
if (value != null) {
return value.getWidth() > 0 && value.getHeight() > 0;
}
return true;
}
/** @return A BufferedImage created from nsImagePtr, or null. */
public BufferedImage toImage() {
if (ptr == 0) return null;
final Dimension2D size = nativeGetNSImageSize(ptr);
final int w = (int)size.getWidth();
final int h = (int)size.getHeight();
final BufferedImage bimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
final DataBufferInt dbi = (DataBufferInt)bimg.getRaster().getDataBuffer();
final int[] buffer = SunWritableRaster.stealData(dbi, 0);
nativeCopyNSImageIntoArray(ptr, buffer, w, h);
SunWritableRaster.markDirty(dbi);
return bimg;
}
/**
* Calculates the layout of the elements for the given bounds and
* constraints.
*
* @param g2 the graphics target ({@code null} not permitted).
* @param bounds the bounds ({@code null} not permitted).
* @param constraints the constraints (not used here).
*
* @return A list of positions for the sub-elements.
*/
@Override
public List<Rectangle2D> layoutElements(Graphics2D g2, Rectangle2D bounds,
Map<String, Object> constraints) {
int elementCount = this.elements.size();
List<Rectangle2D> result = new ArrayList<>(elementCount);
int i = 0;
double x = bounds.getX() + getInsets().left;
double y = bounds.getY() + getInsets().top;
while (i < elementCount) {
// get one row of elements...
List<ElementInfo> elementsInRow = rowOfElements(i, g2,
bounds);
double height = calcRowHeight(elementsInRow);
double width = calcRowWidth(elementsInRow, this.hgap);
if (this.horizontalAlignment == HAlign.CENTER) {
x = bounds.getCenterX() - (width / 2.0);
} else if (this.horizontalAlignment == HAlign.RIGHT) {
x = bounds.getMaxX() - getInsets().right - width;
}
for (ElementInfo elementInfo : elementsInRow) {
Dimension2D dim = elementInfo.getDimension();
Rectangle2D position = new Rectangle2D.Double(x, y,
dim.getWidth(), height);
result.add(position);
x += position.getWidth() + this.hgap;
}
i = i + elementsInRow.size();
x = bounds.getX() + getInsets().left;
y += height;
}
return result;
}
/**
* A utility method that calculates a drawing area based on a bounding area
* and an anchor.
*
* @param dim the dimensions for the drawing area ({@code null} not
* permitted).
* @param anchor the anchor ({@code null} not permitted).
* @param bounds the bounds ({@code null} not permitted).
*
* @return A drawing area.
*/
private Rectangle2D calculateDrawArea(Dimension2D dim, Anchor2D anchor,
Rectangle2D bounds) {
Args.nullNotPermitted(dim, "dim");
Args.nullNotPermitted(anchor, "anchor");
Args.nullNotPermitted(bounds, "bounds");
double x, y;
double w = Math.min(dim.getWidth(), bounds.getWidth());
double h = Math.min(dim.getHeight(), bounds.getHeight());
if (anchor.getRefPt().equals(RefPt2D.CENTER)) {
x = bounds.getCenterX() - w / 2.0;
y = bounds.getCenterY() - h / 2.0;
} else if (anchor.getRefPt().equals(RefPt2D.CENTER_LEFT)) {
x = bounds.getX() + anchor.getOffset().getDX();
y = bounds.getCenterY() - h / 2.0;
} else if (anchor.getRefPt().equals(RefPt2D.CENTER_RIGHT)) {
x = bounds.getMaxX() - anchor.getOffset().getDX() - dim.getWidth();
y = bounds.getCenterY() - h / 2.0;
} else if (anchor.getRefPt().equals(RefPt2D.TOP_CENTER)) {
x = bounds.getCenterX() - w / 2.0;
y = bounds.getY() + anchor.getOffset().getDY();
} else if (anchor.getRefPt().equals(RefPt2D.TOP_LEFT)) {
x = bounds.getX() + anchor.getOffset().getDX();
y = bounds.getY() + anchor.getOffset().getDY();
} else if (anchor.getRefPt().equals(RefPt2D.TOP_RIGHT)) {
x = bounds.getMaxX() - anchor.getOffset().getDX() - dim.getWidth();
y = bounds.getY() + anchor.getOffset().getDY();
} else if (anchor.getRefPt().equals(RefPt2D.BOTTOM_CENTER)) {
x = bounds.getCenterX() - w / 2.0;
y = bounds.getMaxY() - anchor.getOffset().getDY() - dim.getHeight();
} else if (anchor.getRefPt().equals(RefPt2D.BOTTOM_RIGHT)) {
x = bounds.getMaxX() - anchor.getOffset().getDX() - dim.getWidth();
y = bounds.getMaxY() - anchor.getOffset().getDY() - dim.getHeight();
} else if (anchor.getRefPt().equals(RefPt2D.BOTTOM_LEFT)) {
x = bounds.getX() + anchor.getOffset().getDX();
y = bounds.getMaxY() - anchor.getOffset().getDY() - dim.getHeight();
} else {
x = 0.0;
y = 0.0;
}
return new Rectangle2D.Double(x, y, w, h);
}
private InternalImageProcessorResult processImageRetainShape(DataRenderable renderer) throws JRException
{
float normalWidth = availableImageWidth;
float normalHeight = availableImageHeight;
DimensionRenderable dimensionRenderer = imageRenderersCache.getDimensionRenderable((Renderable)renderer);
Dimension2D dimension = dimensionRenderer == null ? null : dimensionRenderer.getDimension(jasperReportsContext);
if (dimension != null)
{
normalWidth = (int) dimension.getWidth();
normalHeight = (int) dimension.getHeight();
}
float ratioX = 1f;
float ratioY = 1f;
int imageWidth = 0;
int imageHeight = 0;
int topOffset = 0;
int leftOffset = 0;
int bottomOffset = 0;
int rightOffset = 0;
short angle = 0;
switch (imageElement.getRotation())
{
case LEFT:
ratioX = availableImageWidth / normalHeight;
ratioY = availableImageHeight / normalWidth;
ratioX = ratioX < ratioY ? ratioX : ratioY;
ratioY = ratioX;
imageWidth = (int)(normalHeight * ratioX);
imageHeight = (int)(normalWidth * ratioY);
topOffset = (int) ((1f - ImageUtil.getXAlignFactor(imageElement)) * (availableImageHeight - imageHeight));
leftOffset = (int) (ImageUtil.getYAlignFactor(imageElement) * (availableImageWidth - imageWidth));
bottomOffset = (int) (ImageUtil.getXAlignFactor(imageElement) * (availableImageHeight - imageHeight));
rightOffset = (int) ((1f - ImageUtil.getYAlignFactor(imageElement)) * (availableImageWidth - imageWidth));
angle = -90;
break;
case RIGHT:
ratioX = availableImageWidth / normalHeight;
ratioY = availableImageHeight / normalWidth;
ratioX = ratioX < ratioY ? ratioX : ratioY;
ratioY = ratioX;
imageWidth = (int)(normalHeight * ratioX);
imageHeight = (int)(normalWidth * ratioY);
topOffset = (int) (ImageUtil.getXAlignFactor(imageElement) * (availableImageHeight - imageHeight));
leftOffset = (int) ((1f - ImageUtil.getYAlignFactor(imageElement)) * (availableImageWidth - imageWidth));
bottomOffset = (int) ((1f - ImageUtil.getXAlignFactor(imageElement)) * (availableImageHeight - imageHeight));
rightOffset = (int) (ImageUtil.getYAlignFactor(imageElement) * (availableImageWidth - imageWidth));
angle = 90;
break;
case UPSIDE_DOWN:
ratioX = availableImageWidth / normalWidth;
ratioY = availableImageHeight / normalHeight;
ratioX = ratioX < ratioY ? ratioX : ratioY;
ratioY = ratioX;
imageWidth = (int)(normalWidth * ratioX);
imageHeight = (int)(normalHeight * ratioY);
topOffset = (int) ((1f - ImageUtil.getYAlignFactor(imageElement)) * (availableImageHeight - imageHeight));
leftOffset = (int) ((1f - ImageUtil.getXAlignFactor(imageElement)) * (availableImageWidth - imageWidth));
bottomOffset = (int) (ImageUtil.getYAlignFactor(imageElement) * (availableImageHeight - imageHeight));
rightOffset = (int) (ImageUtil.getXAlignFactor(imageElement) * (availableImageWidth - imageWidth));
angle = 180;
break;
case NONE:
default:
ratioX = availableImageWidth / normalWidth;
ratioY = availableImageHeight / normalHeight;
ratioX = ratioX < ratioY ? ratioX : ratioY;
ratioY = ratioX;
imageWidth = (int)(normalWidth * ratioX);
imageHeight = (int)(normalHeight * ratioY);
topOffset = (int) (ImageUtil.getYAlignFactor(imageElement) * (availableImageHeight - imageHeight));
leftOffset = (int) (ImageUtil.getXAlignFactor(imageElement) * (availableImageWidth - imageWidth));
bottomOffset = (int) ((1f - ImageUtil.getYAlignFactor(imageElement)) * (availableImageHeight - imageHeight));
rightOffset = (int) ((1f - ImageUtil.getXAlignFactor(imageElement)) * (availableImageWidth - imageWidth));
angle = 0;
break;
}
return
new InternalImageProcessorResult(
renderer.getData(jasperReportsContext),
topOffset,
leftOffset,
bottomOffset,
rightOffset,
angle
);
}
/**
* Fits a rectangle of the specified dimension to the target rectangle,
* aligning and scaling according to the attributes of this instance.
*
* @param srcDim the dimensions of the source rectangle ({@code null}
* not permitted).
* @param target the target rectangle ({@code null} not permitted).
*
* @return The bounds of the fitted rectangle (never {@code null}).
*/
public Rectangle2D fit(Dimension2D srcDim, Rectangle2D target) {
Rectangle2D result = new Rectangle2D.Double();
if (this.scale == Scale2D.SCALE_BOTH) {
result.setFrame(target);
return result;
}
double width = srcDim.getWidth();
if (this.scale == Scale2D.SCALE_HORIZONTAL) {
width = target.getWidth();
if (!this.anchor.getRefPt().isHorizontalCenter()) {
width -= 2 * this.anchor.getOffset().getDX();
}
}
double height = srcDim.getHeight();
if (this.scale == Scale2D.SCALE_VERTICAL) {
height = target.getHeight();
if (!this.anchor.getRefPt().isVerticalCenter()) {
height -= 2 * this.anchor.getOffset().getDY();
}
}
Point2D pt = this.anchor.getAnchorPoint(target);
double x = Double.NaN;
if (this.anchor.getRefPt().isLeft()) {
x = pt.getX();
} else if (this.anchor.getRefPt().isHorizontalCenter()) {
x = target.getCenterX() - width / 2;
} else if (this.anchor.getRefPt().isRight()) {
x = pt.getX() - width;
}
double y = Double.NaN;
if (this.anchor.getRefPt().isTop()) {
y = pt.getY();
} else if (this.anchor.getRefPt().isVerticalCenter()) {
y = target.getCenterY() - height / 2;
} else if (this.anchor.getRefPt().isBottom()) {
y = pt.getY() - height;
}
result.setRect(x, y, width, height);
return result;
}
protected synchronized GraphicsNode getRootNode(JasperReportsContext jasperReportsContext) throws JRException
{
if (rootNodeRef == null || rootNodeRef.get() == null)
{
FontFamilyResolver fontFamilyResolver = BatikFontFamilyResolver.getInstance(jasperReportsContext);
UserAgent userAgentForDoc =
new BatikUserAgent(
fontFamilyResolver,
BatikUserAgent.PIXEL_TO_MM_72_DPI
);
SVGDocumentFactory documentFactory =
new SAXSVGDocumentFactory(userAgentForDoc.getXMLParserClassName(), true);
documentFactory.setValidating(userAgentForDoc.isXMLParserValidating());
SVGDocument document = getSvgDocument(jasperReportsContext, documentFactory);
Node svgNode = document.getElementsByTagName("svg").item(0);
Node svgWidthNode = svgNode.getAttributes().getNamedItem("width");
Node svgHeightNode = svgNode.getAttributes().getNamedItem("height");
String strSvgWidth = svgWidthNode == null ? null : svgWidthNode.getNodeValue().trim();
String strSvgHeight = svgHeightNode == null ? null : svgHeightNode.getNodeValue().trim();
float pixel2mm = BatikUserAgent.PIXEL_TO_MM_72_DPI;
if (
(strSvgWidth != null && strSvgWidth.endsWith("mm"))
|| (strSvgHeight != null && strSvgHeight.endsWith("mm"))
)
{
pixel2mm = BatikUserAgent.PIXEL_TO_MM_96_DPI;
}
UserAgent userAgentForCtx =
new BatikUserAgent(
fontFamilyResolver,
pixel2mm
);
BridgeContext ctx = new BridgeContext(userAgentForCtx);
ctx.setDynamic(true);
GVTBuilder builder = new GVTBuilder();
GraphicsNode rootNode = builder.build(ctx, document);
rootNodeRef = new SoftReference<GraphicsNode>(rootNode);
//copying the document size object because it has a reference to SVGSVGElementBridge,
//which prevents rootNodeRef from being cleared by the garbage collector
Dimension2D svgSize = ctx.getDocumentSize();
documentSize = new SimpleDimension2D(svgSize.getWidth(), svgSize.getHeight());
}
return rootNodeRef.get();
}
/**
* Given a symbol code meant for a single point symbol, returns the
* anchor point at which to display that image based off the image returned
* from the URL of the SinglePointServer.
*
* @param symbolID - the 15 character symbolID of a single point MilStd2525
* symbol.
* @return A pixel coordinate of the format "anchorX,anchorY,SymbolBoundsX,
* SymbolBoundsY,SymbolBoundsWidth,SymbolBoundsHeight,IconWidth,IconHeight".
* Anchor, represents the center point of the core symbol within the image.
* The image should be centered on this point.
* Symbol bounds represents the bounding rectangle of the core symbol within
* the image.
* IconWidth/Height represents the height and width of the image in its
* entirety.
* Returns an empty string if an error occurs.
*/
public String getSinglePointInfo(String symbolID)
{
String info = "";
Point2D anchor = new Point2D.Double();
Rectangle2D symbolBounds = new Rectangle2D.Double();
Dimension2D iconSize = new Dimension();
sps.getSinglePointDimensions(symbolID, anchor, symbolBounds, iconSize);
info = anchor.getX() + "," + anchor.getY() + "," +
symbolBounds.getX() + "," + symbolBounds.getY() + "," +
symbolBounds.getWidth() + "," + symbolBounds.getHeight() + "," +
iconSize.getWidth() + "," + iconSize.getHeight();
return info;
}
/**
* Defines the global border-radius for this element. If the border radius has a non-zero width and height, the
* element's border will have rounded corners. This property is a short-hand property for all other border-radius
* properties.
* <p/>
*
* @param borderRadius
* the defined border-radius for all corners of this element or null, if no global default should be defined
* here.
*/
public void setBorderRadius( final Dimension2D borderRadius ) {
if ( borderRadius == null ) {
this.borderRadiusWidth = null;
this.borderRadiusHeight = null;
} else {
this.borderRadiusWidth = new Float( borderRadius.getWidth() );
this.borderRadiusHeight = new Float( borderRadius.getHeight() );
}
}