gpt4 book ai didi

java - 创建 VolatileImage 总是返回 null

转载 作者:行者123 更新时间:2023-11-30 11:55:51 26 4
gpt4 key购买 nike

我在下面的类中创建了一个可变图像,但每次都会遇到 NullPointerException。 createVolatileImage() 返回 null(从其 JavaDoc)的原因是 GraphicsEnvironment.isHeadless() 为真。

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.VolatileImage;
import javax.swing.JPanel;

public class Tube extends JPanel {

private VolatileImage image;
private int width, height;

public Tube() {
super(true);
}

public Tube(int width, int height) {
this.width = width;
this.height = height;
createImage();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(image == null)
createImage();
if(image.validate(getGraphicsConfiguration()) == VolatileImage.IMAGE_INCOMPATIBLE)
createImage();
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
g2.drawImage(image, 0, 0, this);
}

public VolatileImage getBufferedImage() {
return this.image;
}

private void createImage(){
if(image != null){
image.flush();
image = null;
}
image = createVolatileImage(width, height);
image.setAccelerationPriority(1.0f);
}
}

我怎样才能返回一个非空的 VolatileImage ??

最佳答案

这将改变 headless 模式:

System.setProperty("java.awt.headless", "false");

关于java - 创建 VolatileImage 总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4793054/

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