java - Is it truly never okay to call Component.getGraphics? -


madprogrammer made comment here stating should never ever use component.getgraphics, agree with: in virtually situations, wrong.

but take case of class created years ago. i'm posting think relevant code. can find full code here

public class mousedragoutliner extends mouseadapter implements mousemotionlistener { private class myrunnable implements runnable {     public void run() {         graphics g = mcomponent.getgraphics();         if (g == null) {             return;         }         graphics2d g2 = (graphics2d) g;         stroke s = g2.getstroke();         g2.setstroke(dash_stroke);         int x = math.min(mstart.x, mend.x);         int y = math.min(mstart.y, mend.y);         int w = math.abs(mend.x - mstart.x);         int h = math.abs(mend.y - mstart.y);         if (w == 0 || h == 0) return;         g2.setxormode(color.white);         if (mcustomshape != null) {             rectangle r = mcustomshape.getbounds();             affinetransform scale = affinetransform.getscaleinstance(w / (double)r.width, h / (double)r.height);             affinetransform trans = affinetransform.gettranslateinstance(x - r.x, y-r.y);             g2.transform(trans);             g2.transform(scale);             g2.draw(mcustomshape);         } else {             if (mshape == rectangle) g2.drawrect(x, y, w, h);             else if (mshape == oval) g2.drawoval(x, y, w, h);             else if (mshape == line) g2.drawline(mstart.x, mstart.y, mend.x, mend.y);         }         g2.setstroke(s);         } }  public void domousedragged(mouseevent me) {     mend = me.getpoint();     if (mstart != null) {         mcomponent = me.getcomponent();         mcomponent.repaint();         swingutilities.invokelater(mrunner);     } } } 

domousedragged called both mousedragged , mousemoved (when configured to). in case it's not obvious, call repaint on component (so component ready paint on), run mrunner after. i've used in number of situations quite successfully, benefits being component mousedragoutliner doesn't need customization (in paintcomponent, specifically) accomodate.

if never okay call component.getgraphics, can suggest better solution this?

thanks!


http://sourceforge.net/p/tus/code/head/tree/tjacobs/ui/drag/mousedragoutliner.java#l66

i never never. think need aware of drawbacks when using approach not recommended.

the main point getgrapahics() painting temporary. whenever swing determines component needs repainted lose custom painting. example, try creating outline , minimize or maximize frame , outline lost. if use alt+tab switch application in windows lose painting.

i agree in case not high probability user that, may cause problems , result in bug report. need decide if care problem or not. knows, maybe requirement not big deal?

as suggested @kiheru can use jlayer class. don't think jlayer part of api when created code. not familiar swing tutorial has section on responding events when using jlayer class, seems indicate can want.

when using jlayer can't dynamically add functionality component (the way code does). need add functionality @ design time, don't think should problem.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -