Graphics2D:在白纸上绘制黑色? [英] Graphics2D: Drawing black on white?

查看:733
本文介绍了Graphics2D:在白纸上绘制黑色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这是一个非常愚蠢的问题,但我找不到答案,我对Java2D API没有经验。我试图创建一个图像并将其写入GIF或PNG,并且我希望它在白色背景上使用黑色笔。如果我没有设置任何颜色,我会变黑。
如果我使用setPaint()(用于后续的绘制操作),我将整个画布用该颜色重新绘制。以下示例将整个事件呈现为黑色。



示例在Scala中,但您明白了。

  val bi =新的BufferedImage(200,400,BufferedImage.TYPE_BYTE_BINARY)
val g = bi.createGraphics
g.setBackground(Color.WHITE)
g.setPaint(Color.BLACK)
g.draw(new Rectangle(10,10,30,20))


解决方案

setBackground 方法是/仅用于< a href =http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html#clearRect(int,%20int,%20int,%20int) =noreferrer>




在绘制前用背景色填充矩形:

int width = 200;
int height = 400;
BufferedImage image = new BufferedImage(width,height,
BufferedImage.TYPE_BYTE_BINARY);
Graphics g = image.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
g.setColor(Color.BLACK);
//准备好绘制


I'm sure this is a very stupid question but I can't find the answer, I'm not experienced with the Java2D API. I'm trying to create an image and write it to GIF or PNG, and I want it to use a black pen on a white background. If I don't set any colors, I get white on black. If I use setPaint() (intended for subsequent draw operations) I get the whole canvas repainted with that color. The following sample renders the whole thing black.

The sample is in Scala but you get the idea. Feel free to answer in Java!

  val bi = new BufferedImage(200, 400, BufferedImage.TYPE_BYTE_BINARY )
  val g = bi.createGraphics
  g.setBackground(Color.WHITE)
  g.setPaint(Color.BLACK)
  g.draw(new Rectangle(10, 10, 30, 20))

解决方案

The setBackground method is/was only for use with the clearRect method.

Fill the rectangle with the background colour before painting:

int width = 200;
int height = 400;
BufferedImage image = new BufferedImage(width, height,
                          BufferedImage.TYPE_BYTE_BINARY);
Graphics g = image.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
//ready for drawing

这篇关于Graphics2D:在白纸上绘制黑色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