gpt4 book ai didi

Java 全屏图像调整大小?

转载 作者:行者123 更新时间:2023-12-02 05:09:44 24 4
gpt4 key购买 nike

你好,我是java新手。我正在开发一款游戏。当游戏开始时,会出现一个加载屏幕,然后逐渐消失,到目前为止就是这样。我的问题很简单。有没有办法更改图像尺寸以适应计算机屏幕的尺寸。我希望它适合所有类型的屏幕尺寸,并且是原始图像的更大比例。到目前为止,这是我的主类(还有其他类,如玩家、标题和游戏,但它们与问题无关:

package Main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Comp extends Canvas implements Runnable, KeyListener {

private static final long serialVersionUID = 1L;
public boolean run = true;
public Image screen, img;
public static double dir = 5;
public static int pixelSize = 1;
public static Dimension size = new Dimension(680, 500);
public static Dimension pixel = new Dimension(size.width / pixelSize,
size.height / pixelSize);
public static int mx;
public static manager m;
public static int my;
public static int fps;
public static boolean mr;
public static boolean ml;

JFrame frame = new JFrame();
public Thread t;
public boolean splashscreen = true;
public int time = 0;
public int timer = 140;
public int btime = 0;
public int btimer = 1;
private int FPS = 5;
private long targetTime = 1000 / FPS;

public Comp() {
setPreferredSize(size);
setFocusable(true);
addKeyListener(this);
addKeyListener(new listen());
addMouseListener(new listen());
addMouseMotionListener(new listen());
addMouseWheelListener(new listen());
m = new manager();
requestFocus();
// h = new InputHandler(this);
}

public void start() {
run = true;
frame.add(this);
frame.pack();
frame.requestFocus();
frame.setTitle("In The Maze");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setIconImage(new ImageIcon("res/traps/beartrap1.png").getImage());
t = new Thread(this);
t.start();
}

public void stop() {
run = false;
}

public static void main(String[] args) {
Comp c = new Comp();
c.start();
}

public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 1000, 1000);
if (!splashscreen) {
m.render(g);
}
if(splashscreen){
ImageIcon i62 = new ImageIcon("res/splashscreen.png");
img = i62.getImage();
g.drawImage(img,(int)0 ,(int)0,Comp.size.width+10,Comp.size.height + 10,null);
time++;
}
g.dispose();
bs.show();
}

public void tick() {
if(time >= timer){
time = 0;
splashscreen = false;
}

if(!splashscreen){
m.tick();
}
Keys.update();
Esentials.tick();
}

public void run() {
screen = createVolatileImage(pixel.width, pixel.height);
long start;
long elapsed;
long wait;
long currentTime = System.currentTimeMillis();
while (run) {
start = System.nanoTime();
fps++;
if(System.currentTimeMillis() - currentTime >= 1000){
// System.out.println("fps:" + fps);
currentTime = System.currentTimeMillis();
fps = 0;
}
tick();
render();
elapsed = System.nanoTime() -start;
wait = targetTime = elapsed / 1000000;
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void keyTyped(KeyEvent i) {
}

public void keyPressed(KeyEvent i) {
Keys.keySet(i.getKeyCode(), true);
}

public void keyReleased(KeyEvent i) {
Keys.keySet(i.getKeyCode(), false);
}
}

最佳答案

尝试使用Toolkit.getScreenSize()

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();

要调整图像大小,您可以使用简单的imgscalr

BufferedImage scaledImage = Scalr.resize(myImage, 100);

关于Java 全屏图像调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27431036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com