java - Java中除空格外的屏幕填充

标签 java graphics graphics2d

我正在尝试使用 Java 中的 Graphics2D 对象渲染 war 迷雾。目前,我正在这样渲染。

// Color the screen black
Color color = new Color(0, 0, 0, 200);
graphics.setColor(color);
graphics.fillRect(0, 0, entity.getWorld().getGame().getWidth(), entity.getWorld().getGame().getHeight());
// Make a rectangle around the Entity that is normal
color = new Color(0, 0, 0, 0);
graphics.setColor(color);
PositionComponent2D data = (PositionComponent2D) entity.getComponent("pos");
Vector2D pos = data.getAABB().getPosition();
graphics.fillRect((int) pos.getX() - 20, (int) pos.getY() - 20, 40, 40);

它给出了我正在寻找的效果,除了代码的第二部分不起作用。有没有什么方法可以渲染屏幕上除了玩家附近以外的任何地方,以产生 war 迷雾效果?

最佳答案

您需要使用 composite对于您的第二次绘图调用...类似这样的内容(免责声明:我没有尝试运行此代码,但这应该是一个起点):

// Color the screen black
Color color = new Color(0, 0, 0, 200);
graphics.setColor(color);
graphics.fillRect(0, 0, entity.getWorld().getGame().getWidth(), entity.getWorld().getGame().getHeight());
// Make a rectangle around the Entity that is normal
Composite oldComposite = graphics.getComposite();
graphics.setComposite(AlphaComposite.CLEAR);
PositionComponent2D data = (PositionComponent2D) entity.getComponent("pos");
Vector2D pos = data.getAABB().getPosition();
graphics.fillRect((int) pos.getX() - 20, (int) pos.getY() - 20, 40, 40);
graphics.setComposite(oldComposite);

关于java - Java中除空格外的屏幕填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25171382/

相关文章:

java - 多维数组左移

c# - 绘图图形性能问题

.net - Graphics.DrawPolygon 绘制六边形?

c# - 检查一个点是否在旋转的矩形中(C#)

java - Graphics2D.drawArc 中的高度参数

java - 使用构造函数的 Graphics2D

java - 如何将 odbc 数据库连接到我的 java 代码?

java - Spring框架中子文档数组字段中的过滤数组

java - Hamcrest - 测试具有相同属性值的复杂对象的优雅方式

Java 如何使用 Graphics2D 制作抗锯齿线?