0

I'd like to draw a shape on a Graphics2D and colour a part of it different if it intersects a certain range. I managed to do this by drawing 2 shapes into a BufferedImage (the original shape and the area that will cause a change of colour) by using AlphaComposite and AlphaComposite.SRC_IN. The problem is that if the canvas is scrolled, the screen becomes laggy as every time the paint method is called, a buffered image is created.

Is there any way of achieving the same effect (2 shapes overlapping, only 2nd shape's overlap area is coloured) without using a buffered image object?

Cheers, Max

Max
  • 79
  • 4

1 Answers1

1

I'm not sure I undertand fully what you're trying to do, but you shouldn't create the buffered image each time the paint method is called; only when something is changed in the objects to display.

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
  • Ok, let me clarify this a bit more. The shape I'd like to display is a graph. The graph is filled blue. Whenever the graph is above a certain value, it should be coloured red. I'd like to draw the graph first and overlay it with a rectangle that represents the area in which the graph's fill area turns red. The paint method is called every time the scrollbar is moved. – Max Jul 21 '11 at 07:00
  • I fail to see how any of what you said in your comment, prevents you from implementing Maurice's sage advice, to the effect.. "Cache the `BufferedImage` instance(s). Change them when needed." – Andrew Thompson Jul 21 '11 at 07:03
  • Why are you not drawing a rectangle instead of creating a bufferedimage? – Maurice Perry Jul 21 '11 at 07:03
  • That's what I'm trying to do. The Alphablending doesn't seem to work if you directly draw onto the canvas... – Max Jul 21 '11 at 07:06
  • Use a red with an opacity of 50% setColor(new Color(255,0,0,128)); – Maurice Perry Jul 21 '11 at 07:07
  • @Andrew: Unfortunately, I can't cache the BufferedImage as the object handling the drawing is used to draw other graphs as well. – Max Jul 21 '11 at 07:08
  • @Maurice: This doesn't help. The red rectangle still overwrites the blue graph completely and is also displayed outside of the graph's area. – Max Jul 21 '11 at 07:13