如何渲染具有特定视野的图像?
How can I render a image with a certain field of view?
晚上好!我已经使用 java 和图形对象成功显示了图像。
这是我的 'Renderer'-Class:
public class Renderer {
private static BufferStrategy bs;
private static Graphics g;
private static Window win;
public static final int MIN_BUFFER_COUNT = 1;
public static final int MAX_BUFFER_COUNT = 4;
public static void prepareRenderer(BufferStrategy bs) {
Renderer.bs = bs;
g = bs.getDrawGraphics();
}
public static BufferStrategy createBufferStrategy(Window window, int bufferCount) {
if(window.getCanvas().getBufferStrategy() == null) {
window.getCanvas().createBufferStrategy(bufferCount);
}
win = window;
BufferStrategy _bs = window.getCanvas().getBufferStrategy();
return _bs;
}
public static void clearWindow(Window window) {
g.clearRect(0, 0, window.getWidth(), window.getHeight());
}
public static void drawRectangle(int x, int y, int width, int height) {
g.fillRect(x, y, width, height);
}
public static void setColor(Color color) {
g.setColor(color);
}
public static void drawImage(BufferedImage image, int x, int y, int width, int height) {
g.drawImage(image, x, y, win.getWidth() / width * Config.getField_of_view(), win.getHeight() / height * Config.getField_of_view(), null);
}
public static void drawSprite(Sprite sprite, int x, int y, int width, int height) {
g.drawImage(sprite.getImage(), x, y, (int) (win.getWidth() / width * Config.getField_of_view()), (int) (win.getHeight() / height * Config.getField_of_view()), null);
}
public static void closeRenderer() {
bs.show();
g.dispose();
}
我在渲染器中编写了这个奇怪的代码,因为无论 window 大小如何,我都希望有一定的视野。
这是我在Main-Class:
中写的
@Override
public void render() {
//Prepare
Renderer.prepareRenderer(Renderer.createBufferStrategy(window, Renderer.MAX_BUFFER_COUNT));
//Clear
Renderer.clearWindow(window);
//Draw
Renderer.drawSprite(sprite, 0, 0, 100, 100);
//Close
Renderer.closeRenderer();
}
但现在我遇到了一个问题,因为当我将 window 做成一个矩形时,它看起来很拉伸。
这与 Math.min(win.getWidth(), win.getHeight())
一起工作谢谢
晚上好!我已经使用 java 和图形对象成功显示了图像。 这是我的 'Renderer'-Class:
public class Renderer {
private static BufferStrategy bs;
private static Graphics g;
private static Window win;
public static final int MIN_BUFFER_COUNT = 1;
public static final int MAX_BUFFER_COUNT = 4;
public static void prepareRenderer(BufferStrategy bs) {
Renderer.bs = bs;
g = bs.getDrawGraphics();
}
public static BufferStrategy createBufferStrategy(Window window, int bufferCount) {
if(window.getCanvas().getBufferStrategy() == null) {
window.getCanvas().createBufferStrategy(bufferCount);
}
win = window;
BufferStrategy _bs = window.getCanvas().getBufferStrategy();
return _bs;
}
public static void clearWindow(Window window) {
g.clearRect(0, 0, window.getWidth(), window.getHeight());
}
public static void drawRectangle(int x, int y, int width, int height) {
g.fillRect(x, y, width, height);
}
public static void setColor(Color color) {
g.setColor(color);
}
public static void drawImage(BufferedImage image, int x, int y, int width, int height) {
g.drawImage(image, x, y, win.getWidth() / width * Config.getField_of_view(), win.getHeight() / height * Config.getField_of_view(), null);
}
public static void drawSprite(Sprite sprite, int x, int y, int width, int height) {
g.drawImage(sprite.getImage(), x, y, (int) (win.getWidth() / width * Config.getField_of_view()), (int) (win.getHeight() / height * Config.getField_of_view()), null);
}
public static void closeRenderer() {
bs.show();
g.dispose();
}
我在渲染器中编写了这个奇怪的代码,因为无论 window 大小如何,我都希望有一定的视野。 这是我在Main-Class:
中写的
@Override
public void render() {
//Prepare
Renderer.prepareRenderer(Renderer.createBufferStrategy(window, Renderer.MAX_BUFFER_COUNT));
//Clear
Renderer.clearWindow(window);
//Draw
Renderer.drawSprite(sprite, 0, 0, 100, 100);
//Close
Renderer.closeRenderer();
}
但现在我遇到了一个问题,因为当我将 window 做成一个矩形时,它看起来很拉伸。
这与 Math.min(win.getWidth(), win.getHeight())
一起工作谢谢