About Monkey 2 › Forums › Monkey 2 Code Library › Simple paint application.
This topic contains 0 replies, has 1 voice, and was last updated by 
 cocon 1 year, 4 months ago.
		Viewing 1 post (of 1 total)
	
	- 
		AuthorPosts
 - 
		
			
				
December 15, 2017 at 10:25 am #12302
This is the a very simple paint application, I will use it as a starting point of something else in the future. Adding more features to the code as it is, requires a total redesign which beats the purpose of making a simple snippet. This is why is such a direct implementation without any abstractions, so it becomes interesting for studying. Currently it allows you to draw, clear screen, and save a screenshot.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowField _drawImage:ImageField _drawCanvas:CanvasField _mouseDown:BoolField _mousePrevious:Vec2iField _colorDraw:ColorField _colorClear:ColorMethod New()Super.New("Paint Application", 800, 600)_colorDraw = Color.Black_colorClear = Color.LightGrey_drawImage = New Image(Width, Height, PixelFormat.RGBA8, TextureFlags.Dynamic)_drawCanvas = New Canvas(_drawImage)_drawCanvas.Color = _colorDraw_drawCanvas.Clear(_colorClear)_mousePrevious = New Vec2iPrint("Welcome To Simple Paint Application")Print("Draw with left mouse button.")Print("Press Backspace to clear screen.")Print("Press Enter to save a screenshot.")EndMethod OnRender(canvas:Canvas) Overridecanvas.Clear(_colorClear)canvas.DrawImage(_drawImage, 0, 0)EndMethod OnKeyEvent(event:KeyEvent) OverrideSelect event.TypeCase EventType.KeyDownSelect event.KeyCase Key.Backspace_drawCanvas.Clear(_colorClear)App.RequestRender()Print("Cleared Image")Case Key.EnterLocal pixmap := _drawCanvas.CopyPixmap(_drawCanvas.Viewport)Local time := Time.Now()Local filename := ""filename += time.Yearfilename += time.Monthfilename += time.Dayfilename += time.Hoursfilename += time.Minutesfilename += time.Secondsfilename += Millisecs()filename = HomeDir() + filename + ".png"pixmap.Save(filename)Print("Saved Image: " + filename)EndEndEndMethod OnMouseEvent(event:MouseEvent) OverrideSelect event.TypeCase EventType.MouseDown_mouseDown = True_mousePrevious.X = Mouse.X_mousePrevious.Y = Mouse.YCase EventType.MouseUp_mouseDown = FalseCase EventType.MouseMoveIf _mouseDown_drawCanvas.DrawLine(_mousePrevious.X, _mousePrevious.Y, Mouse.X, Mouse.Y)_drawCanvas.Flush()App.RequestRender()_mousePrevious.X = Mouse.X_mousePrevious.Y = Mouse.YEndEndEndEndFunction Main()New AppInstanceNew MyWindowApp.Run ()EndAttachments:
 - 
		AuthorPosts
 
		Viewing 1 post (of 1 total)
	
	You must be logged in to reply to this topic.
