0

I have implemented the canvas on which we draw with a brush in Smalltak Squeak but with only a single color and a single size. I'd like to add the possibility of changing the color and the size of the brush. Could anyone help me please ?

Thank you in advance.

The differents methods are:

"Methods"
extent: aPoint
    | newForm |
    super extent: aPoint.
    newForm := Form extent: self extent depth: 16.
    newForm fillColor: Color veryLightGray.
    form ifNotNil: [form displayOn: newForm].
    form := newForm.


initialize
    super initialize.
    self extent: 400@350.

drawOn: aCanvas
    aCanvas image: form at: bounds origin.

handlesMouseDown: evt
    ^ true     

mouseDown: evt
    brush := Pen newOnForm: form.
    brush roundNib: 3.
    brush color: Color red.
    lastMouse := evt cursorPoint - bounds origin.
    brush drawFrom: lastMouse to: lastMouse.
    self invalidRect:
    ((lastMouse - brush sourceForm extent corner:
    lastMouse + brush sourceForm extent)
    translateBy: bounds origin).


mouseMove: evt
    | p |
    p := evt cursorPoint - bounds origin.
    p = lastMouse ifTrue: [^ self].
     brush drawFrom: lastMouse to: p.
    self invalidRect: ((
        ((lastMouse min: p) - brush sourceForm extent) corner:
        ((lastMouse max: p) + brush sourceForm extent))
            translateBy: bounds origin).
    lastMouse := p.


addCustomMenuItems: aCustomMenu hand: aHandMorph
    super addCustomMenuItems: aCustomMenu hand: aHandMorph.
    aCustomMenu add: 'clear' action: #clear.


clear
    form fillColor: Color veryLightGray.
    self changed.
Yoan15
  • 33
  • 2
  • 5
  • I don't see your problem. You already set color and size in `#mouseDown:`. – Max Leske Aug 14 '16 at 07:08
  • @MaxLeske yes but i'd like to add the possibility to choose others color and size for the brush. In my code, the color and the size are fixed. – Yoan15 Aug 15 '16 at 22:19
  • As long as you have an API, users can choose whatever color they want. Or are you trying to say that you want to present the choices of color and brush size in a graphical user interface? – Max Leske Aug 16 '16 at 05:00
  • @MaxLeske Yes i'd like to present the choices of the color and brush size – Yoan15 Aug 16 '16 at 07:13
  • Then you will have to build a user interface. How to do that is definitely out of scope for SO. You will have to get your hands dirty with Morphic, which is definitely not something that can be done quickly without experience. To get you started, look at the `Morph` class hierarchy. – Max Leske Aug 16 '16 at 07:49
  • @MaxLeske I thought of adding in the menu color and size methods like clear method which allows me to erase the drawing – Yoan15 Aug 16 '16 at 08:12
  • In what menu? Of course you can do that... once you *have* a menu. – Max Leske Aug 16 '16 at 09:31
  • @MaxLeske I have implemented a method for the menu, the method addCustomMenuItems: aCustomMenu hand: aHandMorph. I'd like to add it in this menu like "clear". – Yoan15 Aug 16 '16 at 09:41
  • What do you mean by "like 'clear'"? I don't have access to the code of your user interface. How should I know how "clear" has been implemented? I suggest you ask your questions on the Squeak Slack channel, the mailing list, or on IRC (see http://squeak.org/community/). – Max Leske Aug 16 '16 at 11:59
  • @MaxLeske Yoan15 has given the code for the "clear" command with the custom menu code. You might need to scroll down .... – z-- Aug 16 '16 at 12:50
  • @Yoan15, please post as well the class definition so that the instance variables are visible. – z-- Aug 16 '16 at 13:15
  • @z-- You understood for the "clear" command ;) "Subclass" Object subclass: #NameOfSubclass instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Morphic-Fun' – Yoan15 Aug 17 '16 at 14:43
  • @z-- and Morph subclass: #PaintMorph instanceVariableNames: 'form brush lastMouse' classVariableNames: '' poolDictionaries: '' category: 'Morphic-Fun' – Yoan15 Aug 17 '16 at 14:47

0 Answers0