我正在尝试实施SWT GC - 就像AWT Graphics2D的xor-mode绘图一样。使用内置XORComposite不是一个选项,因为它没有像在SWT中那样实现xor-mode绘图。
SWT xor-mode绘图通过二进制异或来组合源颜色和目标颜色。 AWT XORComposite(可通过g2d.setXORMode(Color)
使用)使用常量xor颜色,通过二进制异或与源颜色组合,或者目标颜色不会影响结果颜色。
因此,我唯一想到的选择就是编写自己的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没有抛出异常,但是在尝试创建要传递给CompositeContext的Raster对象时,AWT内部会抛出异常。
不幸的是,PixelToParallelogramConverter仅在启用消除锯齿时用于自定义Composites。例如,内置的XORComposite使用本机方法绘制。我假设一个AWT错误,但我不确定。
非常感谢帮助:)
更新
正如Durandal建议的那样,我用java-6-sun和java-1.6.0-openjdk测试了代码。我发现OpenJDK抛出了异常而Sun-JDK没有抛出异常。因此,我在OpenJDK错误跟踪器上报告了bug。
我会在问题解决后更新此问题。有关当前进度的信息,请访问相应的OpenJDK错误。
的问候, 的Matthias
这是一个示例程序,因此您可以在本地测试它:
/*******************************************************************************
* 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));
}
}
答案 0 :(得分:1)
看起来您可能正在访问栅格外的像素。
Rasters有一个minX
,minY
,所以你的循环必须是这样的:
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);
}
}
答案 1 :(得分:0)
g2d剪辑区域有多个框,并且会抛出RasterFormatException,因为它的全尺寸光栅参考会被抗锯齿框的光栅替换。