draw2d/cmd/testX11draw.go

59 lines
1.2 KiB
Go
Raw Normal View History

2011-04-27 08:06:14 +00:00
package main
import (
2011-09-29 07:46:16 +00:00
"exp/gui"
"exp/gui/x11"
2012-01-13 09:14:12 +00:00
"fmt"
2011-04-27 08:06:14 +00:00
"image"
"math"
2015-04-16 09:51:13 +00:00
"github.com/llgcode/draw2d/draw2d"
2011-04-27 08:06:14 +00:00
)
func main() {
window, err := x11.NewWindow()
if err != nil {
fmt.Printf("Cannot open an x11 window\n")
return
}
screen := window.Screen()
gc := draw2d.NewGraphicContext(screen)
gc.SetStrokeColor(image.Black)
gc.SetFillColor(image.White)
gc.Clear()
for i := 0.0; i < 360; i = i + 10 { // Go from 0 to 360 degrees in 10 degree steps
gc.BeginPath() // Start a new path
gc.Save() // Keep rotations temporary
gc.MoveTo(144, 144)
gc.Rotate(i * (math.Pi / 180.0)) // Rotate by degrees on stack from 'for'
gc.RLineTo(72, 0)
gc.Stroke()
gc.Restore() // Get back the unrotated state
}
window.FlushImage()
gc.SetLineWidth(3)
nbclick := 0
for {
switch evt := (<-window.EventChan()).(type) {
2011-09-29 07:46:16 +00:00
case gui.KeyEvent:
2011-04-27 08:06:14 +00:00
if evt.Key == 'q' {
window.Close()
}
2011-09-29 07:46:16 +00:00
case gui.MouseEvent:
2011-04-27 08:06:14 +00:00
if evt.Buttons&1 != 0 {
if nbclick%2 == 0 {
gc.MoveTo(float64(evt.Loc.X), float64(evt.Loc.Y))
} else {
gc.LineTo(float64(evt.Loc.X), float64(evt.Loc.Y))
gc.Stroke()
window.FlushImage()
}
nbclick = nbclick + 1
}
}
}
}