Java AWT 自定义 CompositeContext & Anti-aliasing: RasterFormatException 在客户端区域之外绘制时
我正在尝试为AWT Graphics2D实现类似SWT GC的异或模式绘图。使用内置的XORComposite不是一个选项,因为它不像在SWT中那样实现异或模式绘图。
SWT 异或模式绘图通过二进制独占或组合源颜色和目标颜色。AWT XORComposite(可通过)使用恒定的异或色,该异或色通过二进制独占或与源颜色组合,即目标颜色不影响结果颜色。g2d.setXORMode(Color)
因此,我唯一想到的选择是编写我自己的Composite和CompositeContext实现,这些实现适当地组合了源代码和目标。
经过一些阅读,我想出了这个简单的实现:(是的,我知道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)
值得注意的是,我的复合/复合文本不会引发异常,但是当尝试创建要传递给我的复合文本的栅格对象时,AWT 内部人员会引发异常。
遗憾的是,PixelToParallelogramConverter 仅在启用抗锯齿功能时用于自定义合成。例如,内置的 XORComposite 使用本机方法来绘制。我假设一个AWT错误,但我不确定。
如果能提供帮助,将不胜感激:)
更新:
正如Durandal所建议的,我用java-6-sun和java-1.6.0-openjdk测试了代码。我发现OpenJDK抛出异常,而Sun-JDK没有。因此,我在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));
}
}