Advertisement
Mouamle

Java Square BufferedImage

Aug 9th, 2016
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. public static BufferedImage Square(BufferedImage img) {
  2.     int w = img.getWidth();
  3.     int h = img.getHeight();
  4.  
  5.     BufferedImage main = null;
  6.     boolean isWidth = false;
  7.  
  8.     if (w > h) {
  9.         main = new BufferedImage(w, w, BufferedImage.TYPE_INT_ARGB);
  10.         isWidth = true;
  11.     } else if (h > w) {
  12.         main = new BufferedImage(h, h, BufferedImage.TYPE_INT_ARGB);
  13.         isWidth = false;
  14.     }
  15.  
  16.     Graphics g = main.getGraphics();
  17.     {
  18.         g.setColor(Color.white);
  19.         g.fillRect(0, 0, main.getWidth(), main.getHeight());
  20.  
  21.         int x = 0, y = 0;
  22.  
  23.         if (isWidth) {
  24.             g.drawImage(img, x, (main.getHeight()/2) - (img.getHeight()/2), null);
  25.         } else {
  26.             g.drawImage(img, (main.getWidth()/2) - (img.getWidth()/2), y, null);
  27.         }
  28.  
  29.     }
  30.     g.dispose();
  31.        
  32.     return main;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement