Recent

Author Topic: Reuse a canvas to make a new drawing  (Read 4554 times)

Relativity

  • Full Member
  • ***
  • Posts: 103
Reuse a canvas to make a new drawing
« on: September 08, 2015, 04:12:44 pm »
I have a canvas, on which a drawing is made.
Upon a certain user-driven event, on the canvas a new drawing should be made, that would replace the old one.
But when the event occurs, the old drawing remains there and there is no trace of the new one.
On the other hand, if I minimize the window and then open it again, upon triggering the event the new drawing shows up.

It is clear that I must someway make some settings in the canvas, in order to be able to make the new drawing, but I don't know which ones.
Should I someway use the Refresh-method ? Or maybe destroy and create again the canvas ? Or maybe the AutoRedraw-method has something to do with it ? And what about the Erase-method ?
Thank you.

« Last Edit: September 08, 2015, 04:24:49 pm by Relativity »
"How'm I gonna get through?"
  -- Pet Shop Boys

derek.john.evans

  • Guest
Re: Reuse a canvas to make a new drawing
« Reply #1 on: September 08, 2015, 04:25:52 pm »
You should always use TPaintBox.Invalidate or TPaintBox.Repaint to trigger a OnPaint event. (Refresh is an alias for Repaint)

Paint the TPaintBox in OnPaint. You need to clear the canvas if you don't want to keep the old drawings. eg:

Code: [Select]
PaintBox1.Canvas.Brush.Color := clRed;
PaintBox1.Canvas.FillRect(PaintBox1.ClientRect);

** EDIT ** May bad. You dont have to clear a TPaintBox, but it is often better too.
« Last Edit: September 08, 2015, 04:42:23 pm by Geepster »

Relativity

  • Full Member
  • ***
  • Posts: 103
Re: Reuse a canvas to make a new drawing
« Reply #2 on: September 09, 2015, 11:35:23 am »
Applying your suggestions I effectively get the new drawing, but it is somewhat blurred. Most of all the outline of the rectangles that I draw are not sharp and straight, but are somewhat undulated and discontinuous.
"How'm I gonna get through?"
  -- Pet Shop Boys

derek.john.evans

  • Guest
Re: Reuse a canvas to make a new drawing
« Reply #3 on: September 09, 2015, 12:42:15 pm »
Applying your suggestions I effectively get the new drawing, but it is somewhat blurred. Most of all the outline of the rectangles that I draw are not sharp and straight, but are somewhat undulated and discontinuous.

That sounds weird. Maybe post a screen shot and details on what platform you are running.

Relativity

  • Full Member
  • ***
  • Posts: 103
Re: Reuse a canvas to make a new drawing
« Reply #4 on: September 09, 2015, 01:32:20 pm »
In the two files in attachment you can see the first picture (sharp and perfect) and the second (with blurred outlines). In the two pictures I have drawn exactly the same things, so one can better make comparisons.

You see, I am trying to make a coloured diagram, what in principle would be already available in Lazarus through TColorMapSeries: I didn't use TColorMapSeries because the colour of the rectangles can not be determined through a simple function, but through quite complicated code.

Clicking the button on the top, the following is executed:

procedure TForm1.B_zeigenClick(Sender: TObject);
begin
  DiagrammZeichnen(); // just prepares the data necessary to calculate the rectangles and their colour
  Form1.PaintBox1.Show; (* I need this because as default I keep the PaintBox not visible, otherwise it tries to paint the drawing before having loaded and prepared the data, as soon as I activate the form. Is maybe this the problem ? I have no idea. *)
  Form1.PaintBox1.Canvas.Clear;
  Form1.PaintBox1.Repaint; // This triggers the event that calls OnPaint, where I have written the code that draws the reactangles, the lattice etc...
end; 

This same procedure is called both to produce the first drawing and the second one.

I use a Laptop with Windows Vista Home Basic and Intel(R) Pentium(R) Dual CPU T3400 @ 2.16GHz and 3,00 GB RAM.


"How'm I gonna get through?"
  -- Pet Shop Boys

derek.john.evans

  • Guest
Re: Reuse a canvas to make a new drawing
« Reply #5 on: September 09, 2015, 01:47:34 pm »
Looks like you are changing TCanvas.Pen.Style and not restoring its value.

Its a problem with state based renders like TCanvas & OpenGL.

You can either restore Canvas.Pen.Style after using it for dashed lines, or make sure you set Canvas.Pen.Style :=  psSolid when you want a solid line.

I prefer restoring.

Relativity

  • Full Member
  • ***
  • Posts: 103
Re: Reuse a canvas to make a new drawing
« Reply #6 on: September 10, 2015, 12:18:23 pm »
I restored Canvas.Pen.Style to solid after using it for the dashed lines of the first drawing's lattice and now it works perfectly.
Geepster, this time you really bailed me out of trouble.
That I had to restore the style to solid after having drawn the lattice of the first drawing was trivial, but I'd have never caught on by myself. And no use searching for hours on the internet.
"How'm I gonna get through?"
  -- Pet Shop Boys

 

TinyPortal © 2005-2018