Java 如何将BuffereImage旋转为对象本身
当我旋转每个图像时,当绘制它时,我的性能有问题,FPS从1400下降到6。我是否可以在创建BuffereImage对象后旋转它,以便只进行一次计算 这是我的网格控制类:(Drawing类,问题方法是drawRotated()) 还有我的地图创建者:(旋转部分坏了,我稍后会修复它,现在我需要解决我的6-FPS问题)Java 如何将BuffereImage旋转为对象本身,java,eclipse,frame-rate,Java,Eclipse,Frame Rate,当我旋转每个图像时,当绘制它时,我的性能有问题,FPS从1400下降到6。我是否可以在创建BuffereImage对象后旋转它,以便只进行一次计算 这是我的网格控制类:(Drawing类,问题方法是drawRotated()) 还有我的地图创建者:(旋转部分坏了,我稍后会修复它,现在我需要解决我的6-FPS问题) 包cs.meowxiik.universes.map; 导入cs.meowxiik.universes.graphics.GraphicsManager; 公共类MapCreator{
包cs.meowxiik.universes.map;
导入cs.meowxiik.universes.graphics.GraphicsManager;
公共类MapCreator{
图形管理器图形;
公共地图创建者(GraphicsManager图形){
这个。图形=图形;
}
公共地图getSpaceStationMap(){
地图;
int x=32;
int y=18;
瓷砖[][]瓷砖=新瓷砖[x][y];
对于(int i=0;i
}给定此代码
public void drawRotated(BufferedImage img,double rotationRequired,int x,int y,Canvas canvas, Graphics g, Observer observer) {
//double rotationRequired = Math.toRadians(degrees);
double locationX = img.getWidth() / 2;
double locationY = img.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage rotatedImage = op.filter(img, null); // my edit here
// Drawing the rotated image at the required drawing locations
g.drawImage(rotatedImage , x, y, null);
}
获取旋转图像的图像旋转仅取决于其大小(宽度/高度)和所需旋转
从我对代码的理解来看,所需的轮换基本上是固定的。调用Title
的setTexture
方法时,旋转是已知的
在这种情况下,rotateImage
可以在setTexture
方法中计算。只需创建一个存储计算的旋转图像的新属性。不要破坏原作
如果调用
setTexture
的频率与调用drawRotated
的频率一样高,那么这将无法实现您所期望的性能提升。当然可以。有一些代码可以帮助我们吗?当然,我可以,这是我的Tile类:。这是我的网格控件(基本上是绘图类)有很多不重要的代码,我的“drawRotated”方法有问题,还有我的Map creator类:(旋转发生的部分不起作用,我知道,我以后会解决这个问题)如果你能把代码与你的问题联系起来,会更好,使用链接不是现在的做法。@BrettWalker好的,我不想添加大量代码,我认为当我只需要一个简单的方法来旋转BuffereImage本身时,这是不必要的,但如果你喜欢示例,那就这样吧。好吧,这不是我想要的,但它会解决问题,所以谢谢:)
public class Tile {
boolean walkable;
BufferedImage image;
double radians;
int ID; //0 - floor, 1 - wall
public Tile(boolean walkable, BufferedImage image, int ID) {
this.walkable = walkable;
this.image = image;
this.ID = ID;
}
public BufferedImage getTexture() {
return image;
}
public boolean getWalkable() {
return walkable;
}
public int getDegrees() {
return (int) Math.toDegrees(radians);
}
public int getID() {
return ID;
}
public void setTexture(BufferedImage image, int degrees) {
this.image = image;
this.radians = Math.toRadians(degrees);
}
}
package cs.meowxiik.universes.map;
import cs.meowxiik.universes.graphics.GraphicsManager;
public class MapCreator {
GraphicsManager graphics;
public MapCreator(GraphicsManager graphics) {
this.graphics = graphics;
}
public Map getSpaceStationMap(){
Map map;
int x = 32;
int y = 18;
Tile[][] tiles = new Tile[x][y];
for(int i = 0; i < x; i++){
for(int ii = 0; ii < y; ii++){
tiles[i][ii] = new Tile(true, graphics.floor, 0);
}
}
for(int i = 0; i < x; i++){
for(int ii = 0; ii < y; ii++){
if(i == 0 || ii == 0 || i == x-1 || ii == y-1)tiles[i][ii] = new Tile(false,graphics.stWallZero, 1); //tiles.get(i).set(ii, new Tile(false,graphics.wall));
}
}
for(int i = 0; i < x; i++){
for(int ii = 0; ii < y; ii++){
if(tiles[i][ii].getID() == 0)break;
boolean connectedUp = false;
boolean connectedLeft = false;
boolean connectedRight = false;
boolean connectedBottom = false;
if(i + 1 < tiles.length)
if(tiles[i + 1][ii + 0].getID() == 1)
connectedRight = true;
if(i != 0)
if(tiles[i - 1][ii + 0].getID() == 1)
connectedLeft = true;
if(ii + 1 < tiles[i].length)
if(tiles[i + 0][ii + 1].getID() == 1)
connectedBottom = true;
if(ii != 0)
if(tiles[i + 0][ii - 1].getID() == 1)
connectedUp = true;
if(connectedUp && !(connectedLeft || connectedRight || connectedBottom))tiles[i][ii].setTexture(graphics.stWallOne, 0);
if(connectedLeft && !(connectedUp || connectedRight || connectedBottom))tiles[i][ii].setTexture(graphics.stWallOne, 90);
if(connectedRight && !(connectedUp || connectedLeft || connectedBottom))tiles[i][ii].setTexture(graphics.stWallOne, 180);
if(connectedBottom && !(connectedUp || connectedRight || connectedLeft))tiles[i][ii].setTexture(graphics.stWallOne, 270);
if((connectedUp && connectedBottom) && !(connectedLeft || connectedRight))tiles[i][ii].setTexture(graphics.stWallTwo, 90);
if(!(connectedUp && connectedBottom) && !(connectedLeft || connectedRight))tiles[i][ii].setTexture(graphics.stWallTwo, 0);
}
}
map = new Map(x,y,tiles);
return map;
}
public void drawRotated(BufferedImage img,double rotationRequired,int x,int y,Canvas canvas, Graphics g, Observer observer) {
//double rotationRequired = Math.toRadians(degrees);
double locationX = img.getWidth() / 2;
double locationY = img.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
BufferedImage rotatedImage = op.filter(img, null); // my edit here
// Drawing the rotated image at the required drawing locations
g.drawImage(rotatedImage , x, y, null);
}