gpt4 book ai didi

java - 输出图像加速

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

您好)有一个问题是否可以加快代码速度,如下所示:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

public class Game extends Canvas {

private static final long serialVersionUID = 1L;

private static final int WIDTH = 400;
private static final int HEIGHT = 400;

@Override
public void paint(Graphics g) {

super.paint(g);

int w = 20;
int h = 20;
int type = BufferedImage.TYPE_INT_ARGB;

BufferedImage image = new BufferedImage(w, h, type);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, w, h);
g2d.dispose();
int MapWidth = image.getWidth(this);
int MapHeight = image.getHeight(this);

for (int s = MapWidth - MapWidth; s < MapWidth * 10; s++) {
for (int i = MapHeight - MapHeight; i < MapHeight * 10; i++) {
g.drawImage(image, s, i, (int) w, (int) h, this);
}
}

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();

frame.setSize(WIDTH, HEIGHT);
frame.add(new Game());

frame.setVisible(true);
}
});
}
}

他画画了这么久。怎样才能加快速度呢?请帮忙)

并且一个程序在绘制图像之前无法关闭。有必要加快他们的绘制速度

最佳答案

它很慢,因为在你的

    for (int s = 0; s < MapWidth * 10; s++) {
for (int i = 0; i < MapHeight * 10; i++) {
g.drawImage(image, s, i, (int) w, (int) h, this);
}
}

对于 400*400 MapWidth、MapHeight,您正在绘制 4000*4000 个对象,因此您绘制了 16 000 000 个对象,它应该很慢。

如果你用这个替换它,它会很快:) :

    for (int s = 0; s < MapWidth * 10; s += w) {
for (int i = 0; i < MapHeight * 10; i += h) {
g.drawImage(image, s, i, (int) w, (int) h, this);
}
}

关于java - 输出图像加速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19295210/

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