java - Finding AffineTransform origin on screen -
let's have affinetransform (transform
) , call bunch of it's methods. lets rotate , translate it. transform graphics object (g2d
) it:
g2d.transform(transform);
i want find coordinate on my screen new (0, 0) is. if drew rectangle @ coordinates untransformed g2d
, 1 transformed g2d
overlap. how can point, have math, affinetransform or graphics2d have built in way (i couldn't find one)?
you could...
create copy of original graphics
context (graphics#create
) (which idea) apply transform copy (this leave original unaffected, don't forget dispose
of copy when you're done)
import java.awt.dimension; import java.awt.eventqueue; import java.awt.fontmetrics; import java.awt.graphics; import java.awt.graphics2d; import java.awt.geom.affinetransform; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import java.util.logging.level; import java.util.logging.logger; import javax.imageio.imageio; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class test { public static void main(string[] args) { new test(); } public test() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { private bufferedimage img; public testpane() { try { img = imageio.read(your source image here...); } catch (ioexception ex) { ex.printstacktrace(); } } @override public dimension getpreferredsize() { return img == null ? new dimension(200, 200) : new dimension(img.getwidth(), img.getheight()); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g.create(); int x = (getwidth() - img.getwidth()) / 2; int y = (getheight() - img.getheight()) / 2; affinetransform @ = affinetransform.gettranslateinstance(x, y); at.rotate(math.toradians(180), img.getwidth() / 2, img.getheight() / 2); g2d.settransform(at); g2d.drawimage(img, 0, 0, this); g2d.dispose(); // don't touching original ;) g2d = (graphics2d) g.create(); fontmetrics fm = g2d.getfontmetrics(); g2d.drawstring("top left", 0, 0 + fm.getascent()); string txt = "bottom right"; g2d.drawstring(txt, getwidth() - fm.stringwidth(txt) - 1, ((getheight() - fm.getheight()) - 1) + fm.getascent()); g2d.dispose(); } } }
you could...
reverse transformation, tricky if it's complex transformation...
you could...
get reference original transformation first, apply new transform, paint ever needed painted , reapply original transformation
you could...
use affinetransform#settoidentity
"reset" transform it's original state...
affinetransform @ = g2d.gettransform(); at.translate(x, y); at.rotate(math.toradians(180), img.getwidth() / 2, img.getheight() / 2); // drawing... at.settoidentity(); g2d.settransform(at); // basics