gpt4 book ai didi

java - 将java小程序的图形内容保存到图像文件无法正常工作

转载 作者:行者123 更新时间:2023-12-01 11:18:45 27 4
gpt4 key购买 nike

我试图将 Java 小程序的图形内容自动保存到图像文件,但遇到文件无法正确保存的问题。我完整的Java代码是:

package nl.mark.SierpinskiCarpet;

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SierpinskiCarpet extends Applet {
private Graphics g = null;
private int d0 = 729; // 3^6
private BufferedImage bufferedImage;
private final GraphicsConfiguration gConfig = GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
.getDefaultConfiguration();

public void init() {
g = getGraphics();
resize(d0, d0);
}

public void paint(Graphics g) {
drawSierpinskiCarpet(0, 0, getWidth(), getHeight());
storeImage();
}

public void storeImage() {
BufferedImage image = create(d0, d0, true);
Graphics2D g = image.createGraphics();
// you can disable this if you don't want smooth graphics
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
drawSierpinskiCarpet(0, 0, getWidth(), getHeight());
g.dispose();
try {
ImageIO.write(image, "png", new File(
"N:\\Tapijt van Sierpiński\\image.png"));
} catch (IOException e) {
}
}

private void drawSierpinskiCarpet(int xOL, int yOL, int breedte, int hoogte) {
if (breedte > 2 && hoogte > 2) {
int b = breedte / 3;
int h = hoogte / 3;
g.fillRect(xOL + b, yOL + h, b, h);
for (int k = 0; k < 9; k++)
if (k != 4) {
int i = k / 3;
int j = k % 3;
drawSierpinskiCarpet(xOL + i * b, yOL + j * h, b, h);
}
}
}

private BufferedImage create(final int width, final int height,
final boolean alpha) {
BufferedImage buffer = gConfig.createCompatibleImage(width, height,
alpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE);
return buffer;
}
}

小程序中显示的图像(Sierpinski Carpet)是正确的,但创建的文件只是空白图像。为了绘制图案,调用方法drawSierpinskiCarpet(),执行该函数后,调用函数storeImage()来保存图像,但会在输出目录中产生空白图像文件。保存图像时出了什么问题?

最佳答案

您应该永远在组件上调用getGraphics(并且永远存储作为给定的Graphics对象paint 的参数左右)。

(并且您永远不应该默默地吞下 IOException。至少添加一些 e.printStackTrace() 来知道何时出现问题)。

这里的问题是您从图像中获取了 Graphics 对象,但对 drawSierpinskiCarpet 的调用仍然使用了之前的 Graphics g存储为类中的字段。

在这种情况下,一种简单的解决方案是通过递归调用将 Graphics 对象作为参数传递:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SierpinskiCarpet extends Applet
{
private int d0 = 729; // 3^6
private BufferedImage bufferedImage;
private final GraphicsConfiguration gConfig = GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
.getDefaultConfiguration();

public void init()
{
resize(d0, d0);
}

public void paint(Graphics g)
{
drawSierpinskiCarpet(g, 0, 0, getWidth(), getHeight());
storeImage();
}

public void storeImage()
{
BufferedImage image = create(d0, d0, true);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
drawSierpinskiCarpet(g, 0, 0, getWidth(), getHeight());
g.dispose();
try
{
ImageIO.write(image, "png", new File(
"C:\\Users\\User\\Desktop\\sierpinskiImage.png"));
}
catch (IOException e)
{
e.printStackTrace();
}
}

private void drawSierpinskiCarpet(Graphics g, int xOL, int yOL,
int breedte, int hoogte)
{
if (breedte > 2 && hoogte > 2)
{
int b = breedte / 3;
int h = hoogte / 3;
g.fillRect(xOL + b, yOL + h, b, h);
for (int k = 0; k < 9; k++)
if (k != 4)
{
int i = k / 3;
int j = k % 3;
drawSierpinskiCarpet(g, xOL + i * b, yOL + j * h, b, h);
}
}
}

private BufferedImage create(final int width, final int height,
final boolean alpha)
{
BufferedImage buffer =
gConfig.createCompatibleImage(width, height, alpha
? Transparency.TRANSLUCENT : Transparency.OPAQUE);
return buffer;
}
}

关于java - 将java小程序的图形内容保存到图像文件无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31502470/

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