Help with making random Shapes

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 8173 Views - Last Post: 05 January 2007 - 02:06 PM Rate Topic: -----

#1 BigD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-January 07

Help with making random Shapes

Posted 02 January 2007 - 07:30 PM

How do you make random shapes on a form like a lines, circles, squares, rectangles, and triangles also when you clikc the button again it should stop the shapes from appearing on the form so if i click lines cicrles and squares the form is full of random lines circles and squares when i clikc them again the the lines, circles, and sqaures stop. Does anyone know the coding for this?
Is This A Good Question/Topic? 0
  • +

Replies To: Help with making random Shapes

#2 KeyWiz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 438
  • Joined: 26-October 06

Re: Help with making random Shapes

Post icon  Posted 02 January 2007 - 09:21 PM

There are many ways to do as you ask, what have you got so far? Show me your code.
Was This Post Helpful? 0
  • +
  • -

#3 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Help with making random Shapes

Posted 02 January 2007 - 09:42 PM

Use line and shape controls. Use control arrays so that you can create more shapes at runtime. Use the Rnd function to generate random numbers. Use these numbers to set the number of shapes, their size and position.
Was This Post Helpful? 0
  • +
  • -

#4 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Help with making random Shapes

Posted 03 January 2007 - 06:45 AM

View PostLouisda16th, on 2 Jan, 2007 - 09:42 PM, said:

Use line and shape controls. Use control arrays so that you can create more shapes at runtime. Use the Rnd function to generate random numbers. Use these numbers to set the number of shapes, their size and position.


:blink: How do you create control arrays at runtime with events?

Or you meant to add:
Make invisible object over drawings to capture click event?
Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Help with making random Shapes

Posted 03 January 2007 - 07:19 AM

NO NO NO.
You don't use a single hidden Object. It only triggers the event of the object, and not of the actual shape.

We need to create controls dynamically by using a control array.
A control array is a set of similar objects that can be accessed by a subscript.

Use the Load() and Unload() Functions to create and destroy objects dynamically.
You can also use the <form>.Controls.Add() Function

Click here to understand how this is done.
It's written in such a manner that beginners find it easier to comprehend.
Was This Post Helpful? 0
  • +
  • -

#6 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Help with making random Shapes

Posted 03 January 2007 - 07:47 AM

View Postborn2c0de, on 3 Jan, 2007 - 07:19 AM, said:

NO NO NO.
You don't use a single hidden Object. It only triggers the event of the object, and not of the actual shape.

We need to create controls dynamically by using a control array.
A control array is a set of similar objects that can be accessed by a subscript.

Use the Load() and Unload() Functions to create and destroy objects dynamically.
You can also use the <form>.Controls.Add() Function

Click here to understand how this is done.
It's written in such a manner that beginners find it easier to comprehend.

I actually never got the idea why to do that maybe you would help me understand. I would'f urged (suppressed :D) him to use some variant of code like :
Dim WithEvents tekst As Label
Dim kujud() As Shape
Dim xvisible As Boolean
Dim x As Integer

Private Sub Command1_Click()
	ReDim kujud(-1) As Shape
End Sub

Private Sub Form_Load()
	Randomize
	x = InputBox("Insert nr.")
	
	ReDim kujud(Val(x))
	For i = 1 To x
		Set kujud(i) = Form1.Controls.Add("vb.shape", "kuju_" & i, Form1)
		
		With kujud(i)
			.Shape = (Rnd() * 50) \ 10 + 1
			.Width = (Rnd() * 5000) \ 5
			.Height = (Rnd() * 5000) \ 5
			.Top = (Rnd() * 5000) \ 5
			.Left = (Rnd() * 5000) \ 5
		End With
	Next i
	
	Set tekst = Form1.Controls.Add("vb.label", "tekst" & i, Form1)
	With tekst
		.Top = 0
		.Left = 0
		.Width = Me.Width
		.Height = Me.Height
		.visible = True
	End With
End Sub

Private Sub tekst_Click()
	xvisible = Not xvisible
	
	For i = 1 To x
		kujud(i).visible = xvisible
	Next i
End Sub


This code would generate x number of elements of random type and size, and by clicking on them you can make them all visible/invisible

This post has been edited by m2s87: 03 January 2007 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#7 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Help with making random Shapes

Posted 03 January 2007 - 08:48 AM

DMN :D I actually forgot while writing what i was doing :P Actually now i think he wanted something like
Dim WithEvents tekst As Label
Dim WithEvents stopper As timer
Dim kujud() As Shape
Dim makemore As Boolean
Dim x As Integer

Private Sub Form_Load()
	Set stopper = Form1.Controls.Add("vb.timer", "stopper", Form1)
	With stopper
		.Interval = 600
		.Enabled = True
	End With
	
	Set tekst = Form1.Controls.Add("vb.label", "tekst" & i, Form1)
	With tekst
		.Top = 0
		.Left = 0
		.Width = Me.Width
		.Height = Me.Height
		.visible = True
		.BackStyle = 0
	End With
End Sub
Private Sub tekst_Click()
	makemore = Not makemore
End Sub

