Class 10 fyne GoLang GUI Canvas Rectangle
I this tutorial we are going to create a basic retangle in fyne & golang.
Here is the one line code to create a rectangle.
[sourcecode lang=”go”]
rect := canvas.NewRectangle(color.Black)
[/sourcecode]
You can change color of rectangle
rect.FillColor = color.NRGBA{R: 255, G: 255, B: 0, A: 255} [/sourcecode] You can change stroke of rectangle [sourcecode lang=”go”] // Add Stroke
rect.StrokeColor = color.NRGBA{R: 255, G: 0, B: 0, A: 255} [/sourcecode]
You can change stroke width of rectangle
[sourcecode lang=”go”] //Change stroke widthrect.StrokeWidth = 5.0
[/sourcecode] [sourcecode lang=”go” autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ gutter=”true” highlight=”1-3,6,9″ htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false” title=”Source Code main.go”]
package main
// importing fyne
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
)
func main() {
// creating app
a := app.New()
// creating windows
w := a.NewWindow("Rectangle : Canvas")
rect := canvas.NewRectangle(color.Black)
//fill color
rect.FillColor = color.NRGBA{R: 255, G: 255, B: 0, A: 255}
rect.StrokeColor = color.NRGBA{R: 255, G: 0, B: 0, A: 255}
rect.StrokeWidth = 5.0
//setup content
w.SetContent(rect)
//resizing window
w.Resize(fyne.NewSize(400, 400))
w.ShowAndRun() // show and run app
}
Fyne GoLang GUI Course