gpt4 book ai didi

java - 在 Swing 中将图像移动到背景图像上时如何最小化渲染

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

我正在渲染应保持不变的背景图像。在它的顶部,应该有一个移动的项目图像。

现在,我在每次调用绘制时渲染背景图像和项目图像。然而,有没有一种更智能、更高效的方式来渲染移动的项目,而无需重新渲染背景图像呢?

这就是我的类(class)的样子:

import javax.swing.*;
import java.awt.*;

public class Test extends Canvas {

private int x, y;
private Toolkit toolkit;

Test() {
toolkit = Toolkit.getDefaultToolkit();
}

void render() {

JFrame f = new JFrame();
f.add(this);
f.setSize(500, 500);
f.setVisible(true);
}

public void paint(Graphics g) {

// Render the background image
Image background = toolkit.getImage("background.jpg");
g.drawImage(background, 0, 0, this);

// Render the current position of the item
Image item = toolkit.getImage("item.png");
g.drawImage(item, x, y, this);
}

public void moveItem(int x, int y) {
this.x = x;
this.y = y;

validate();
repaint();
}
}

最佳答案

因此,您可以做很多小事情来加快渲染速度

  • 减少更新循环或绘制 channel 实际执行的工作量(例如不加载图像)
  • 减少在更新外观和绘制过程中创建的短期对象的数量(对于 example
  • 优化更新的剪切区域。

下面是一个简单的示例,它使用JComponent#paintImmediately来限制实际绘制的区域。在这种情况下,它将旧空间更新为玩家对象占据的新空间。这是相当简单的,更复杂的更新过程可能需要执行大量此类调用来处理消失的对象或具有大面积移动的对象。

Simple example

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Area;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}

public class TestPane extends JPanel {

private BufferedImage background;
private Player player;

public TestPane() throws IOException {
background = ImageIO.read(new File("/Users/shanew/Downloads/124178.jpg"));
player = new Player();

player.setX(-player.getWidth());
player.setY((getPreferredSize().height - player.getHeight()) / 2);

Timer timer = new Timer(5, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Area area = new Area(player.getBounds());
int x = player.getX();
x += 1;
if (x > getPreferredSize().width) {
x = -player.getWidth();
}
player.setX(x);
area.add(new Area(player.getBounds()));
paintImmediately(area.getBounds());
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked");
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Started");
timer.start();

}
});
}

@Override
public Dimension getPreferredSize() {
return new Dimension(711, 400);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g2d.translate(x, y);
g2d.drawImage(background, 0, 0, this);
player.paint(g2d, this);
g2d.dispose();
}

}

public class Player {

private BufferedImage image;

private int x, y;
private Rectangle bounds;

public Player() throws IOException {
image = ImageIO.read(new File("/Users/shanew/Downloads/e13333ab21b52d8.png"));
bounds = new Rectangle();
}

public int getWidth() {
return image.getWidth();
}

public int getHeight() {
return image.getHeight();
}

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public void paint(Graphics2D g2d, ImageObserver observer) {
g2d.drawImage(image, x, y, observer);
}

public Rectangle getBounds() {
bounds.x = getX();
bounds.y = getY();
bounds.width = getWidth();
bounds.height = getHeight();
return bounds;
}

}

}

另一个工作流程可能是使用单独的 BufferedImage 来存储动态内容。这样子系统的渲染速度会比尝试渲染大量小元素更快

关于java - 在 Swing 中将图像移动到背景图像上时如何最小化渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59557048/

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