swing - Java Graphics: setClip vs clipRect vs repaint(int,int,int,int) -
similar last post apologies, lot less long winded. wondering best option optimising redrawing jframe/jpanel when each repaint call small part of screen redrawn.
also apart overloading repaint not 100% on how implement setclip or cliprect. understanding used when overriding paint or update? see below code:
public class aquasim extends jpanel { //variables //other methods... public void paint (graphics g) { super.paintcomponent(g); //needed? graphics2d g2d = (graphics2d) g; //draws land area penguins. g2d.setcolor(new color(121,133,60)); g2d.fill(land); g2d.setcolor(color.blue); g2d.fill(sea); drawcreatures(g2d); } public void drawcreatures(graphics2d g2d) { (creature c : crlist) //a list of alive creatures { //each creature object stores image , coords. g2d.drawimage(c.createimage(txs,tys), c.getpos(1).width, c.getpos(1).height, this); } } }
ideally prefer not have loop though each creature object each repaint request, part of reason post. dont know if there way of sending current creature being drawn paint or overriding paint in creature class draw on graphics object in main class. bit more code...
private class creature implements actionlistener { //variables & other methods @override public void actionperformed(actionevent e) { if (getstate()!=state.dead) { move(); repaint(); //<---would rather set clipping area in paint/update. x,y,w,h needs include icon & info box. //repaint(gx,gy,getpos(1).width,getpos(1).height); } else anim.stop(); } //... public void move() { //determines how move , sets here updating position used in drawcreatures. } }
so suggestions efficient method use? baring in mind repaint called lot many objects/creatures i.e. many times second, hence why dont want redrawing everythin on screen each repaint request.
only small part of screen redrawn.
use repaint(....)
.
the repaintmanager worry needs painted , set clip of graphics object you.
Comments
Post a Comment