java – 使用TexturePaint的图标抛出InternalError:Surface不可缓存

前端之家收集整理的这篇文章主要介绍了java – 使用TexturePaint的图标抛出InternalError:Surface不可缓存前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个自定义图标,它使用TexturePaint作为绘图,并在Graphics2D对象上调用fillPolygon.

这段代码在JDK6和JDK7上运行良好,但在64位Linux的JDK8下失败(在64位Linux上尝试了最新的JDK1.8).在Windows上,这适用于JDK8.

用于重现问题的JDK的java -version命令的输出.

java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02,mixed mode)

重现问题的代码

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

public class TexturePaintIconTest {

  private static class CustomIcon implements Icon{

    private static final int[] sXArray = new int[]{2,5,7,12,4,1,2};
    private static final int[] sYArray = new int[]{4,2,11,8,4};
    private static final int sNumberOfPoints = 9;

    @Override
    public void paintIcon(Component c,Graphics g,int x,int y) {
      g.translate(x,y);

      Graphics2D g2d = (Graphics2D) g;
      Color fillColor = new Color( 140,25,35);
      g2d.setPaint(createPatternPaint(fillColor));
      g2d.fillPolygon(sXArray,sYArray,sNumberOfPoints);

      g.translate(-x,-y);
    }

    private Paint createPatternPaint(Color color ){
      int[] colors = new int[]{color.getRGB(),0};
      IndexColorModel colorModel = new IndexColorModel(1,colors,true,-1,DataBuffer.TYPE_BYTE);
      BufferedImage image = new BufferedImage(2,BufferedImage.TYPE_BYTE_INDEXED,colorModel);
      DataBufferByte dataBuffer = (DataBufferByte) image.getRaster().getDataBuffer();
      byte[] data = dataBuffer.getData();
      data[0] = 0;
      data[1] = 1;
      data[2] = 1;
      data[3] = 0;
      return new TexturePaint(image,new Rectangle(2,2));
    }

    @Override
    public int getIconWidth() {
      return 16;
    }

    @Override
    public int getIconHeight() {
      return 16;
    }
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        JFrame testFrame = new JFrame("TestFrame");

        JLabel label = new JLabel("Label with icon",new CustomIcon(),SwingConstants.LEFT);
        testFrame.add(label);

        testFrame.pack();
        testFrame.setDefaultCloSEOperation(WindowConstants.EXIT_ON_CLOSE);
        testFrame.setVisible(true);
      }
    });
  }

}

运行main方法会导致以下错误

Exception in thread "AWT-EventQueue-0" java.lang.InternalError: Surface not cachable
    at sun.java2d.xr.XRPaints$XRTexture.getAccSrcSurface(XRPaints.java:211)
    at sun.java2d.xr.XRPaints$XRTexture.isPaintValid(XRPaints.java:224)
    at sun.java2d.xr.XRPaints.isValid(XRPaints.java:75)
    at sun.java2d.xr.XRSurfaceData.getMaskFill(XRSurfaceData.java:205)
    at sun.java2d.SurfaceData.validatePipe(SurfaceData.java:675)
    at sun.java2d.xr.XRSurfaceData.validatePipe(XRSurfaceData.java:123)
    at sun.java2d.SunGraphics2D.validatePipe(SunGraphics2D.java:446)
    at sun.java2d.pipe.ValidatePipe.validate(ValidatePipe.java:55)
    at sun.java2d.pipe.ValidatePipe.fillPolygon(ValidatePipe.java:147)
    at sun.java2d.SunGraphics2D.fillPolygon(SunGraphics2D.java:2389)
    at com.luciad.internal.lucy.map.TexturePaintIconTest$CustomIcon.paintIcon(TexturePaintIconTest.java:22)

我已经在Java bug数据库(official oneopenjdk one)中搜索过,但没有找到可以解释这个问题的错误报告.我的自定义图标出错了,还是偶然发现了JDK错误

解决方法

如果相同的代码工作方式不同,具体取决于JVM必须是一个错误,因为它是代码中唯一特定于操作系统的部分

猜你在找的Java相关文章