- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试实现 SWT GC类似 AWT 的异或模式绘图 Graphics2D .使用内置 XORComposite不是一个选项,因为它不像在 SWT 中那样实现异或模式绘图。
SWT 异或模式绘图通过二进制异或组合源颜色和目标颜色。 AWT XORComposite (可通过 g2d.setXORMode(Color)
使用)使用常量 xor-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 使用 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));
}
}
最佳答案
警告:我已经有一段时间没有接触栅格了。
看起来您可能正在访问栅格外的像素。
光栅有一个 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);
}
}
关于Java AWT 自定义 CompositeContext 和抗锯齿 : RasterFormatException when drawing outside of the client area,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14625833/
我遇到了一个错误,我不知道如何解决。我有以下代码(来自 Eliom Graffiti 教程),我正在尝试使用 make test.byte 进行测试。 open Eliom_content.Html5
我阅读文档的理解是这样的: 客户端是测试用例的子类。当我们运行 manage.py test 时,会为每个以“test_”开头的方法创建一个 SimpleTest 类的实例(它继承自 TestCase
我已经编写了一个用于接收多个客户端的服务器,它可以分别与客户端通信。在这里,我可以列出服务器中已连接的客户端,但是当客户端断开连接时,它不会从服务器中删除客户端。 Server.py import s
我正在制作一个社交网站。当任何用户在站点上更新或创建新内容时,我需要查看站点的任何其他用户来查看更改更新。 我有一些需要低延迟的评论,因此建议为此订阅。 我也有事件,但这些不需要这么低的延迟。每 10
我想在突变后使用乐观 UI 更新:https://www.apollographql.com/docs/react/basics/mutations.html 我对“乐观响应”和“更新”之间的关系感到
我想了解 Dask 在本地机器上的使用模式。 具体而言, 我有一个适合内存的数据集 我想做一些 pandas 操作 分组依据... 日期解析 等等 Pandas 通过单核执行这些操作,这些操作对我来说
我使用 Apollo、React 和 Graphcool。我有一个查询来获取登录的用户 ID: const LoginServerQuery = gql` query LoginServerQ
在本指南的帮助下,我最近在几个设备的应用程序中设置了 P2P 通信:http://developer.android.com/training/connect-devices-wirelessly/n
注意:我在节点项目中使用@twilio/conversations 1.1.0 版。我正在从使用可编程聊天过渡到对话。 我看到对 Client.getConversationByUniqueName
我对服务客户端和设备客户端库有点困惑。谁能解答我对此的疑问。 问题:当我通过 deviceClient 发送数据时,我无法接收数据,但当我使用服务客户端发送数据时,相同的代码可以工作。现在,xamar
我对服务客户端和设备客户端库有点困惑。谁能解答我对此的疑问。 问题:当我通过 deviceClient 发送数据时,我无法接收数据,但当我使用服务客户端发送数据时,相同的代码可以工作。现在,xamar
假设我有一个简单的应用程序。 如何设置 OAuth2 以允许其他应用程序访问我的应用程序的某些部分。 例如,当开发人员想要使用 Facebook API 时,他们会使用 Facebook API 用户
我有两个模块: 在一个模块中,我从另一个模块run 中引用了一个函数: @myorg/server import { Client } from '.' import { Middleware } f
我在通过服务器从客户端向客户端发送数据时遇到了一些问题(以避免监听客户端上的端口)。 我有一个这样的服务器: var net = require("net"); var server = net.cr
我正在使用 django.test.client.Client 来测试用户登录时是否显示某些文本。但是,我的 Client 对象似乎并没有让我保持登录状态。 如果使用 Firefox 手动完成,则此测
有两个我制作的程序无法运行。有服务器和客户端。服务器通过给用户一个 ID(从 0 开始)来接受许多客户端。服务器根据服务器的 ID 将命令发送到特定的客户端。 (示例:200 个客户端连接到 1 个服
今天,我在 Windows 10 的“程序和功能”列表中看到了 2 个不同版本的 ARC,因此我选择卸载旧版本,因为我需要一些空间。在卸载结束时,它们都消失了! 所以,我从 https://insta
在每个新的客户端连接上 fork 服务器进程 不同的进程(服务器的其他子进程,即 exec)无法识别在 fork 子进程中使用相同 fd 的客户端。 如何在其他进程上区分客户端? 如果文件描述符为新
a和b有什么区别? >>> import boto3 >>> a = boto3.Session().client("s3") >>> b = boto3.client("s3") >>> a ==
a和b有什么区别? >>> import boto3 >>> a = boto3.Session().client("s3") >>> b = boto3.client("s3") >>> a ==
我是一名优秀的程序员,十分优秀!