go to Part:   previous   next   1   2   3   4   5   6   7   8   9   10   11 

Patterns Part 2

Drawing by program

We will now let the button do something.

In Revolution the idea of Object is very important. A button is an object, so is an image, a text field and so on. Including a stack.

Each object has a script; let's get at the script of the button: you can right-click on the button and choose from the pop-up menu:

A window will pop up:

NOTE: I have set my background colour of the script windows to a very light pink, yours is probably white, not important.

As you can see, there is already something in that window.

A script is a set of handlers.

The script of button "Go!" has one handler in it: MouseUp.

A handler tells an object what to do when some event occurs.

The MouseUp handler tells the button what to do when the mouse button goes up after you have pushed it down.

We fill something in there:

The button "Apply" at the bottom right of the script window has become black. Press it to apply the script to the button. You do not need to close the script window.

Now, let's run this program: in the tools palette, choose the run mode by clicking the run arrow:

And now you can press the "Go!" button:

Well, that's something…

What did we do?

The program says:

on MouseUp

choose rectangle tool

drag from 20,50 to 100,150

end MouseUp

The first line chooses the paint tool that is a rectangle.

The second line says to drag from one point to another, which will make a rectangle.

20,50 means: twenty pixels from the left of the stack window and fifty pixels from the top.

Likewise 100,150 means 100 pixels from the left and 150 from the top.

So there indeed we have our rectangle.

Let's make another one by adding one line:

on MouseUp

choose rectangle tool

drag from 20,50 to 100,150

drag from 30,60 to 120,170

end MouseUp

Don't forget to press the "Apply" button.

Note that you can change the script even though we are still in run mode.

Press the "Go!" button again:

Whoops!! You can't ! ! ! !

That's because our program left the rectangle tool selected. You can draw more rectangles by hand, but you can't press the button.

First select run mode again:

Now you can press the button and you will get:

Which paints the first and the second rectangle. And their dimensions are as you expect.

Getting it right

We don't want to have to select the run button each time, so we add another line:

on MouseUp

choose rectangle tool

drag from 20,50 to 100,150

drag from 30,60 to 120,170

choose browse tool

end MouseUp

This last line resets the tool back to the run arrow.

Clearing the canvas

We also should wipe the image clean before starting to draw, the easiest way is to get rid of the image before starting to draw:

on MouseUp

if there is an image 1 then delete image 1

choose rectangle tool

drag from 20,50 to 100,150

drag from 30,60 to 120,170

choose browse tool

end MouseUp

This is NOT good programming, because we don't really know that there is only one image and that it has number 1 etc. but just for now it will work.

OK, nothing exciting.

Random stuff

There is a "function" called "random" and we will use it for generating patterns.

random(10) means: pick a number between 1 and 10 at random.

OK, so what about:

on MouseUp

if there is an image 1 then delete image 1

choose rectangle tool

drag from random(100),random(100) to random(100),random(100)

choose browse tool

end MouseUp

There are four lines now:

There are four "calls" on the "random" function, each one asks the random function to pick a number between 1 and 100, and the random function dutifully returns four different such numbers, each one picked at random and each one between 1 and 100.

Each time you press the "Go!" button you will get a different rectangle.

In next part we're working with colour.