Thursday, 15 May 2014

Java - Method that resizes, rotates and draws a image after arguments -



Java - Method that resizes, rotates and draws a image after arguments -

im trying create method in java resize rotate , draw image after given arguments. not work tho. idk how? btw posible normal graphic class , not graphic2d ? thanks!

public void drawresizedandrotatedimage(image image, graphics g, int x, int y, int sizex, int sizey, double degrees){ //resize bufferedimage im = new bufferedimage(sizex, sizey, bufferedimage.type_4byte_abgr); @suppresswarnings("unused") graphics g2 = (graphics)im.getgraphics(); //rotate imageicon imageicon = new imageicon(image); bufferedimage bufferedimage = new bufferedimage(imageicon.geticonwidth(), imageicon.geticonheight(), bufferedimage.type_4byte_abgr); graphics2d g2d = (graphics2d)bufferedimage.getgraphics(); g2d.rotate(math.toradians(degrees), imageicon.geticonwidth() / 2, imageicon.geticonheight() / 2); //draw g.drawimage(image, x, y, sizex, sizey, null); }

okay, create bufferedimage , grab it's graphics context...

bufferedimage im = new bufferedimage(sizex, sizey, bufferedimage.type_4byte_abgr); graphics g2 = (graphics)im.getgraphics();

and nil it...

you create sec bufferedimage, grab it's graphics context, set it's rotational context , paint nil it...

bufferedimage bufferedimage = new bufferedimage(imageicon.geticonwidth(), imageicon.geticonheight(), bufferedimage.type_4byte_abgr); graphics2d g2d = (graphics2d)bufferedimage.getgraphics(); g2d.rotate(math.toradians(degrees), imageicon.geticonwidth() / 2, imageicon.geticonheight() / 2);

you paint original image supplied graphics context...

g.drawimage(image, x, y, sizex, sizey, null);

having achieved nil @ all...

i think need start taking read through transforming shapes, text, , images grounding on basics...

transformations compounding, effect whatever painted graphics context after have been applied.

the simplest approach utilize affinetransform

double scalewidth = (double)sizex / (double)image.getwidth(this); double scaleheight = (double)sizey / (double)image.getheight(this); bufferedimage img = new bufferedimage(sizex, sizey, bufferedimage.type_int_argb); graphics2d g2d = img.creategraphics(); affinetransform @ = new affinetransform(); at.scale(scalewidth, scaleheight); at.rotate(math.toradians(degrees), image.getwidth(this) / 2d, image.getheight(this) / 2d); g2d.settransform(at); g2d.drawimage(image, sizex / 2, sizey / 2, null); g2d.dispose(); // , finally, result... g.drawimage(img, x, y, null);

for example...

import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.geom.affinetransform; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class testimage { public static void main(string[] args) { new testimage(); } public testimage() { eventqueue.invokelater(new runnable() { @override public void run() { seek { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } grab (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { private bufferedimage original; public testpane() { seek { original = imageio.read(new file("your image here")); } grab (ioexception ex) { ex.printstacktrace(); } } @override public dimension getpreferredsize() { homecoming new dimension(200, 200); } public image scaleandrotate(image image, int sizex, int sizey, double degrees) { double scalewidth = (double)sizex / (double)image.getwidth(this); double scaleheight = (double)sizey / (double)image.getheight(this); bufferedimage img = new bufferedimage(sizex, sizey, bufferedimage.type_int_argb); graphics2d g2d = img.creategraphics(); affinetransform @ = new affinetransform(); at.scale(scalewidth, scaleheight); at.rotate(math.toradians(degrees), image.getwidth(this) / 2d, image.getheight(this) / 2d); g2d.settransform(at); g2d.drawimage(image, sizex / 2, sizey / 2, null); g2d.dispose(); homecoming img; } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g.create(); int scalewidth = getwidth() / 2; int scaleheight = getheight() / 2; image img = scaleandrotate(original, scalewidth, scaleheight, 45.0); int x = (getwidth() - img.getwidth(this)) / 2; int y = (getheight()- img.getheight(this)) / 2; g2d.drawimage(img, x, y, this); g2d.dispose(); } } }

you may take read through...

java: maintaining aspect ratio of jpanel background image quality of image after resize low -- java

for discussions scaling algorithms , techniques

you should know size of image when rotates changes, have @ (one of favourite answers refer to) rotate image in java more details how calculate resulting image size of rotated image...

java image rotation resize draw

No comments:

Post a Comment