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

时间:2014-07-27 23:18:00

标签: java android background scale

我正在尝试缩小背景图像以适应不同的窗口大小。我正在使用的图像是1080x1920,我试图让它缩小到下面代码中540x960的窗口。我没有运气。我正在使用下面的bg.draw(g)来绘制背景,并且在过去,能够写出“(g,0,0,getWidth(),getHeight(),null);”或类似的东西让它缩放,但它似乎没有用这种方法。

任何帮助都会很棒!

GameState类:

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

}

1 个答案:

答案 0 :(得分:0)

以下是调整BufferedImage

大小的方法

您可以将此resize()方法添加到Background类,其中变量image是您的Background类的实例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;
}

然后,您可以在bg.resize(int x, int y)

中将此调整大小方法称为MenuState