java - Rotate image without clear background -
i have read lot of articles of drawing images, cant work when need keep background. i'm trying rotate image on image after click on jbutton. background image generated on jpanel by:
public void paintcomponent(graphics g){ int index = 0; graphics2d g2 = (graphics2d) g; super.paintcomponent(g); try { image = imageio.read(url1); image2 = imageio.read(url2); image3 = imageio.read(url3); } catch (ioexception e) { } g.drawimage(image, 0, 0, null); g.drawimage(image3, 0, 0, null); if(scaledrawnflag == 0){ for(index = 0; index < 60; index ++){ tx = affinetransform.getrotateinstance(math.toradians(6*index), this.getheight()/2, this.getwidth()/2); op = new affinetransformop(tx, affinetransformop.type_bilinear); g.drawimage(op.filter(image3, null), 0, 0, null); } scaledrawnflag = 1; } g.drawimage(image2, 0, 0, null); }
which jpanel named panel , draw image 1 time keep refresh performance, animated image. draws scale tachometer, total of 60 lines, each line copy of image3
the animated image, generated pressing jbutton, , made by:
public void paintcomponent(graphics g){ super.paintcomponent(g); bufferedimage img = new bufferedimage(370, 370, bufferedimage.translucent); graphics2d g2d = img.creategraphics(); g2d.setcomposite(alphacomposite.getinstance(alphacomposite.clear, 0.0f)); g2d.fillrect(0, 0, img.getwidth(), img.getheight()); g2d.setcomposite(alphacomposite.getinstance(alphacomposite.src_over, 0.0f)); graphics2d temp = (graphics2d) g; tx = affinetransform.getrotateinstance(math.toradians(degrees), this.getheight()/2, this.getwidth()/2); op = new affinetransformop(tx, affinetransformop.type_bilinear); temp.drawimage(op.filter(image2, null), 0, 0, null); temp.dispose(); }
which jpanel named overpanel, on initial jpanel.
but, when call methods:
public void up(){ degrees ++; if(degrees == 360) degrees = 0; repaint(); } public void down(){ degrees --; if(degrees == -360) degrees = 0; repaint(); }
which on overpanel class, jpanel entirely cleared. animation working but, background disappear.
what must keep background?
i tried solution, drawing 60 lines again on every up() , down() call. background repainted, takes time complete, so, animation rotate tachometer's indicator lags.
never dispose of graphics object given jvm. you're doing this:
// temp **is** same object g , graphics object given jvm graphics2d temp = (graphics2d) g; //.... temp.dispose();
and shouldn't since breaks painting chain. should instead disposing of g2d object, 1 created.
also, ok
graphics2d temp = (graphics2d) g.create(); // temp **is** new object //.... temp.dispose(); // ok
other issues:
- i wouldn't creating bufferedimage inside of paintcomponent rather make field of class, , display inside paintcomponent.
- your top code shows ignored critical exceptions -- don't want this.
- it shows reading in of image files within painting method, unnecessarily slow down graphics. again, don't this, read in images once outside of painting method, store results, , use them in painting method.
- the
paintcomponent
methodprotected
, notpublic
. avoid increasing visibility unnecessarily.
in minimal example program, scaledrawnflag
variable , associated if-block appear messing up. purpose of variable , if block? if rid of variable , if block, background persist. myself, i'd things differently creating stable background image , drawing every time in paintcomponent(...)
method. not override update(...)
either that's awt kludge , not swing graphics. try avoid null
layouts , setbounds(...)
plague since leads inflexible, rigid gui's difficult debug, maintain , enhance. example:
import java.awt.borderlayout; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.gridlayout; import java.awt.renderinghints; import java.awt.event.actionevent; import java.awt.event.keyevent; import java.awt.geom.affinetransform; import java.awt.image.affinetransformop; import java.awt.image.bufferedimage; import java.io.ioexception; import java.net.url; import javax.imageio.imageio; import javax.swing.*; @suppresswarnings("serial") public class mymainpanel extends jpanel { private mydrawingpanel mydrawingpanel; public mymainpanel() { try { mydrawingpanel = new mydrawingpanel(); } catch (ioexception e) { e.printstacktrace(); system.exit(-1); } jpanel rightpanel = new jpanel(); rightpanel.setlayout(new gridlayout(0, 1, 5, 5)); rightpanel.add(new jbutton(new myupaction("up", keyevent.vk_u))); rightpanel.add(new jbutton(new mydownaction("down", keyevent.vk_d))); jpanel rightwrappanel = new jpanel(new borderlayout()); rightwrappanel.add(rightpanel, borderlayout.page_start); setborder(borderfactory.createemptyborder(5, 5, 5, 5)); setlayout(new borderlayout()); add(mydrawingpanel, borderlayout.center); add(rightwrappanel, borderlayout.line_end); } private class myupaction extends abstractaction { public myupaction(string name, int mnemonic) { super(name); putvalue(mnemonic_key, mnemonic); } @override public void actionperformed(actionevent e) { mydrawingpanel.up(); } } private class mydownaction extends abstractaction { public mydownaction(string name, int mnemonic) { super(name); putvalue(mnemonic_key, mnemonic); } @override public void actionperformed(actionevent e) { mydrawingpanel.down(); } } private static void createandshowgui() { jframe frame = new jframe("mymainpanel"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(new mymainpanel()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } } @suppresswarnings("serial") class mydrawingpanel extends jpanel { private static final string needle_img_path = "http://1.bp.blogspot.com/" + "-fq-opgbslp4/ttoj7doamwi/aaaaaaaabtc/t7gkjlfrquo/s400/secondhand.png"; private static final string orange_disk_img_path = "http://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/circle_fulvous_solid.svg/200px-circle_fulvous_solid.svg.png"; private static final string green_line_img_path = "http://www.xtremeskater.com/math/images/circle_radius.png"; private static final int max_degrees = 360; private int imgwidth = 0; private int imgheight = 0; private bufferedimage needleimg = null; private bufferedimage orangediskimg = null; private bufferedimage greenlineimg = null; private bufferedimage backgroundimg = null; private int degrees; public mydrawingpanel() throws ioexception { url needleurl = new url(needle_img_path); url orangediskurl = new url(orange_disk_img_path); url greenlineurl = new url(green_line_img_path); needleimg = imageio.read(needleurl); orangediskimg = imageio.read(orangediskurl); greenlineimg = imageio.read(greenlineurl); imgwidth = math.max(orangediskimg.getwidth(), greenlineimg.getwidth()); imgheight = math.max(orangediskimg.getheight(), greenlineimg.getheight()); backgroundimg = new bufferedimage(imgwidth, imgheight, bufferedimage.type_int_argb); graphics2d g2 = backgroundimg.creategraphics(); drawbackground(g2, imgwidth, imgheight); g2.dispose(); } @override public dimension getpreferredsize() { if (ispreferredsizeset()) { return super.getpreferredsize(); } return new dimension(imgwidth, imgheight); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); if (backgroundimg != null) { g.drawimage(backgroundimg, 0, 0, null); } affinetransform tx = affinetransform.getrotateinstance(math.toradians(degrees), this.getheight() / 2, this.getwidth() / 2); affinetransformop op = new affinetransformop(tx, affinetransformop.type_bilinear); g.drawimage(op.filter(needleimg, null), 0, 0, null); } public void up() { degrees++; degrees %= max_degrees; repaint(); } public void down() { degrees--; degrees += max_degrees; degrees %= max_degrees; repaint(); } public int getdregrees() { return degrees; } private void drawbackground(graphics2d g2, int biwidth, int biheight) { int index; g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g2.drawimage(orangediskimg, 0, 0, null); g2.drawimage(greenlineimg, 0, 0, null); affinetransform tx = affinetransform.getrotateinstance(math.toradians(6), biwidth / 2, biheight / 2); (index = 0; index < 60; index++) { g2.transform(tx); g2.drawimage(greenlineimg, 0, 0, null); } } }
Comments
Post a Comment