下面列出了怎么用java.awt.geom.NoninvertibleTransformException的API类实例代码及写法,或者点击链接到github查看源代码。
public void createShapes(final float x, final float y, final int size) {
int outerSize = 15;
AffineTransform rotationTransform = AffineTransform.getRotateInstance(rotation, centerX, centerY);
circleShape = calculateCircleShape(x, y, size, outerSize);
triangleShape = calculateTriangleShape(x, y, size, outerSize, rotationTransform);
outerIndicator = createOuterIndicator(centerX, centerY, (innerRadius + outerRadius) / 2.0,
rotationTransform, outerIndicatorRadius);
innerIndicator = createInnerIndicator(rotationTransform, innerIndicatorRadius);
invalid = false;
try {
// For calculating pick location. MouseEvents already respect the ui scaling.
triangleInverse = rotationTransform.createInverse();
} catch (NoninvertibleTransformException e) {
e.printStackTrace();
}
}
@Override
public Raster getRaster(final int x, final int y, final int w, final int h) {
WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
dummy.setLocation((x + i), (y + j));
try {
transform.inverseTransform(dummy, dummy);
triangleInverse.transform(dummy, dummy);
Point2D sv = getSaturationAndValue(dummy.getX(), dummy.getY());
setPixel(raster, i, j, getColorRGB(getHue(), sv.getX(), sv.getY()));
} catch (NoninvertibleTransformException ignored) {}
}
}
return raster;
}
private static AffineTransform extractRotation(Point2D.Double pt,
AffineTransform tx, boolean andTranslation) {
tx.deltaTransform(pt, pt);
AffineTransform rtx = AffineTransform.getRotateInstance(pt.x, pt.y);
try {
AffineTransform rtxi = rtx.createInverse();
double dx = tx.getTranslateX();
double dy = tx.getTranslateY();
tx.preConcatenate(rtxi);
if (andTranslation) {
if (dx != 0 || dy != 0) {
tx.setTransform(tx.getScaleX(), tx.getShearY(),
tx.getShearX(), tx.getScaleY(), 0, 0);
rtx.setTransform(rtx.getScaleX(), rtx.getShearY(),
rtx.getShearX(), rtx.getScaleY(), dx, dy);
}
}
}
catch (NoninvertibleTransformException e) {
return null;
}
return rtx;
}
/**
* Change the dtx for the strike refs we use. Keeps a reference to the at. At
* must not contain translation.
* Called by setRenderTransform, setDTX, initFontData.
*/
private final void resetDTX(AffineTransform at) {
fsref = null;
dtx = at;
invdtx = null;
if (!dtx.isIdentity()) {
try {
invdtx = dtx.createInverse();
}
catch (NoninvertibleTransformException e) {
// we needn't care for rendering
}
}
if (gti != null) {
gti.strikesRef = null;
}
}
private static AffineTransform extractRotation(Point2D.Double pt,
AffineTransform tx, boolean andTranslation) {
tx.deltaTransform(pt, pt);
AffineTransform rtx = AffineTransform.getRotateInstance(pt.x, pt.y);
try {
AffineTransform rtxi = rtx.createInverse();
double dx = tx.getTranslateX();
double dy = tx.getTranslateY();
tx.preConcatenate(rtxi);
if (andTranslation) {
if (dx != 0 || dy != 0) {
tx.setTransform(tx.getScaleX(), tx.getShearY(),
tx.getShearX(), tx.getScaleY(), 0, 0);
rtx.setTransform(rtx.getScaleX(), rtx.getShearY(),
rtx.getShearX(), rtx.getScaleY(), dx, dy);
}
}
}
catch (NoninvertibleTransformException e) {
return null;
}
return rtx;
}
/**
* !!! not used currently, but might be by getPixelbounds?
*/
public void pixellate(FontRenderContext renderFRC, Point2D loc, Point pxResult) {
if (renderFRC == null) {
renderFRC = frc;
}
// it is a total pain that you have to copy the transform.
AffineTransform at = renderFRC.getTransform();
at.transform(loc, loc);
pxResult.x = (int)loc.getX(); // but must not behave oddly around zero
pxResult.y = (int)loc.getY();
loc.setLocation(pxResult.x, pxResult.y);
try {
at.inverseTransform(loc, loc);
}
catch (NoninvertibleTransformException e) {
throw new IllegalArgumentException("must be able to invert frc transform");
}
}
/**
* Change the dtx for the strike refs we use. Keeps a reference to the at. At
* must not contain translation.
* Called by setRenderTransform, setDTX, initFontData.
*/
private final void resetDTX(AffineTransform at) {
fsref = null;
dtx = at;
invdtx = null;
if (!dtx.isIdentity()) {
try {
invdtx = dtx.createInverse();
}
catch (NoninvertibleTransformException e) {
// we needn't care for rendering
}
}
if (gti != null) {
gti.strikesRef = null;
}
}
@Override
public Shape getClip() {
if (clipping == null)
return null;
try {
Area area = new Area(clipping);
area.transform(transform.createInverse());
if (area.isRectangular())
return area.getBounds2D();
return area;
} catch (NoninvertibleTransformException t) {
RuntimeException e2 = new RuntimeException();
e2.initCause(t);
throw e2;
}
}
/**
* Change the dtx for the strike refs we use. Keeps a reference to the at. At
* must not contain translation.
* Called by setRenderTransform, setDTX, initFontData.
*/
private final void resetDTX(AffineTransform at) {
fsref = null;
dtx = at;
invdtx = null;
if (!dtx.isIdentity()) {
try {
invdtx = dtx.createInverse();
}
catch (NoninvertibleTransformException e) {
// we needn't care for rendering
}
}
if (gti != null) {
gti.strikesRef = null;
}
}
/**
* !!! not used currently, but might be by getPixelbounds?
*/
public void pixellate(FontRenderContext renderFRC, Point2D loc, Point pxResult) {
if (renderFRC == null) {
renderFRC = frc;
}
// it is a total pain that you have to copy the transform.
AffineTransform at = renderFRC.getTransform();
at.transform(loc, loc);
pxResult.x = (int)loc.getX(); // but must not behave oddly around zero
pxResult.y = (int)loc.getY();
loc.setLocation(pxResult.x, pxResult.y);
try {
at.inverseTransform(loc, loc);
}
catch (NoninvertibleTransformException e) {
throw new IllegalArgumentException("must be able to invert frc transform");
}
}
/**
* Change the dtx for the strike refs we use. Keeps a reference to the at. At
* must not contain translation.
* Called by setRenderTransform, setDTX, initFontData.
*/
private final void resetDTX(AffineTransform at) {
fsref = null;
dtx = at;
invdtx = null;
if (!dtx.isIdentity()) {
try {
invdtx = dtx.createInverse();
}
catch (NoninvertibleTransformException e) {
// we needn't care for rendering
}
}
if (gti != null) {
gti.strikesRef = null;
}
}
/**
* !!! not used currently, but might be by getPixelbounds?
*/
public void pixellate(FontRenderContext renderFRC, Point2D loc, Point pxResult) {
if (renderFRC == null) {
renderFRC = frc;
}
// it is a total pain that you have to copy the transform.
AffineTransform at = renderFRC.getTransform();
at.transform(loc, loc);
pxResult.x = (int)loc.getX(); // but must not behave oddly around zero
pxResult.y = (int)loc.getY();
loc.setLocation(pxResult.x, pxResult.y);
try {
at.inverseTransform(loc, loc);
}
catch (NoninvertibleTransformException e) {
throw new IllegalArgumentException("must be able to invert frc transform");
}
}
/**
* !!! not used currently, but might be by getPixelbounds?
*/
public void pixellate(FontRenderContext renderFRC, Point2D loc, Point pxResult) {
if (renderFRC == null) {
renderFRC = frc;
}
// it is a total pain that you have to copy the transform.
AffineTransform at = renderFRC.getTransform();
at.transform(loc, loc);
pxResult.x = (int)loc.getX(); // but must not behave oddly around zero
pxResult.y = (int)loc.getY();
loc.setLocation(pxResult.x, pxResult.y);
try {
at.inverseTransform(loc, loc);
}
catch (NoninvertibleTransformException e) {
throw new IllegalArgumentException("must be able to invert frc transform");
}
}
/**
* Change the dtx for the strike refs we use. Keeps a reference to the at. At
* must not contain translation.
* Called by setRenderTransform, setDTX, initFontData.
*/
private final void resetDTX(AffineTransform at) {
fsref = null;
dtx = at;
invdtx = null;
if (!dtx.isIdentity()) {
try {
invdtx = dtx.createInverse();
}
catch (NoninvertibleTransformException e) {
// we needn't care for rendering
}
}
if (gti != null) {
gti.strikesRef = null;
}
}
private static AffineTransform extractRotation(Point2D.Double pt,
AffineTransform tx, boolean andTranslation) {
tx.deltaTransform(pt, pt);
AffineTransform rtx = AffineTransform.getRotateInstance(pt.x, pt.y);
try {
AffineTransform rtxi = rtx.createInverse();
double dx = tx.getTranslateX();
double dy = tx.getTranslateY();
tx.preConcatenate(rtxi);
if (andTranslation) {
if (dx != 0 || dy != 0) {
tx.setTransform(tx.getScaleX(), tx.getShearY(),
tx.getShearX(), tx.getScaleY(), 0, 0);
rtx.setTransform(rtx.getScaleX(), rtx.getShearY(),
rtx.getShearX(), rtx.getScaleY(), dx, dy);
}
}
}
catch (NoninvertibleTransformException e) {
return null;
}
return rtx;
}
/**
* Change the dtx for the strike refs we use. Keeps a reference to the at. At
* must not contain translation.
* Called by setRenderTransform, setDTX, initFontData.
*/
private final void resetDTX(AffineTransform at) {
fsref = null;
dtx = at;
invdtx = null;
if (!dtx.isIdentity()) {
try {
invdtx = dtx.createInverse();
}
catch (NoninvertibleTransformException e) {
// we needn't care for rendering
}
}
if (gti != null) {
gti.strikesRef = null;
}
}
/**
* Change the dtx for the strike refs we use. Keeps a reference to the at. At
* must not contain translation.
* Called by setRenderTransform, setDTX, initFontData.
*/
private final void resetDTX(AffineTransform at) {
fsref = null;
dtx = at;
invdtx = null;
if (!dtx.isIdentity()) {
try {
invdtx = dtx.createInverse();
}
catch (NoninvertibleTransformException e) {
// we needn't care for rendering
}
}
if (gti != null) {
gti.strikesRef = null;
}
}
/**
* !!! not used currently, but might be by getPixelbounds?
*/
public void pixellate(FontRenderContext renderFRC, Point2D loc, Point pxResult) {
if (renderFRC == null) {
renderFRC = frc;
}
// it is a total pain that you have to copy the transform.
AffineTransform at = renderFRC.getTransform();
at.transform(loc, loc);
pxResult.x = (int)loc.getX(); // but must not behave oddly around zero
pxResult.y = (int)loc.getY();
loc.setLocation(pxResult.x, pxResult.y);
try {
at.inverseTransform(loc, loc);
}
catch (NoninvertibleTransformException e) {
throw new IllegalArgumentException("must be able to invert frc transform");
}
}
private Selection getSelection(MouseEvent evt)
throws NoninvertibleTransformException {
ShapeCreationPanel scp = (ShapeCreationPanel) evt.getComponent();
AffineTransform tx = scp.getTransform();
double r = scp.getHandleSize() / 2;
Point2D mouseLoc = new Point2D.Float(evt.getX(), evt.getY());
CurvedPolyline[] shapes = getMirror(scp);
for (int shapeIndex = 0; shapeIndex < shapes.length; shapeIndex++) {
if (scp.getHandlesActive().supports(scp, shapeIndex)) {
for (int b = shapes[shapeIndex].getPointCount() - 1; b >= 0; b--) {
Point2D p = tx.transform(
shapes[shapeIndex].getPoint(b), null);
Ellipse2D e = new Ellipse2D.Double(p.getX() - r,
p.getY() - r, 2 * r, 2 * r);
if (e.contains(mouseLoc))
return new Selection(shapeIndex, b, null);
}
}
}
return getSelectedShape(scp, evt.getPoint());
}
/**
* !!! not used currently, but might be by getPixelbounds?
*/
public void pixellate(FontRenderContext renderFRC, Point2D loc, Point pxResult) {
if (renderFRC == null) {
renderFRC = frc;
}
// it is a total pain that you have to copy the transform.
AffineTransform at = renderFRC.getTransform();
at.transform(loc, loc);
pxResult.x = (int)loc.getX(); // but must not behave oddly around zero
pxResult.y = (int)loc.getY();
loc.setLocation(pxResult.x, pxResult.y);
try {
at.inverseTransform(loc, loc);
}
catch (NoninvertibleTransformException e) {
throw new IllegalArgumentException("must be able to invert frc transform");
}
}
/**
* Returns the user clipping region. The initial default value is
* {@code null}.
*
* @return The user clipping region (possibly {@code null}).
*
* @see #setClip(java.awt.Shape)
*/
@Override
public Shape getClip() {
if (this.clip == null) {
return null;
}
AffineTransform inv;
try {
inv = this.transform.createInverse();
return inv.createTransformedShape(this.clip);
} catch (NoninvertibleTransformException ex) {
return null;
}
}
private Point transformPoint(final JXLayer<? extends V> layer, final Point point) {
AffineTransform transform = this.getTransform(layer);
if (transform != null) {
try {
transform.inverseTransform(point, point);
} catch (NoninvertibleTransformException e) {
e.printStackTrace();
}
}
return point;
}
@Override
protected void saveTransformForUndo() {
try {
AffineTransform inverse = bufferedTransform.createInverse();
bufferedFigure.willChange();
bufferedFigure.transform(inverse);
bufferedFigure.changed();
}
catch (NoninvertibleTransformException e) {
LOG.warn("Exception inverting transform.", e);
}
}
private void updatePoints(int x, int y) {
Point2D inv = new Point2D.Double(x, y);
try {
inv = transform.inverseTransform(inv, null);
} catch (NoninvertibleTransformException e) {
e.printStackTrace();
}
x = (int)inv.getX();
y = (int)inv.getY();
switch (paintType) {
default:
case BASIC:
case LINEAR:
// pick the closest point to move
if (inv.distance(startX, startY) < inv.distance(endX, endY)) {
startX = x;
startY = y;
} else {
endX = x;
endY = y;
}
break;
case RADIAL:
// pick the closest point to move
if (inv.distance(ctrX, ctrY) < inv.distance(focusX, focusY)) {
ctrX = x;
ctrY = y;
} else {
focusX = x;
focusY = y;
}
break;
}
updatePaint();
}
TexturePaintContext(ColorModel cm, AffineTransform xform,
int bWidth, int bHeight, int maxw) {
this.colorModel = getInternedColorModel(cm);
this.bWidth = bWidth;
this.bHeight = bHeight;
this.maxWidth = maxw;
try {
xform = xform.createInverse();
} catch (NoninvertibleTransformException e) {
xform.setToScale(0, 0);
}
this.incXAcross = mod(xform.getScaleX(), bWidth);
this.incYAcross = mod(xform.getShearY(), bHeight);
this.incXDown = mod(xform.getShearX(), bWidth);
this.incYDown = mod(xform.getScaleY(), bHeight);
this.xOrg = xform.getTranslateX();
this.yOrg = xform.getTranslateY();
this.colincx = (int) incXAcross;
this.colincy = (int) incYAcross;
this.colincxerr = fractAsInt(incXAcross);
this.colincyerr = fractAsInt(incYAcross);
this.rowincx = (int) incXDown;
this.rowincy = (int) incYDown;
this.rowincxerr = fractAsInt(incXDown);
this.rowincyerr = fractAsInt(incYDown);
}
public Shape untransformShape(Shape s) {
if (s == null) {
return null;
}
if (transformState > TRANSFORM_INT_TRANSLATE) {
try {
return transformShape(transform.createInverse(), s);
} catch (NoninvertibleTransformException e) {
return null;
}
} else {
return transformShape(-transX, -transY, s);
}
}
private Point2D getMousePositionInMap() {
Point2D mousePositionInMap = null;
AffineTransform geomToScreen = panel.getMapTransform();
try {
AffineTransform geomToScreenInv = geomToScreen.createInverse();
mousePositionInMap = geomToScreenInv.transform(new Point2D.Double(panel.getMousePosition().x,panel.getMousePosition().y), null);
//System.out.println(mousePosition);
} catch (NoninvertibleTransformException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return mousePositionInMap;
}
public boolean pointToPath(Point2D pt, Point2D result) {
result.setLocation(pt);
if (tx != null) {
try {
tx.inverseTransform(pt, result);
}
catch (NoninvertibleTransformException ex) {
}
}
return result.getX() > 0;
}
TexturePaintContext(ColorModel cm, AffineTransform xform,
int bWidth, int bHeight, int maxw) {
this.colorModel = getInternedColorModel(cm);
this.bWidth = bWidth;
this.bHeight = bHeight;
this.maxWidth = maxw;
try {
xform = xform.createInverse();
} catch (NoninvertibleTransformException e) {
xform.setToScale(0, 0);
}
this.incXAcross = mod(xform.getScaleX(), bWidth);
this.incYAcross = mod(xform.getShearY(), bHeight);
this.incXDown = mod(xform.getShearX(), bWidth);
this.incYDown = mod(xform.getScaleY(), bHeight);
this.xOrg = xform.getTranslateX();
this.yOrg = xform.getTranslateY();
this.colincx = (int) incXAcross;
this.colincy = (int) incYAcross;
this.colincxerr = fractAsInt(incXAcross);
this.colincyerr = fractAsInt(incYAcross);
this.rowincx = (int) incXDown;
this.rowincy = (int) incYDown;
this.rowincxerr = fractAsInt(incXDown);
this.rowincyerr = fractAsInt(incYDown);
}
public static void main(final String[] args)
throws NoninvertibleTransformException {
final BufferedImage bi = new BufferedImage(300, 300,
BufferedImage.TYPE_INT_RGB);
final Graphics2D g = (Graphics2D) bi.getGraphics();
test(g);
g.translate(2.0, 2.0);
test(g);
g.translate(-4.0, -4.0);
test(g);
g.scale(2.0, 2.0);
test(g);
g.scale(-4.0, -4.0);
test(g);
g.rotate(Math.toRadians(90));
test(g);
g.rotate(Math.toRadians(90));
test(g);
g.rotate(Math.toRadians(90));
test(g);
g.rotate(Math.toRadians(90));
test(g);
g.dispose();
if (!status) {
throw new RuntimeException("Test failed");
}
}