gpt4 book ai didi

Java AWT 自定义 CompositeContext 和抗锯齿 : RasterFormatException when drawing outside of the client area

转载 作者:太空狗 更新时间:2023-10-29 22:58:37 32 4
gpt4 key购买 nike

我正在尝试实现 SWT GC类似 AWT 的异或模式绘图 Graphics2D .使用内置 XORComposite不是一个选项,因为它不像在 SWT 中那样实现异或模式绘图。

SWT 异或模式绘图通过二进制异或组合源颜色和目标颜色。 AWT XORComposite (可通过 g2d.setXORMode(Color) 使用)使用常量 xor-color,它通过二进制异或与源颜色组合,即目标颜色不影响结果颜色。

所以我想到的唯一选择是自己编写 CompositeCompositeContext适当结合源和目标的实现。

经过一些阅读,我想到了这个简单的实现:(是的,我知道 getPixel(...)、setPixel(...) 开销。我希望它在优化之前正常工作。)

private static class XorComposite implements Composite {

public static XorComposite INSTANCE = new XorComposite();

private XorContext context = new XorContext();

@Override
public CompositeContext createContext(ColorModel srcColorModel,
ColorModel dstColorModel, RenderingHints hints) {
return context;
}

}

private static class XorContext implements CompositeContext {

public XorContext() {
}

@Override
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
int w = Math.min(src.getWidth(), dstIn.getWidth());
int h = Math.min(src.getHeight(), dstIn.getHeight());

int[] srcRgba = new int[4];
int[] dstRgba = new int[4];

for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
src.getPixel(x, y, srcRgba);
dstIn.getPixel(x, y, dstRgba);
for (int i = 0; i < 3; i++) {
dstRgba[i] ^= srcRgba[i];
}
dstOut.setPixel(x, y, dstRgba);
}
}
}

@Override
public void dispose() {
}

}

此实现在禁用抗锯齿时工作正常。如果启用了抗锯齿,只要我的绘图完全可见,即在我绘制的 JPanel 内部,它就可以工作。但是,如果绘图越过 JPanel 的边界,则会抛出 RasterFormatException:

Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (y + height) is outside raster
at sun.awt.image.IntegerInterleavedRaster.createWritableChild(IntegerInterleavedRaster.java:470)
at sun.awt.image.IntegerInterleavedRaster.createChild(IntegerInterleavedRaster.java:514)
at sun.java2d.pipe.GeneralCompositePipe.renderPathTile(GeneralCompositePipe.java:106)
at sun.java2d.pipe.AAShapePipe.renderTiles(AAShapePipe.java:201)
at sun.java2d.pipe.AAShapePipe.fillParallelogram(AAShapePipe.java:102)
at sun.java2d.pipe.PixelToParallelogramConverter.fillRectangle(PixelToParallelogramConverter.java:322)
at sun.java2d.pipe.PixelToParallelogramConverter.fill(PixelToParallelogramConverter.java:159)
at sun.java2d.pipe.ValidatePipe.fill(ValidatePipe.java:160)
at sun.java2d.SunGraphics2D.fill(SunGraphics2D.java:2422)
at org.eclipse.gef4.graphics.examples.AwtXorTestPanel.paint(AwtXorTest.java:117)
... (irrelevant)

值得注意的是,我的 Composite/CompositeContext 没有抛出异常,但 AWT 内部在尝试创建要传递给我的 CompositeContext 的 Raster 对象时抛出异常。

不幸的是,PixelToParallelogramConverter 仅在启用抗锯齿时用于自定义合成。例如,内置的 XORComposite 使用 native 方法进行绘制。我假设存在 AWT 错误,但我不确定。

帮助将不胜感激:)

更新:

正如 Durandal 所建议的,我使用 java-6-sun 和 java-1.6.0-openjdk 测试了代码。我发现 OpenJDK 会抛出异常,而 Sun-JDK 不会。因此,我报告了 bug在 OpenJDK 错误跟踪器上。

问题解决后我会更新这个问题。请访问相应的 OpenJDK 错误以获取有关当前进度的信息。

问候,马蒂亚斯

这是一个示例程序,因此您可以在本地进行测试:

/*******************************************************************************
* Copyright (c) 2013 itemis AG and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Matthias Wienand (itemis AG) - initial API and implementation
*
*******************************************************************************/
package org.eclipse.gef4.graphics.examples;

import java.awt.Color;

public class AwtXorTest extends JApplet {

private static final long serialVersionUID = 1L;

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("AWT XorMode Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new AwtXorTest();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
}

@Override
public void init() {
JPanel panel = new AwtXorTestPanel();
getContentPane().add(panel);
}

}

class AwtXorTestPanel extends JPanel {

private static class XorComposite implements Composite {

public static XorComposite INSTANCE = new XorComposite();

private XorContext context = new XorContext();

@Override
public CompositeContext createContext(ColorModel srcColorModel,
ColorModel dstColorModel, RenderingHints hints) {
return context;
}

}

private static class XorContext implements CompositeContext {

public XorContext() {
}

@Override
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
int w = Math.min(src.getWidth(), dstIn.getWidth());
int h = Math.min(src.getHeight(), dstIn.getHeight());

int[] srcRgba = new int[4];
int[] dstRgba = new int[4];

for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
src.getPixel(x, y, srcRgba);
dstIn.getPixel(x, y, dstRgba);
for (int i = 0; i < 3; i++) {
dstRgba[i] ^= srcRgba[i];
}
dstOut.setPixel(x, y, dstRgba);
}
}
}

@Override
public void dispose() {
}

}

private static final long serialVersionUID = 1L;

public AwtXorTestPanel() {
setPreferredSize(new Dimension(640, 480));
}

@Override
public void paint(Graphics graphics) {
Graphics2D g2d = (Graphics2D) graphics;

// comment out to see it working:
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

g2d.setComposite(XorComposite.INSTANCE);
g2d.setColor(new Color(0, 255, 255)); // resulting color should be red
g2d.fill(new Rectangle(100, 100, 500, 500));
}

}

最佳答案

警告:我已经有一段时间没有接触栅格了。

看起来您可能正在访问栅格外的像素。

光栅有一个 minXminY,所以你的循环需要像这样:

int srcMinX = src.getMinX();
int srcMinY = src.getMinY();
int dstInMinX = dstIn.getMinX();
int dstInMinY = dstIn.getMinY();
int dstOutMinX = dstOut.getMinX();
int dstOutMinY = dstOut.getMinY();

for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
src.getPixel(x+srcMinX, y+srcMinY, srcRgba);
dstIn.getPixel(x+dstInMinX, y+dstInMinY, dstRgba);
for (int i = 0; i < 3; i++) {
dstRgba[i] ^= srcRgba[i];
}
dstOut.setPixel(x+dstOutMinX, y+dstOutMinY, dstRgba);
}
}

关于Java AWT 自定义 CompositeContext 和抗锯齿 : RasterFormatException when drawing outside of the client area,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14625833/

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