java - How to setup desired Coordinates in Graphics2D -
i want set mathematical (where y grows not down) coordinate space (-1, -1) (+1, +1) , have fit in window regardless of window size.
i using anonymous jcomponent subclass in java se 7 , casting incoming graphics
in paintcomponent
graphics2d
, drawing on graphics2d
but graphics2d
set computer coordinate space changes size of window. how rescale according window size , have y go upwards? following program should show dark square in upper right quadrant.
import java.awt.graphics; import java.awt.graphics2d; import java.awt.geom.rectangle2d; import javax.swing.jcomponent; import javax.swing.jframe; public class g { public static void main (string [] args) { jframe frame = new jframe(g.class.getcanonicalname()); frame.setundecorated(true); jcomponent component = new jcomponent() { private static final long serialversionuid = 1l; @override protected void paintcomponent (graphics g) { super.paintcomponent(g); paint2d((graphics2d)g); } protected void paint2d (graphics2d g2) { g2.draw(new rectangle2d.double(0.1, 0.1, 0.9, 0.9)); } }; frame.add(component); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(400, 400); frame.setvisible(true); } }
setup coordinate system how want, using transform()
, translate()
. so:
- you want origin @ (0, height); bottom left.
- then want flip y axis.
example code:
affinetransform tform = affinetransform.gettranslateinstance( 0, height); tform.scale( 1, -1); g2.settransform( tform);
[my edited version]:
public static void main (string [] args) { jframe frame = new jframe( g2dtransform_question.class.getcanonicalname()); jcomponent component = new jcomponent() { private static final long serialversionuid = 1l; @override protected void paintcomponent (graphics g) { super.paintcomponent(g); paint2d((graphics2d)g); } protected void paint2d (graphics2d g2) { affinetransform tform = affinetransform.gettranslateinstance( 0, getheight()); tform.scale( getwidth(), -getheight()); // note -- make 1.0 'full width'. g2.settransform( tform); g2.setcolor( color.blue); // note -- can *see* something. g2.fill( new rectangle2d.double(0.1, 0.1, 0.8, 0.8)); // note -- 'fill' works better 'draw'. } }; frame.setlayout( new borderlayout()); // note -- make component size frame. frame.add( component, borderlayout.center); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(400, 400); frame.setvisible(true); }
[hovercraft's version]: hover!
import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.geom.affinetransform; import java.awt.geom.rectangle2d; import javax.swing.jcomponent; import javax.swing.jframe; public class g { public static final int pref_w = 400; public static final int pref_h = pref_w; public static void main (string [] args) { jframe frame = new jframe(g.class.getcanonicalname()); frame.setundecorated(true); jcomponent component = new jcomponent() { private static final long serialversionuid = 1l; @override protected void paintcomponent (graphics g) { super.paintcomponent(g); affinetransform tform = affinetransform.gettranslateinstance( 0, getheight()); tform.scale( 1, -1); graphics2d g2 = (graphics2d) g.create(); g2.settransform( tform); paint2d(g2); g2.dispose(); } protected void paint2d (graphics2d g2) { g2.draw(new rectangle2d.double(10, 10, 20, 30)); } @override public dimension getpreferredsize() { return new dimension(pref_w, pref_h); } }; frame.add(component); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setvisible(true); } }
Comments
Post a Comment