graphics - Jr Java-er Wants to Pass Params to Paint -
i'm able draw rectangles, elipses, , lines in java means of adding component extends jcomponent in modify paintcomponent method:
public class mycomponent extends jcomponent { public void paintcomponent(graphics g) { /* simple draw stuff */ } }
i know how have class extend either japplet or jpanel , draw in paint method:
public class myclass extends jpanel { public void paint(graphics g) { /* simple draw stuff */ } }
but, both of these methods suffer not allowing me pass them parameters. in case of multiframe animated sprite, conceivably have external variable reads determine frame number , internally draws appropriate "sprite" contents based on frame number, i'd prefer able pass frame number directly. unfortunately, not not know called from, don't know the graphics g requires input.
there may better way accomplish want, directly communicate draw routine tell draw want, whenever desire, don't know how accomplish this.
if such method possible, how done? if better use existing paint or paintcomponent methods, how can best pass additional information them?
apparently wasn't clear in asked. wish have component or other entity has own paintcomponent or paint method, inside of which, based on either framenumber parameter passed it, or apparently-more-likely, class property such framenumber can access, method determines frame of sprite draw.
importantly, though, wish able re-call paint or paintcomponent redraw sprite when frame number changes. big confusion comes in not knowing how re-call method, which, best of understanding, called when frame resized or otherwise redrawn.
so, how can redraw component/object/entity, frame-by-frame?
firstly, don't override paint
, use paintcomponent
.
secondly, need define kind of model records state of graphical objects. when paintcomponent
called, need render state. instead of trying pass parameters paint methods, should have method allows paint methods access model (ie getmodel
) passed component @ earlier time.
then update engine update model , component paint model
for example ... use timer when key pressed
Comments
Post a Comment