如何渲染具有特定视野的图像?

问题描述 投票:1回答:1

晚上好!我已经使用Java和graphics对象成功显示了图像。这是我的“渲染器”类:

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();
    }

我在渲染器中编写了这个奇怪的代码,因为无论窗口大小如何,我都希望拥有一定的视野。这是我在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();
    }

但是现在我遇到了一个问题,因为当我将窗口变成矩形时,它看起来很拉伸。

java graphics bufferedimage
1个回答
0
投票

此工作与Math.min(win.getWidth(), win.getHeight())谢谢

© www.soinside.com 2019 - 2024. All rights reserved.