package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"image/color"
)
func main() {
myApp := app.New()
w := myApp.NewWindow("Fyne的canvas")
Rectangles(w)
w.Resize(fyne.NewSize(600, 600)) // 窗口大小
w.ShowAndRun() // 渲染完毕
}
// 长方形
func Rectangles(w fyne.Window) {
rect := canvas.NewRectangle(color.White) // 画一个长方形
rect.StrokeColor = color.Black // 边框颜色
rect.StrokeWidth = 5 // 边框大小
w.SetContent(rect) //窗口添加控件
}
二,canvas渲染文本文字
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"image/color"
)
func main() {
myApp := app.New()
w := myApp.NewWindow("Fyne的canvas")
txt(w)
w.Resize(fyne.NewSize(600, 600)) // 窗口大小
w.ShowAndRun() // 渲染完毕
}
// 渲染文本框
func txt(w fyne.Window) {
// 文本框
txt := canvas.NewText("12123", color.RGBA{255, 0, 0, 255})
txt.Alignment = fyne.TextAlignCenter // 文字对齐方式
txt.TextSize = 10
txt.TextStyle = fyne.TextStyle{ // 文字状态
Bold: true,
Italic: true,
Monospace: false,
}
txt1 := canvas.NewText("456456", color.RGBA{255, 205, 95, 255})
txt1.Alignment = fyne.TextAlignCenter // 文字对齐方式
txt1.TextSize = 20
txt1.TextStyle = fyne.TextStyle{ // 文字状态
Bold: false, // 加粗
Italic: false, // 斜体
Monospace: true,
}
w.SetContent(container.NewVBox(txt, txt1)) //窗口添加控件
}
三,线条
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"image/color"
)
func main() {
myApp := app.New()
w := myApp.NewWindow("Fyne的canvas")
Lines(w)
w.Resize(fyne.NewSize(600, 600)) // 窗口大小
w.ShowAndRun() // 渲染完毕
}
// 线条
func Lines(w fyne.Window) {
l := canvas.NewLine(color.RGBA{255, 0, 0, 155})
l.StrokeWidth = 25
l.Position1 = fyne.NewPos(0, 0)
l.Position2 = fyne.NewPos(0, 100)
l.Refresh()
l1 := canvas.NewLine(color.RGBA{255, 100, 100, 155})
l1.StrokeWidth = 15
l1.Position1 = fyne.NewPos(0, 0)
l1.Position2 = fyne.NewPos(100, 100)
l.Refresh()
w.SetContent(container.NewWithoutLayout(l, l1))
}
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"image/color"
)
func main() {
myApp := app.New()
w := myApp.NewWindow("Fyne的canvas")
cles(w)
w.Resize(fyne.NewSize(600, 600)) // 窗口大小
w.ShowAndRun() // 渲染完毕
}
// 圆形
func cles(w fyne.Window) {
c := canvas.NewCircle(color.RGBA{255, 11, 11, 235})
c.StrokeWidth = 10
c.StrokeColor = color.RGBA{10, 10, 10, 235}
c.Position1 = fyne.NewPos(10, 10)
c.Position2 = fyne.NewPos(100, 100)
c1 := canvas.NewCircle(color.RGBA{255, 11, 11, 235})
c1.StrokeWidth = 10
c1.StrokeColor = color.RGBA{10, 10, 10, 235}
c1.Position1 = fyne.NewPos(100, 100)
c1.Position2 = fyne.NewPos(200, 200)
w.SetContent(container.NewWithoutLayout(c, c1)) // 有定位必须在 newWithoutLayout 上面画
}
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/theme"
)
func main() {
myApp := app.New()
w := myApp.NewWindow("Fyne的canvas")
imagess(w)
w.Resize(fyne.NewSize(600, 600)) // 窗口大小
w.ShowAndRun() // 渲染完毕
}
// 图片
func imagess(w fyne.Window) {
image := canvas.NewImageFromResource(theme.FyneLogo())
image.FillMode = canvas.ImageFillContain
w.SetContent(image)
}
六,折线图
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"image/color"
"math/rand"
"strconv"
)
func main() {
myApp := app.New()
w := myApp.NewWindow("Fyne的canvas")
zxLines(w)
w.Resize(fyne.NewSize(600, 600)) // 窗口大小
w.ShowAndRun() // 渲染完毕
}
// 折线图
func zxLines(w fyne.Window) {
data := genData(10, 100)
ly := container.NewWithoutLayout()
for i, d := range data {
// 线条
line := canvas.NewLine(genColor())
line.Position1 = fyne.NewPos(float32(100*i+10), 200) // 35-200 70-200
line.Position2 = fyne.NewPos(float32(100*i+10), d) // 35-随机 70-随机
line.StrokeWidth = 30
// 文字
text := canvas.NewText(strconv.FormatFloat(float64(200-d), 'f', 3, 64), color.Black)
text.Move(line.Position2)
text.TextSize = 15
text.Alignment = fyne.TextAlignCenter
ly.Objects = append(ly.Objects, line)
ly.Objects = append(ly.Objects, text)
}
for i := 0; i < len(data)-1; i++ {
// 一跳折线
linez := canvas.NewLine(genColor())
linez.Position1 = fyne.NewPos(float32(100*i+10), data[i]) // 35-200 70-200 起始坐标
linez.Position2 = fyne.NewPos(float32(100*(i+1)+10), data[i+1]) // 35-随机 70-随机 目标地点坐标
linez.StrokeWidth = 5
ly.Objects = append(ly.Objects, linez)
}
w.SetContent(ly)
}
// 生存随机坐标
func genData(count, max int) (data []float32) {
for i := 0; i < count; i++ {
data = append(data, float32(rand.Intn(max)))
}
return
}
// 生存随机颜色
func genColor() color.Color {
return color.RGBA{uint8(rand.Intn(256)), uint8(rand.Intn(256)), uint8(rand.Intn(256)), 255}
}
七, 随机小圆点 (保存成图片,刷新小圆点)
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"image"
"image/color"
"image/jpeg"
"math/rand"
"os"
"strconv"
"time"
)
func main() {
myApp := app.New()
w := myApp.NewWindow("Fyne的canvas")
scatter(w)
w.Resize(fyne.NewSize(600, 600)) // 窗口大小
w.ShowAndRun() // 渲染完毕
}
// 随机 小圆点
func scatter(w fyne.Window) {
c := container.NewWithoutLayout()
cdata := genData(20, 1000)
var r float32 = 15
ydata := genData(20, 600)
for i, v := range cdata {
c1 := canvas.NewCircle(genColor())
c1.Position1 = fyne.NewPos(v-r, ydata[i]-r)
c1.Position2 = fyne.NewPos(v+r, ydata[i]+r)
fmt.Println(i, v)
c.Objects = append(c.Objects, c1)
}
btn := widget.NewButton("image button", func() {
for i := 0; i < 10; i++ {
refreshPos(20, 1000, 600, c)
saveImage(w.Canvas().Capture(), "test"+strconv.Itoa(i)+".jpeg")
}
})
btnRefresh := widget.NewButtonWithIcon("", theme.ViewRefreshIcon(), func() {
refreshPos(20, 1000, 600, c)
})
w.SetContent(container.NewVBox(container.NewHBox(btn, btnRefresh), c))
}
func saveImage(img image.Image, path string) {
file, err := os.Create(path)
if err != nil {
fmt.Println("os.create err")
}
defer file.Close()
jpeg.Encode(file, img, nil)
fmt.Println("done")
}
func refreshPos(counts, maxX, maxY int, c *fyne.Container) {
cdata := genData(counts, maxX)
time.Sleep(time.Millisecond * 100)
ydata := genData(counts, maxY)
for i, v := range c.Objects {
v.Move(fyne.NewPos(cdata[i], ydata[i]))
}
}
// 生存随机坐标
func genData(count, max int) (data []float32) {
for i := 0; i < count; i++ {
data = append(data, float32(rand.Intn(max)))
}
return
}
// 生存随机颜色
func genColor() color.Color {
return color.RGBA{uint8(rand.Intn(256)), uint8(rand.Intn(256)), uint8(rand.Intn(256)), 255}
}
八,动态加载 柱状图
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"image/color"
"math/rand"
"time"
)
func main() {
myApp := app.New()
w := myApp.NewWindow("Fyne的canvas")
dtBar(w)
w.Resize(fyne.NewSize(600, 600)) // 窗口大小
w.ShowAndRun() // 渲染完毕
}
// 柱状图
func dtBar(w fyne.Window) {
xdata := genData(10, 100)
c := container.NewWithoutLayout()
for i, v := range xdata {
line := canvas.NewLine(genColor())
line.Position1 = fyne.NewPos(float32(100*i+10), 200) // 35-200 70-200
line.Position2 = fyne.NewPos(float32(100*i+10), v) // 35-随机 70-随机
line.StrokeWidth = 30
Bar(line)
c.Objects = append(c.Objects, line)
}
w.SetContent(c)
}
// 讲柱状图动态加载
func Bar(line *canvas.Line) {
go func() {
for i := 90; i > 10; i-- {
line.Position2 = fyne.NewPos(line.Position2.X, float32(i))
line.Refresh()
time.Sleep(time.Millisecond * 100)
}
}()
}
// 生存随机坐标
func genData(count, max int) (data []float32) {
for i := 0; i < count; i++ {
data = append(data, float32(rand.Intn(max)))
}
return
}
// 生存随机颜色
func genColor() color.Color {
return color.RGBA{uint8(rand.Intn(256)), uint8(rand.Intn(256)), uint8(rand.Intn(256)), 255}
}
发表评论