Private Sub Stopper_Timer()
  If makemore = False Then
	Randomize
	x = x + 1
	
	ReDim kujud(Val(x))
	For i = x To x
		Set kujud(i) = Form1.Controls.Add("vb.shape", "kuju_" & i, Form1)
		
		With kujud(i)
			.Shape = (Rnd() * 50) \ 10 + 1
			.Width = (Rnd() * 5000) \ 5
			.Height = (Rnd() * 5000) \ 5
			.Top = (Rnd() * 5000) \ 5
			.Left = (Rnd() * 5000) \ 5
			.visible = True
		End With
	Next i
  End If
End Sub

Was This Post Helpful? 0
  • +
  • -

#8 BigD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-January 07

Re: Help with making random Shapes

Posted 04 January 2007 - 02:29 PM

Ok this is how it is. Im using Visual Basic 5 at school and i made random lines,circles,and triangles, now i would need help making a square and rectangle. Im not sure how the coding went for the Line but it goes like this


Line(x1,y1)-(x2,y2)QBColor©

then i had some stuff like d=Int(upperbound-lowerbound+1)*Rnd-1))
something like that. All I would need help in now is how to make a square and rectangle
Was This Post Helpful? 0
  • +
  • -

#9 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Help with making random Shapes

Posted 04 January 2007 - 03:27 PM

Use this function
Public Sub joonista_ristkylik(ByVal x1, ByVal y1, ByVal x2, ByVal y2)
	Picture1.Line (x1, y1)-(x1, y2), vbBlue
	Picture1.Line (x1, y1)-(x2, y1), vbBlue
	Picture1.Line (x2, y2)-(x1, y2), vbBlue
	Picture1.Line (x2, y2)-(x2, y1), vbBlue
End Sub


And when you want random size square, just
randomize
a=rnd()*2000+100
call joonista_ristkylik(0,0,a,a)


and if you need a rectangle then just
randomize
a=rnd()*2000+100
h=rnd()*2000+100
call joonista_ristkylik(0,0,a,h)


Note: you need a Picture to draw in, so add Picture1 to the form

Hope it helps :D
Was This Post Helpful? 0
  • +
  • -

#10 BigD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-January 07

Re: Help with making random Shapes

Posted 04 January 2007 - 04:15 PM

Just one question what is Public Sub joonista_ristkylik? havent seen that before
Was This Post Helpful? 0
  • +
  • -

#11 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Help with making random Shapes

Posted 04 January 2007 - 04:30 PM

View PostBigD, on 4 Jan, 2007 - 04:15 PM, said:

Just one question what is Public Sub joonista_ristkylik? havent seen that before


As the name says, its a public (means you can get access to it where ever it is) procedure called (in en:) draw_rectangle. It is a code snippet that draws a rectangle, you just need to specify the coordinates.
Was This Post Helpful? 0
  • +
  • -

#12 BigD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-January 07

Re: Help with making random Shapes

Posted 04 January 2007 - 04:52 PM

Im really absolutly sure that your coding works but i need a simple way because im sure my VB teacher will notice i didnt make this. My code looked something like this

Dim a,b,c,d,e,f,g,x as integer
Dim upperbound,lowerbound as integer
Dim x1,y1,x2,y2 as integer

Private Sub cmdexit_click()
end
end sub

Private sub cmdclear_click()
refresh
end sub

Private Sub cmddraw_click()
x=x+1
If x= 1 then
cmdraw.caption=Stop
tmrdraw.interval=100
else
cmddraw.caption=Draw Line
tmrdraw.interval=0
x=0
end if
end sub.....................Im not sure if this is right becuz i dont have the notes with me lol

Private Sub tmrdraw_Timer()

there is too much here for me to memorize
i had stuff like
a=Int((upperbound-lowerbound+1)*Rnd-1))
b-g would say the same thing
the i had like
f=c
Line(x1.y1)-(x2,y2),QBColor©
Line(x1.y1)-(x2,y2),QBColor©
Line(x1.y1)-(x2,y2),QBColor©
end sub

Im not sure if this correct how i typed it but at school my random lines work i just need something like this that makes squares
Was This Post Helpful? 0
  • +
  • -

#13 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Help with making random Shapes

Posted 04 January 2007 - 05:10 PM

I guess what you want is the drawing function. You do need to code it yourself, but i guess it could look like:

Private Sub tmrdraw_Timer()
	Randomize
 
	a=50+Int((upperbound-lowerbound+1)*Rnd-1))
	b=50+Int((upperbound-lowerbound+1)*Rnd-1))
	x=50
	y=50
	f=c

	Line (x, y)-(x, b),QBColorİ
	Line (x, y)-(a, y),QBColorİ
	Line (a, b)-(x, b),QBColorİ
	Line (a, b)-(a, y),QBColorİ
end sub

Was This Post Helpful? 0
  • +
  • -

#14 BigD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-January 07

Re: Help with making random Shapes

Posted 04 January 2007 - 05:21 PM

So if i use the code you gave me when i click draw triangle it should make random triangles appear all over the form?
Was This Post Helpful? 0
  • +
  • -

#15 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • Posts: 1,967
  • Joined: 03-August 06

Re: Help with making random Shapes

Posted 05 January 2007 - 06:26 AM

VB5 is quite old. You should move to VB6. Or better: VB.NET 2005
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2