Very slow drawing

I create a control set of rectangles in code and fill a window to resemble a grid. The code executes very slow.
It takes about 10 seconds before it even starts to draw. The actual drawing is about 2 secs. Intel Core i5 8Gb.

[code] 'Draw boxes in a double loop
'with step 9 (width 8 + 1)

For j as Integer = 1 to (MainWindow.Height- Rectangle1.Height) step 9
For i As Integer = 1 To MainWindow.Width step 9
Dim b As New Box
b.Visible = True
b.Left =i
b.top = j
BoxCounter = BoxCounter + 1
Next
next

'adjust window width to fit the edges

MainWindow.Width = MainWindow.Width + (MainWindow.Width mod 9) -1
MainWindow.Height = MainWindow.Height + ((MainWindow.Height - Rectangle1.Height) mod 9) +1
[/code]

Is there a way to speed things up? Current example draws 3015 boxes.

What are you trying to achieve ?

A Canvas drawing would probably be more appropriate.

Just draw the rectangles in the paint event?
You dont need to create thousands of objects.

Ehh… I want to address the boxes individually and be clickable.

No problem. A canvas can do that too.

Ok, I will try.

  • create a canvas
  • create 4 global constants (rWidth, rHeight, rXspace, rYSpace)

In the PAINTEVENT of the canvas

  dim x as integer
  dim y as integer
  //
  for x=1 to g.width-rWidth step rWidth+rXSpace
    for y=1 to g.height-rHeight step rHeight+rYSpace
      g.drawrect x,y,rWidth,rHeight
    next y
  next x

in the MouseDown Event

  x=(x/(rWidth+rXspace))
  y=(y/(rHeight+rYSpace))
  msgbox "You clicked "+str(x)+","+str(y) 
// You may need to adjust these calculations based on your need, FOR ILLUSTRATION PURPOSES ONLY

rHeight is how TALL the rectangle should be
rWidth is the Width (incase you want NON-SQUARES)
rXspace is how much space between them horizontally
rYSpace is space vertically

This draws 11,000 rectanges is 0.07 seconds or 536 times faster than you example

[ANY CODE SAMPLES PROVIDED ARE FOR ILLUSTATION PURPOSES ONLY, AND IT IS UP TO THE USER TO DETERMINE ANY/ALL MODIFICATION REQUIRED TO MAKE IT FIT THEIR PURPOSE OR DESIGN,]

What you probably want is something like SimpleDraw described in xDev 12.5.

There’s also extensive examples of drawing in the Xojo install.

/Example Projects/Graphics and Multimedia/ObjectsInCanvas might be useful, though I haven’t ever looked at it.

[quote=280004:@Dave S]* create a canvas

  • create 4 global constants (rWidth, rHeight, rXspace, rYSpace)

In the PAINTEVENT of the canvas

  dim x as integer
  dim y as integer
  //
  for x=1 to g.width-rWidth step rWidth+rXSpace
    for y=1 to g.height-rHeight step rHeight+rYSpace
      g.drawrect x,y,rWidth,rHeight
    next y
  next xg

in the MouseDown Event

  x=(x/(rWidth+rXspace))
  y=(y/(rHeight+rYSpace))
  msgbox "You clicked "+str(x)+","+str(y) 
// You may need to adjust these calculations based on your need, FOR ILLUSTRATION PURPOSES ONLY

rHeight is how TALL the rectangle should be
rWidth is the Width (incase you want NON-SQUARES)
rXspace is how much space between them horizontally
rYSpace is space vertically

This draws 11,000 rectanges is 0.07 seconds or 536 times faster than you example

[ANY CODE SAMPLES PROVIDED ARE FOR ILLUSTATION PURPOSES ONLY, AND IT IS UP TO THE USER TO DETERMINE ANY/ALL MODIFICATION REQUIRED TO MAKE IT FIT THEIR PURPOSE OR DESIGN,][/quote]

Thanks Dave,

I knew bout the canvas ofcourse.

Thx.

Ps Your disclaimer? Should I worry? Do I have to sign anything?

seems people have been doing direct copy/paste and wondering why in some situations it didn’t solve their problem. Without taking the due dilegnce to figure out what was being told to them, and the context in which it was offered.