我正在尝试为 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)
值得注意的是,我的 Composite/CompositeContext 不会引发异常,但是 AWT 内部在尝试创建它想要传递给我的 CompositeContext 的 Raster 对象时会引发异常。
不幸的是,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));
}
}