diff --git a/src/main/java/ruscz/game/gameOfProd/Image/ImagePerson.java b/src/main/java/ruscz/game/gameOfProd/Image/ImagePerson.java index b1d929f..66ddf38 100644 --- a/src/main/java/ruscz/game/gameOfProd/Image/ImagePerson.java +++ b/src/main/java/ruscz/game/gameOfProd/Image/ImagePerson.java @@ -1,5 +1,7 @@ package ruscz.game.gameOfProd.Image; +import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; + import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.*; @@ -12,6 +14,8 @@ public class ImagePerson { private BufferedImage[] imagesOfDirection; private int subimageWidth; private int subimageHeight; + private Dimension imgSize = new Dimension(subimageWidth, subimageHeight); + private Dimension boundary = new Dimension(WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE); public void setAllImagesOfDirection(Rectangle[] rectangles){ imagesOfDirection = new BufferedImage[12]; @@ -22,7 +26,8 @@ public class ImagePerson { int count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { - imagesOfDirection[count] = image.getSubimage(subimageWidth * i, subimageHeight * j, subimageWidth, subimageHeight); + BufferedImage img = image.getSubimage(subimageWidth * i, subimageHeight * j, subimageWidth, subimageHeight); + imagesOfDirection[count] = resize(img); count++; } } @@ -43,6 +48,31 @@ public class ImagePerson { setAllImagesOfDirection(rectangles); } + public BufferedImage resize(BufferedImage img) { + Image tmp; + if (subimageWidth > WindowOfFreeMove.CELL_SIZE) { + int newHeight = subimageHeight * WindowOfFreeMove.CELL_SIZE / subimageWidth; + tmp = img.getScaledInstance(WindowOfFreeMove.CELL_SIZE, newHeight, Image.SCALE_SMOOTH); + } + else if (subimageHeight > WindowOfFreeMove.CELL_SIZE) { + int newWidth = subimageWidth * WindowOfFreeMove.CELL_SIZE / subimageHeight; + tmp = img.getScaledInstance(newWidth, WindowOfFreeMove.CELL_SIZE, Image.SCALE_SMOOTH); + } + else if (subimageWidth < WindowOfFreeMove.CELL_SIZE && subimageHeight < WindowOfFreeMove.CELL_SIZE) { + int newWidth = subimageWidth * WindowOfFreeMove.CELL_SIZE / subimageHeight; + tmp = img.getScaledInstance(newWidth, WindowOfFreeMove.CELL_SIZE, Image.SCALE_SMOOTH); + } + else { + tmp = img.getScaledInstance(subimageWidth, subimageHeight, Image.SCALE_SMOOTH); + } + BufferedImage dimg = new BufferedImage(WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, BufferedImage.TYPE_INT_ARGB); + Graphics2D g2d = dimg.createGraphics(); + g2d.drawImage(tmp, (WindowOfFreeMove.CELL_SIZE - tmp.getWidth(null)) / 2, (WindowOfFreeMove.CELL_SIZE - tmp.getHeight(null)) / 2, null); + g2d.dispose(); + + return dimg; + } + public BufferedImage[] getImagesOfDirection() { return imagesOfDirection; diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java index 0ecca05..6e3d0af 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java @@ -13,7 +13,7 @@ import java.awt.event.KeyEvent; import java.util.LinkedList; public class WindowOfFreeMove extends JFrame { - public static final int CELL_SIZE = 40; + public static final int CELL_SIZE = 50; public static int CANVAS_WIDTH = 50; public static int CANVAS_HEIGHT = 50; public static final int WINDOW_WIDTH = 11;