|
-
Oct 28th, 2001, 07:30 AM
#1
Thread Starter
New Member
Drawing Lines
JUST STARTED FIRST ASSIGNMENT IN VB AND AM STUCK.
HAVE TO DRAW A LINE ON A PIC BOX GOING NORTH, SOUTH, EAST OR WEST WITH 3 COMMAND BUTTONS AND LABEL SHOWING DIRECTION (N,S,E,W)
1- FORWARD BUTTON
2- TURN LEFT
3 TURN RIGHT
DEFAULT IS 300 TWIPS PER CLICK
THE LINE STARTS AT BOTTOM, MIDDLE OF PICTURE BOX AND IS ALWAYS CONNECTED.
I HAVE BEEN ABLE TO MOVE AN IMAGE ROUND ON A PIC BOX (ALTHOUGH NOT ROTATING IT) WITH A CORRESPONDING DIRECTION IN A LABEL (N,S,E,W). THIS IMAGE MOVES 300 TWIPS WITH EACH CLICK. HOWEVER, I JUST CANNOT FIGURE OUT HOW TO DRAW A LINE THAT ACTS IN THE SAME WAY.
CAN ANYONE HELP ME, PLEASE???????????
KIND REGARDS
JAMES
-
Oct 28th, 2001, 07:58 AM
#2
PowerPoster
Re: Drawing Lines
Hi
ok first thing .. let go of that caps lock key!! 
here is some code for u to play around with.. i am not sure what stage u are at with VB and so maybe some of it u may not be sure about. Pls ask if u have any qtns or if u need to do it an easier way eg without a seperate 'Update' subroutine.
Regards
Stuart
VB Code:
'FORM HAS:
Picture1 - Picture box
Command1, Command2, Command3, Command4 - Command buttons
Label1 - Label control
Option Explicit
Dim CurX As Single 'starting / current positions
Dim CurY As Single
Private Sub Form_Load()
'Set captions .. can be done at design time instead of here
Command1.Caption = "North"
Command2.Caption = "West"
Command3.Caption = "East"
Command4.Caption = "South"
'Set start position to centre bottom
With Picture1 'Work on Picture1 within With.. With End stmt
CurX = .Width \ 2 'Centre of picture
CurY = .Height 'Bottom of picture
.CurrentX = CurX 'Set drawing posn
.CurrentY = CurY
End With
End Sub
Private Sub Command1_Click()
Update 0, -300 'Tell sub to move X by 0 and Y by -300
Label1.Caption = "North" 'update caption
End Sub
Private Sub Command2_Click()
Update -300, 0
Label1.Caption = "West"
End Sub
Private Sub Command3_Click()
Update 300, 0
Label1.Caption = "East"
End Sub
Private Sub Command4_Click()
Update 0, 300
Label1.Caption = "South"
End Sub
Private Sub Update(ByVal MoveX As Single, ByVal MoveY As Single)
'Sub gets movement amounts of X and Y
'You can check for boundaries of picture or remove everything before and
'including the "Then" keyword
If CurX + MoveX > 0 And CurX + MoveX < Picture1.Width Then CurX = CurX + MoveX
If CurY + MoveY > 0 And CurY + MoveY < Picture1.Height Then CurY = CurY + MoveY
'Draw line from current position to new position
Picture1.Line -(CurX, CurY)
End Sub
-
Oct 28th, 2001, 09:54 AM
#3
Frenzied Member
Re: Re: Drawing Lines
Originally posted by beachbum
Hi
ok first thing .. let go of that caps lock key!!
if he starts misspelling stuff he'll be teh next jeff K
I'm bringing geeky back...
-
Oct 28th, 2001, 09:54 AM
#4
What are you saying James? I thought Stuarts code hit it right on the head.
-
Oct 28th, 2001, 12:13 PM
#5
Thread Starter
New Member
Originally posted by MarkT
What are you saying James? I thought Stuarts code hit it right on the head.
Stuart U nailed it,
Thanks mate, that helped a great deal.
How about making the line disappear and reappear several commands later? The lines already drawn must stay visible.
Any help appreciated
James in sunny UK
-
Oct 28th, 2001, 05:28 PM
#6
PowerPoster
hi again
ok, how about something like this.. it is not "perfect" but u can get the idea and play around with it.
1) Keep all the sub code from the original and only change the ones listed here
2) Add a small shape control to the picture box. Add a checkbox control to the form (not in the pic box)
VB Code:
Option Explicit
Dim CurX As Single 'starting / current positions
Dim CurY As Single
Private Sub Form_Load()
'Set captions .. can be done at design time instead of here
Command1.Caption = "North"
Command2.Caption = "West"
Command3.Caption = "East"
Command4.Caption = "South"
'Set start position to centre bottom
With Picture1 'Work on Picture1 within With.. With End stmt
CurX = .Width \ 2 'Centre of picture
CurY = .Height 'Bottom of picture
.CurrentX = CurX 'Set drawing posn
.CurrentY = CurY
'New---------------------------------------------------------
.AutoRedraw = True
End With
Check1.Caption = "Hide Line"
Shape1.Visible = False
'New---------------------------------------------------------
End Sub
Private Sub Update(ByVal MoveX As Single, ByVal MoveY As Single)
'Sub gets movement amounts of X and Y
'You can check for boundaries of picture or remove everything before and
'including the "Then" keyword
If CurX + MoveX > 0 And CurX + MoveX < Picture1.Width Then CurX = CurX + MoveX
If CurY + MoveY > 0 And CurY + MoveY < Picture1.Height Then CurY = CurY + MoveY
'Draw line from current position to new position
Picture1.Line -(CurX, CurY)
'New---------------------------------------------------------
With Shape1
.Move CurX - .Width \ 2, CurY - .Height \ 2
End With
'New---------------------------------------------------------
End Sub
'New---------------------------------------------------------
Private Sub Check1_Click()
If Check1.Value = vbChecked Then
Shape1.Visible = True
Picture1.DrawMode = 11 'Sorry cant remember constant name
Else
Shape1.Visible = False
Picture1.DrawMode = 13
End If
End Sub
'New---------------------------------------------------------
Regards
Stuart
-
Nov 6th, 2001, 02:42 AM
#7
PowerPoster
Hi Jimmbo
In response to ur Pm regarding saving lines into an array, i added extra code to that which u already had.
ps. I receive an email as soon as u or anyone else replies to this thread and so it can save u from doing pm's. 
Regards
Stuart
VB Code:
'Form contains
'Command buttons (Command1 to Command4)
'Label1 for direction
'Check1 if u want to hide / show line
'Shape1 inside picture box
'Picture1 for drawing area
'NEW CONTROLS SINCE LAST TIME
'Command Buttons (Command5 to Command7) for record, stop, play
'Timer1 for playback
Option Explicit
'NEW ------------------------------------.
Private Type LineType
X1 As Single
Y1 As Single
X2 As Single
Y2 As Single
'You could add stuff here for colours etc
End Type
Dim MyLine() As LineType
Dim LineCounter As Long
Dim RecorderOn As Boolean
'NEW ------------------------------------'
Dim CurX As Single 'starting / current positions
Dim CurY As Single
Private Sub Form_Load()
'NEW ------------------------------------
LineCounter = -1 'so that first item starts at zero by adding 1
Command5.Caption = "Record"
Command6.Caption = "Stop Recording"
Command7.Caption = "Playback"
Command6.Enabled = False
Command7.Enabled = False
RecorderOn = True 'set to on auto
Timer1.Interval = 500 '1/2 second playback
Timer1.Enabled = False 'Set timer off to start
'NEW ------------------------------------
'Set captions .. can be done at design time instead of here
Command1.Caption = "North"
Command2.Caption = "West"
Command3.Caption = "East"
Command4.Caption = "South"
'Set start position to centre bottom
With Picture1 'Work on Picture1 within With.. With End stmt
CurX = .Width \ 2 'Centre of picture
CurY = .Height 'Bottom of picture
.CurrentX = CurX 'Set drawing posn
.CurrentY = CurY
.AutoRedraw = True
End With
Check1.Caption = "Hide Line"
Shape1.Visible = False
End Sub
Private Sub Command1_Click()
Update 0, -300 'Tell sub to move X by 0 and Y by -300
Label1.Caption = "North" 'update caption
End Sub
Private Sub Command2_Click()
Update -300, 0
Label1.Caption = "West"
End Sub
Private Sub Command3_Click()
Update 300, 0
Label1.Caption = "East"
End Sub
Private Sub Command4_Click()
Update 0, 300
Label1.Caption = "South"
End Sub
Private Sub Update(ByVal MoveX As Single, ByVal MoveY As Single)
'Sub gets movement amounts of X and Y
'You can check for boundaries of picture or remove everything before and
'including the "Then" keyword
If CurX + MoveX > 0 And CurX + MoveX < Picture1.Width Then CurX = CurX + MoveX
If CurY + MoveY > 0 And CurY + MoveY < Picture1.Height Then CurY = CurY + MoveY
'NEW ------------------------------------.
'Put line into array
If RecorderOn And Shape1.Visible = False Then 'Only do if drawing a line
'NB u could also set some other flag if preferred
LineCounter = LineCounter + 1
ReDim Preserve MyLine(LineCounter)
With MyLine(LineCounter)
.X1 = Picture1.CurrentX
.Y1 = Picture1.CurrentY
.X2 = CurX
.Y2 = CurY
End With
End If
'NEW ------------------------------------'
'Draw line from current position to new position
Picture1.Line -(CurX, CurY)
With Shape1
.Move CurX - .Width \ 2, CurY - .Height \ 2
End With
End Sub
Private Sub Check1_Click()
If Check1.Value = vbChecked Then
Shape1.Visible = True
Picture1.DrawMode = 11 'Sorry cant remember constant name
Else
Shape1.Visible = False
Picture1.DrawMode = 13
End If
End Sub
'NEW ------------------------------------.
Private Sub Command5_Click()
'Start recording
With Picture1
'Remember current coordinates before clearing
CurX = .CurrentX
CurY = .CurrentY
.Cls 'Clear picture to start with
.CurrentX = CurX 'Reset coords
.CurrentY = CurY
End With
RecorderOn = True
LineCounter = -1
'Show applicable buttons
Command5.Enabled = False 'Record
Command6.Enabled = True 'Stop
Command7.Enabled = False 'Play
End Sub
Private Sub Command6_Click()
'Stop recording
RecorderOn = False
Command5.Enabled = True
Command6.Enabled = False
Command7.Enabled = True
End Sub
Private Sub Command7_Click()
'Playback using Timer 1
Command5.Enabled = False
Command6.Enabled = False
Command7.Enabled = False
If UBound(MyLine) >= 0 Then
Picture1.Cls 'Clear before redraw
Timer1.Enabled = True 'Only do if some to redraw
Else
Command5.Enabled = True 'Else reset to allow recording
End If
End Sub
Private Sub Timer1_Timer()
'Actual redraw
Static lNumLines As Long
If lNumLines <= UBound(MyLine) Then
With MyLine(lNumLines)
Picture1.Line (.X1, .Y1)-(.X2, .Y2)
End With
lNumLines = lNumLines + 1
Else
lNumLines = 0
Timer1.Enabled = False
Command5.Enabled = True
Command7.Enabled = True
End If
End Sub
'NEW ------------------------------------'
-
Nov 6th, 2001, 04:26 PM
#8
Thread Starter
New Member
RECORD
Hi Stu
I am having trouble with the code.
Have an error message when playback is pressed "subscript out of range" and VB directs me to:
If UBound(MyLine) >=0 Then etc
Any ideas before I headbut the monitor
Regards
Jimbo
-
Nov 6th, 2001, 07:25 PM
#9
PowerPoster
Hi
That is strange. I just copied and pasted to a new project and it worked fine. did u change anything in the code before running it?
If u didnt change code and you are still getting a problem at that line u can change...
FROM : If Ubound(MyLine)>=0 Then
TO: If LineCounter >= 0 Then
Regards
Stuart
-
Nov 7th, 2001, 11:49 AM
#10
Addicted Member
I just copied and pasted and pasted staurt and Beachebump first code.I placed label 4 commad buttons and one picture.
It is not working.It is not drawing lines when I click the nuttons.
Thanks.
-
Nov 7th, 2001, 11:55 AM
#11
Addicted Member
It is working.Sorry it is my mistake.I chenged image color = black. .
Thanks.
-
Nov 9th, 2001, 08:44 AM
#12
Thread Starter
New Member
Store and play back 2 shapes
Hi
Stu, it seems your code has the ability to store several shapes but I am struggling to play back each shape individually.
My aim is to have a combo box on the form with the choice of recording 2 different shapes. At any time I am able to playback either shape.
Any Ideas????
James
Option Explicit
Private Type LineType
X1 As Single
Y1 As Single
X2 As Single
Y2 As Single
End Type
Dim Shape1 As String
Dim Shape2 As String
Dim Black As String
Dim Blue As String
Dim Green As String
Dim Red As String
Dim MyLine() As LineType
Dim LineCounter As Long
Dim RecorderOn As Boolean
Dim CurX As Single
Dim CurY As Single
Dim n As Integer
Dim up As Single
Dim Down As Single
Dim west As Single
Dim south As Single
Dim east As Single
Dim north As Single
Private Sub Command1_Click()
n = Text1.Text
Image1.Stretch = True
If Combo1.Text = "Red" Then
Picture1.ForeColor = vbRed
ElseIf Combo1.Text = "Green" Then
Picture1.ForeColor = vbGreen
ElseIf Combo1.Text = "Blue" Then
Picture1.ForeColor = vbBlue
ElseIf Combo1.Text = "Black" Then
Picture1.ForeColor = vbBlack
End If
If Label1.Caption = "north" Then
Update 0, -n
ElseIf Label1.Caption = "east" Then
Update -n, 0
ElseIf Label1.Caption = "south" Then
Update 0, n
ElseIf Label1.Caption = "west" Then
Update n, 0
End If
End Sub
'west is right
'east is left
Private Sub Command2_Click()
If Label1.Caption = "north" Then
Label1.Caption = "east"
ElseIf Label1.Caption = "east" Then
Label1.Caption = "south"
ElseIf Label1.Caption = "south" Then
Label1.Caption = "west"
Else
Label1.Caption = "north"
End If
End Sub
Private Sub Command3_Click()
If Label1.Caption = "north" Then
Label1.Caption = "west"
ElseIf Label1.Caption = "west" Then
Label1.Caption = "south"
ElseIf Label1.Caption = "south" Then
Label1.Caption = "east"
Else
Label1.Caption = "north"
End If
End Sub
Private Sub Command4_Click()
Text2.Text = "down"
If Text2.Text = "down" Then
Picture1.DrawMode = 13
Image1.Visible = True
End If
End Sub
Private Sub Command5_Click()
Text2.Text = "up"
If Text2.Text = "up" Then
Picture1.DrawMode = 11
Image1.Visible = True
End If
End Sub
Private Sub Command6_Click()
With Picture1
Picture1.Cls
Combo1.Text = "Black"
Label1.Caption = "north"
CurX = .Width \ 2
CurY = .Height \ 2
.CurrentX = CurX
.CurrentY = CurY
End With
With Image1
.Move CurX - .Width \ 2, CurY - .Height \ 2
End With
If Command8.Enabled = False Then
Image1.Visible = True
End If
Combo1.Enabled = True
End Sub
Private Sub Command7_Click()
'Start recording
With Picture1
'Remember current coordinates before clearing
CurX = .CurrentX
CurY = .CurrentY
.Cls 'Clear picture to start with
CurX = .Width \ 2
CurY = .Height \ 2
.CurrentX = CurX
.CurrentY = CurY
End With
If RecorderOn = True Then
LineCounter = -1
'Show applicable buttons
Command7.Enabled = False 'Record
Command8.Enabled = True 'Stop
Command9.Enabled = False 'Play
Combo1.Enabled = False
End If
End Sub
Private Sub Command8_Click()
'Stop recording
RecorderOn = False
Command7.Enabled = True 'was true
Command8.Enabled = False
Command9.Enabled = True
End Sub
Private Sub Command9_Click()
'Playback using Timer 1
Command7.Enabled = False
Command8.Enabled = False
Command9.Enabled = False
Combo1.Enabled = False 'have put in
If UBound(MyLine) >= 0 Then
Picture1.Cls 'Clear before redraw
Image1.Visible = False 'image invis. on play
Timer1.Enabled = True 'Only do if some to redraw
Else
Command7.Enabled = False 'Else reset to allow recording
'was true
End If
End Sub
Private Sub Form_Load()
Combo2.Text = "Shape1"
Combo2.AddItem "Shape1"
Combo2.AddItem "Shape2"
Combo1.Text = "Black"
Combo1.AddItem "Black"
Combo1.AddItem "Red"
Combo1.AddItem "Green"
Combo1.AddItem "Blue"
Text2.Text = "down"
LineCounter = -1
Command7.Caption = "Record Shape"
Command8.Caption = "Stop"
Command9.Caption = "Play"
Command8.Enabled = False
Command9.Enabled = False
RecorderOn = True
Timer1.Interval = 500
Timer1.Enabled = False
With Picture1
CurX = .Width \ 2
CurY = .Height \ 2
.CurrentX = CurX
.CurrentY = CurY
.AutoRedraw = True
End With
With Image1
.Move CurX - .Width \ 2, CurY - .Height \ 2
End With
Image1.Visible = True
End Sub
Private Sub Update(ByVal MoveX As Single, ByVal MoveY As Single)
If CurX + MoveX > 0 And CurX + MoveX < Picture1.Width Then CurX = CurX + MoveX
If CurY + MoveY > 0 And CurY + MoveY < Picture1.Height Then CurY = CurY + MoveY
LineCounter = LineCounter + 1
ReDim Preserve MyLine(LineCounter)
With MyLine(LineCounter)
.X1 = Picture1.CurrentX
.Y1 = Picture1.CurrentY
.X2 = CurX
.Y2 = CurY
End With
Picture1.Line -(CurX, CurY)
With Image1
.Move CurX - .Width \ 2, CurY - .Height \ 2
End With
End Sub
Private Sub Timer1_Timer()
'Actual redraw
Static lNumLines As Long
If lNumLines <= UBound(MyLine) Then
With MyLine(lNumLines)
Picture1.Line (.X1, .Y1)-(.X2, .Y2)
End With
lNumLines = lNumLines + 1
Else
lNumLines = 0
Timer1.Enabled = False
Command7.Enabled = True
Command9.Enabled = True
End If
End Sub
-
Nov 9th, 2001, 10:12 AM
#13
Thread Starter
New Member
Auto record
Me again
Lets forget about 2 shapes for the time being.
Any further lines drawn after stop is pressed are still recorded and played back.
It seems the recorder is on auto all the time (which is how it was set in form), but I want to be able to draw further lines on the picturebox without recording them.
Without sounding a dick, How do i turn off auto record.
Desperate
James
-
Nov 9th, 2001, 07:40 PM
#14
-
Nov 10th, 2001, 08:26 AM
#15
Thread Starter
New Member
RECORD
Hi Stu,
I will try to be a simple and precise as possible (Hopefully)
Ok, I am now reffering to your original code and not mine.
1 - Run program
2 - Press record
3 - Draw a square
4 - Press Stop record
5 - Draw another line without pressing record
6 - finish drawing other line
7 - press play to see square that i recorded
8 - square is drawn ASWELL as the other line
9 - I dont want this other line to redraw when i press play.
So, what i want to do is record a shape, then I want to be able to draw another shape without it being automatically recorded.
so, i want to be able to
1 - press record
2 - draw a square (for example)
3 - press stop
4 - draw a few more shapes for fun without recording
5 - press play to see recorded square only
6 - draw a few more lines and shapes without recording
7 - press play again to see square only.
Thus, only the square is saved in memory.
I was also attempting to record several shapes for furture playback at any time. However, I think I will just be happy to record one shape.
Hope you understand
Kind Regards
James
-
Nov 10th, 2001, 06:05 PM
#16
PowerPoster
Hi James
Now u got me confused lol. I just set up the program again straight from here (copy / paste) and it does not redisplay any lines drawn after u press Stop Record. This is how it goes (for me at least )
1 - Run program
2 - Press record
3 - Draw a square
4 - Press Stop record
5 - Draw another line without pressing record
6 - finish drawing other line
7 - press play to see square that i recorded
8 - square is drawn AND NOT THE OTHER LINE
Are u sure that u didnt modify the code somehow?
Also in regard to recording a number of shapes. You can already do that. Remember that u have the checkbox to stop the line being visible. And so, if u do this u can record 2 squares for example.
1 - Run program
2 - Press record
3 - Draw a square
4 - Check the Hide Line checkbox
5 - Move to a new position
6 - Uncheck the checkbox
7 - Draw another square
8 - Press Stop record
9 - press play to see both squares
This works. You dont have to use the particular controls that i set up but the underlying code can be transferred as u desire. If u just make up a separate project for testing and copy / paste the code directly from here I am sure that it will work as planned.
Regards
Stuart
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|