Numbered Line Drawer: Persistent and Number alignment problems. lazarus / 39-Graphics ---------------------------------------------------------------------- Boleeman · 2024-04-27 08:18 I have been working on a number line drawer that draws horizontal and vertical number lines. One can change the thickness/width and length of lines and increment amount and the number of intervals. I have a bit of a problem when more than one line is drawn (horizontal line drawn and then a vertical line is drawn) and the form is minimized, with only the last line remaining. Wanted all of the lines drawn to the TPaintbox to be persistent. Also have a bit of a problem adequately aligning the numbers when the font size is increased or the number of digits is increased from 1 to 2 digits. Not sure if anyone can help out? Would also like to work out how to make the scale lines vector, so that that can be moved and resized? ---------------------------------------------------------------------- KodeZwerg · 2024-04-27 08:59 Quote from: Boleeman on April 27, 2024, 10:18:48 amI have a bit of a problem when more than one line is drawn (horizontal line drawn and then a vertical line is drawn) and the form is minimized, with only the last line remaining. Wanted all of the lines drawn to the TPaintbox to be persistent.I have not downloaded or watched your source, I just give the usual advise: Create a Bitmap, draw on it, assign it to PaintBox and maybe use some of its events to refresh the Bitmap to to PaintBox. ---------------------------------------------------------------------- wp · 2024-04-27 10:18 Looking at your program I see - nothing, a white paintbox... Stepping into your SplitLine procedure the debugger shows that, in the first drawing operation, the points (X1,Y1) and (X2,Y2) are both zero. A bit later, in "if OptDirection.ItemIndex = 0" this leads to LineDistance = 0, and thus the variables segment_x and segment_y are not initialized since this happens only "if LineDistance <> 0". The lines in the "for i := 0 to NumSections" loop are based on these variables -- they are drawn somewhere in the universe but not on my screen... It does not make sense to further dig into the program. Please first fix the logic of your code and submit a working version. The most straightforward way to draw aligned text is to measure the width of the text and begin drawing (that's the argument in the Canvas.TextOut method) at a point shifted by the text width to the left (for right-aligned) or by half of the text (for centered text). Try to avoid hard-coded correction numbers, they will be wrong when the font changes. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.PaintBox1Paint(Sender: TObject);const  CROSS_LEN = 10;  DISTANCE = 10;var  txt: String;  Center: TPoint;  ext: TSize;begin  txt := IntToStr(SpinEdit1.Value);  Center := Point(Paintbox1.Width div 2, Paintbox1.Height div 2);  with Paintbox1.Canvas do  begin    // Fill the background    Brush.Color := clWhite;    FillRect(0, 0, Paintbox1.Width, Paintbox1.Height);     // Draw a cross in the center to define the reference point of the text    Pen.Color := clBlue;    Line(Center.X-CROSS_LEN, Center.Y, Center.X+CROSS_LEN, Center.Y);    Line(Center.X, Center.Y-CROSS_LEN, Center.X, Center.Y+CROSS_LEN);     // Measure the size of the text    Font.Assign(Paintbox1.Font);    ext := TextExtent(txt);     // Draw right-aligned text, centered vertically    TextOut(Center.X-ext.CX-CROSS_LEN-DISTANCE, Center.Y-ext.CY div 2, txt);     // Draw horizontally centered text at distance of DISTANCE pixels above the cross end    TextOut(Center.X-(ext.CX div 2), Center.Y-ext.CY-CROSS_LEN-DISTANCE, txt);     // For clarification: Draw the enclosing rectangles of both texts    Brush.Style := bsClear;    Pen.Color := clRed;    Rectangle(      Center.X-ext.CX-CROSS_LEN-DISTANCE,      Center.Y-ext.CY div 2,    Center.X-CROSS_LEN-DISTANCE,      Center.Y-ext.CY div 2+ext.CY    );    Rectangle(      Center.X-ext.CX div 2,      Center.Y-ext.CY- CROSS_LEN-DISTANCE,      Center.X-ext.CX div 2 + ext.CX,      Center.Y-CROSS_LEN-DISTANCE    );  end;end; ---------------------------------------------------------------------- Boleeman · 2024-04-27 10:47 Been recovering from Covid-19 over the past weeks. Lots of temperature/fever symptoms. Still coughing but getting better. WP. I do get the scaled lines drawn by dragging the mouse either horizontally or vertically (need to either pick horizontal or vertical TOptiongroup). I was using an array of segments to restore the last number line, but needed to expand it to use all segments and not just the last drawn line. There are a few other ways of achieving the same result so I will probably look into those approaches. I hard coded the numbers as I did not know how to scale the numbers using the size of the font/text. Thanks for the code sample, outlining how its done. I realised that when having 1 digit numbers I needed to add a space before it to pad/align it similarly to 2 digit numbers. Need to refill on the Vitamin C, to get over this damn virus bug. ---------------------------------------------------------------------- wp · 2024-04-27 11:03 Quote from: Boleeman on April 27, 2024, 12:47:01 pmI realised that when having 1 digit numbers I needed to add a space before it to pad/align it similarly to 2 digit numbers. This is correct only when you have a mono-spaced font, such as "Courier" where all characters have the same width. In most other fonts, however, each character has its own, individual width. I would guess that a '9' is wider that your padding space, and the alignment will not work out exactly. Get well soon.