gpt4 book ai didi

java - getSubimage(...) 如何工作?

转载 作者:行者123 更新时间:2023-11-30 03:37:38 28 4
gpt4 key购买 nike

我正在尝试从 BufferedImage 中提取图像的一部分。我该怎么做呢?它不允许我为 y 设置大于零的值。

例如:

getSubimage(0,0,theImage.getWidth(),theImage.getHeight()); //OK
getSubimage(0,(int)theImage.getHeight()/2,theImage.getWidth(), theImage.getHeight()); //ERROR!

这是错误消息:

Exception in thread "main" java.awt.image.RasterFormatException: (y + height) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
at java.awt.image.BufferedImage.getSubimage(Unknown Source)
at Loop.renderImages(Loop.java:147)
at Loop.run(Loop.java:122)
at Driver.main(Driver.java:9)

这是完整的代码。我知道这很困惑...对于编码来说还是个新手。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;

import javax.imageio.ImageIO;


public class Loop implements Runnable{

//458 = max right boundary
private static long now, lastSecond, lastFrame, deltaSecond, deltaFrame;
private boolean on = true;
private static final long second = 1000000000;
private static final int FPS = 60;
private static final long frame = second/FPS;
private static int currentFPS, framesCurrentSecond, totalFrames, seconds;
private static boolean running = true;
private static int SCPosX, SCPosY;
private static int SCXOffset, SCYOffset, bottomOffset = 120;
private static int gameWidth = FrameBuilder.WIDTH;
private static int gameHeight = FrameBuilder.HEIGHT;
private int score;
private static BufferedImage spaceCraft, spaceCraft2;
private static ImageBufferer IB;
private static FrameBuilder fb;
private Thread spaceShipThread = new Thread();
private static int boundaryLeft, boundaryRight, boundaryUp, boundaryDown;
private static boolean moveUp = false, moveDown = false, moveRight = false, moveLeft = false;
private static boolean fireMainCannon = false;
private static long cannonTick = second/3;
private static long deltaCannonTick = 0;
private static final int healthMax = 458;
private Timer animationTimer;
private static BufferedImage starOne, starOneSub;
private static int starOneY = 0, starOneStart, starOneEnd;

public Loop()
{
File S1 = new File("C:\\Users\\Andrew\\Documents\\star1.png");
try {
starOne = ImageIO.read(S1);
} catch (IOException e) {
e.printStackTrace();
}
starOneStart = (int)starOne.getHeight() - starOne.getHeight();
starOneEnd = starOne.getHeight();
animationTimer = new Timer(80, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
on = !on;
}

});
Timer starOneTimer = new Timer(100, new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(starOneStart > 0 && starOneEnd > 0)
{
starOneStart--;
starOneEnd--;
}
else
{
starOneStart = starOne.getHeight()-((int)starOne.getHeight()/4);
starOneEnd = starOne.getHeight();
}
}

});
starOneTimer.start();
animationTimer.start();

SCXOffset = 0;
SCYOffset = 0;
spawnSpaceShip();



IB = new ImageBufferer();
fb = new FrameBuilder();
fb.c.addKeyListener(new Controls());
fb.setDefaultComponents();
score = 0;


boundaryLeft = 24;
boundaryRight = gameWidth-spaceCraft.getWidth()-64;
boundaryDown = gameHeight-bottomOffset;
SCPosX = (gameWidth/2)-(spaceCraft.getWidth()+16);
SCPosY = boundaryDown - spaceCraft.getHeight()/2;
boundaryUp = -5;

now = System.nanoTime();
lastSecond = now;
lastFrame = now;
seconds = 0;
framesCurrentSecond = 0;
totalFrames = 0;

}

