【发布时间】:2014-07-27 23:18:00
【问题描述】:
我目前正在尝试缩小背景图像以适应不同的窗口大小。我正在使用的图像是 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
);
}
}
}
【问题讨论】:
-
Background是自定义类吗? -
是的,对不起!我添加了背景类的编辑。
标签: java android background scale