6

I am using java.awt.geom.Rectangle2D.Double class to generate a rectangle. I want to generate a rectangle which is filled with a color (say green) and have a border (outline).

Now the problem is if I call

g2.draw(new Rectangle2D.Double(....)); // g2 is an instance of Graphics2D

then it doesn't fill the rectangle and when I call

g2.fill(new Rectangle2D.Double(....)); // g2 is an instance of Graphics2D

then id doesn't generate border.

Amit
  • 33,847
  • 91
  • 226
  • 299
  • Dan and Samuel are both right. It's logical too. `fill` fills the entire rectangular area, including the area occupied by the border you just drew. Time to pull your foot out of the line of fire :) – Carl Smotricz Jan 08 '10 at 12:22

2 Answers2

11

To do this, render the rectangle twice, first the fill and then the border (draw).

Rectangle2D rect = new Rectangle2D.Double(...);
g2.setColor(Color.white);
g2.fill(rect);
g2.setColor(Color.black);
g2.draw(rect);
Samuel Sjöberg
  • 719
  • 5
  • 12
5

How about doing both? Draw the filled rectangle first and then draw the outline one over the top.

Dan Dyer
  • 53,737
  • 19
  • 129
  • 165