public void run() {
while(running)
{
now = System.nanoTime();
deltaSecond = now - lastSecond;
deltaFrame = now - lastFrame;
if(deltaFrame >= frame)
{
handleSpaceCraft(SCXOffset, SCYOffset);
renderImages(IB.getImages(), IB.getXCoords(), IB.getYCoords());
IB.clearLists();
lastFrame = now;
framesCurrentSecond++;

}
if(deltaSecond >= second)
{
lastSecond = now;
seconds++;
totalFrames += framesCurrentSecond;
System.out.println("FPS: " + framesCurrentSecond + " TOTAL: " + totalFrames);
framesCurrentSecond = 0;
}
}
}
public void renderImages(ArrayList<BufferedImage> images, ArrayList<Integer> x, ArrayList<Integer> y)
{
BufferStrategy bs = FrameBuilder.getCanvas().getBufferStrategy();
if(bs==null)
{
FrameBuilder.getCanvas().createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
starOneSub = starOne.getSubimage(0, (int)starOne.getHeight()/2, starOne.getWidth(), starOneEnd);
for(int i = 0; i < images.size(); i++)
{
g.drawImage(starOneSub, 0, 0, starOneSub.getWidth()+500, starOneSub.getHeight()+600,null);
g.setColor(Color.RED);
g.fillRect(24, gameHeight-72, SpaceCraft.maxHealth*4, 20);
g.setColor(Color.GREEN);
g.fillRect(24, gameHeight-72, SpaceCraft.health*(4) , 20);
g.drawImage(images.get(i), x.get(i), y.get(i) , images.get(i).getWidth()+32, images.get(i).getHeight()+32, null);
g.dispose();

}
bs.show();
}
public void handleSpaceCraft(int x, int y)
{
if(moveUp)
{
SCYOffset-=10;
if(SCPosY+SCYOffset <= boundaryUp)
SCYOffset+=10;
}
if(moveDown)
{
SCYOffset+=10;
if(SCPosY+SCYOffset >= boundaryDown)
SCYOffset-=10;
}
if(moveRight)
{
SCXOffset+=7;
if(SCPosX+SCXOffset >= boundaryRight)
SCXOffset-=7;
}
if(moveLeft)
{
SCXOffset-=7;
if(SCPosX+SCXOffset <= boundaryLeft)
SCXOffset+=7;
}
if(on)
IB.addImage(spaceCraft, SCPosX+SCXOffset, SCPosY+SCYOffset);
else
IB.addImage(spaceCraft2, SCPosX+SCXOffset, SCPosY+SCYOffset);
}
public class Controls implements KeyListener
{

@Override
public void keyTyped(KeyEvent e)
{

}

@Override
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_W)
moveUp = true;
if(key == KeyEvent.VK_A)
moveLeft = true;
if(key == KeyEvent.VK_D)
moveRight = true;
if(key == KeyEvent.VK_S)
moveDown = true;
if(key == KeyEvent.VK_SPACE)
fireMainCannon = true;
}

@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_S)
moveDown = false;
if(key == KeyEvent.VK_D)
moveRight = false;
if(key == KeyEvent.VK_W)
moveUp = false;
if(key == KeyEvent.VK_A)
moveLeft = false;
if(key == KeyEvent.VK_SPACE)
{
fireMainCannon = false;
}

}
}
public void spawnSpaceShip()
{
File SC1 = new File("C:\\Users\\Andrew\\Documents\\spaceCraftGray1.png");
File SC2 = new File("C:\\Users\\Andrew\\Documents\\spaceCraftGray2.png");
try {
spaceCraft = ImageIO.read(SC1);
spaceCraft2 = ImageIO.read(SC2);
} catch (IOException e) {
e.printStackTrace();
}
}

}

最佳答案

根据您调用该函数的方式,您似乎假设先传递左上角,然后传递右下角。

如果你看Javadoc对于该方法,您将观察到参数为 x,y,w,h。

也就是说,您首先使用 x,y 提供左上角,然后提供宽度和高度。因此,如果您尝试获取图像的下半部分,正确的调用是将提供的高度减半。

getSubimage(0,(int)theImage.getHeight()/2,theImage.getWidth(), theImage.getHeight() / 2); 

关于java - getSubimage(...) 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27474933/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com