Java 将背景图像缩放到不同的窗口大小?[爪哇]

Java 将背景图像缩放到不同的窗口大小?[爪哇],java,android,background,scale,Java,Android,Background,Scale,我目前正在尝试缩小背景图像的比例,以适应不同的窗口大小。我正在使用的图像是1080x1920,我正在尝试将其缩小到下面代码中540x960的窗口。我一点运气都没有。我在下面使用bg.drawg来绘制背景,在过去,我能够写g,0,0,getWidth,getHeight,null;或者类似于规模化的东西,但这种方法似乎不起作用 任何帮助都会很棒 游戏状态类: package GameState; import SpriteSheet.Background; import java.awt.*;

我目前正在尝试缩小背景图像的比例,以适应不同的窗口大小。我正在使用的图像是1080x1920,我正在尝试将其缩小到下面代码中540x960的窗口。我一点运气都没有。我在下面使用bg.drawg来绘制背景,在过去,我能够写g,0,0,getWidth,getHeight,null;或者类似于规模化的东西,但这种方法似乎不起作用

任何帮助都会很棒

游戏状态类:

package GameState;

import SpriteSheet.Background;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

public class MenuState extends GameState {

private Background bg;

private int currentChoice = 0;
private String[] options = {
    "Start",
    "Help",
    "Quit"
};

private Color titleColor;
private Font titleFont;

private Font font;

public MenuState(GameStateManager gsm) {

    this.gsm = gsm;

    try {

        bg = new Background("/Images/LivingRoom.png", 0.5);

        titleColor = new Color(128, 0, 0);
        titleFont = new Font(
                "Century Gothic",
                Font.PLAIN,
                28);

        font = new Font("Arial", Font.PLAIN, 12);

    }
    catch(Exception e) {
        e.printStackTrace();
    }

}

public void init() {}

public void update() {
    bg.update();
}

public void draw(Graphics2D g) {

    // draw bg
    bg.draw(g);
    bg.resize(540, 960);

    // draw title
    g.setColor(titleColor);
    g.setFont(titleFont);
    g.drawString("Title", 80, 70);

    // draw menu options
    g.setFont(font);
    for(int i = 0; i < options.length; i++) {
        if(i == currentChoice) {
            g.setColor(Color.BLACK);
        }else {
            g.setColor(Color.RED);
        }
        g.drawString(options[i], 145, 140 + i * 15);
    }

}

public void resize(){

    bg.resize(540, 960);

}

private void select() {
    if(currentChoice == 0) {
        // start
    }
    if(currentChoice == 1) {
        // help
    }
    if(currentChoice == 2) {
        System.exit(0);
    }
}

public void keyPressed(int k) {
    if(k == KeyEvent.VK_ENTER){
        select();
    }
    if(k == KeyEvent.VK_UP) {
        currentChoice--;
        if(currentChoice == -1) {
            currentChoice = options.length - 1;
        }
    }
    if(k == KeyEvent.VK_DOWN) {
        currentChoice++;
        if(currentChoice == options.length) {
            currentChoice = 0;
        }
    }
}
public void keyReleased(int k) {}

public void draw() {}

public void mousePressed(MouseEvent e) {}

public void mouseClicked() {}

}
背景类:

package SpriteSheet;

import Main.GamePanel;

import java.awt.*;
import java.awt.image.*;

import javax.imageio.ImageIO;

public class Background {

private static BufferedImage image;

private double x;
private double y;
private double dx;
private double dy;

private double moveScale;

public Background(String s, double ms) {

    try {
        image = ImageIO.read(
            getClass().getResourceAsStream(s)
        );
        moveScale = ms;
    }
    catch(Exception e) {
        e.printStackTrace();
    }

}

public void setPosition(double x, double y) {
    this.x = (x * moveScale) % GamePanel.WIDTH;
    this.y = (y * moveScale) % GamePanel.HEIGHT;
}

public void setVector(double dx, double dy) {
    this.dx = dx;
    this.dy = dy;
}

public void update() {
    x += dx;
    y += dy;
}

public void resize(int newWidth, int newHeight){
    BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, image.getType());
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(image, 0, 0, newWidth, newHeight, null);
    g.dispose();

    BufferedImage resizeImage = null;
    image = resizeImage;
}

public void draw(Graphics2D g) {

    g.drawImage(image, (int)x, (int)y, null);

    if(x < 0) {
        g.drawImage(
            image,
            (int)x + GamePanel.WIDTH,
            (int)y,
            null
        );
    }
    if(x > 0) {
        g.drawImage(
            image,
            (int)x - GamePanel.WIDTH,
            (int)y,
            null
        );
    }
}

}

下面是一种调整BuffereImage大小的方法

您可以将其作为调整大小方法添加到后台类,其中变量image是后台类的实例image变量:

private void resize(int newWidth, int newHeight){
    BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, image.getType());
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(image, 0, 0, newWidth, newHeight, null);
    g.dispose();

    image = resizeImage;
}

然后您可以在MenuState类中将此resize方法称为bg.resizeint x,int y

Background是自定义类吗?很抱歉!我在background类中添加了一个edit。