Graphics2D: Drawing black on white?

拟墨画扇 提交于 2019-12-21 07:07:04

问题


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))

回答1:


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


来源:https://stackoverflow.com/questions/575706/graphics2d-drawing-black-on-white

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!