draw2d/cmd/testX11draw.go

58 lines
1.2 KiB
Go
Raw Normal View History

2010-11-28 22:29:34 +00:00
package main
import (
"fmt"
"exp/draw"
"exp/draw/x11"
"image"
"math"
"draw2d.googlecode.com/hg/draw2d"
2010-11-28 22:29:34 +00:00
)
func main() {
window, err := x11.NewWindow()
2010-12-06 17:18:39 +00:00
if err != nil {
2010-11-28 22:29:34 +00:00
fmt.Printf("Cannot open an x11 window\n")
return
}
screen := window.Screen()
2011-03-22 20:23:03 +00:00
gc := draw2d.NewImageGraphicContext(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
}
2010-12-06 17:18:39 +00:00
2011-03-22 20:23:03 +00:00
window.FlushImage()
2010-12-06 17:18:39 +00:00
2011-03-22 20:23:03 +00:00
gc.SetLineWidth(3)
nbclick := 0
for {
2010-12-06 17:18:39 +00:00
2011-03-22 20:23:03 +00:00
switch evt := (<-window.EventChan()).(type) {
case draw.KeyEvent:
if evt.Key == 'q' {
window.Close()
}
case draw.MouseEvent:
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()
2010-11-28 22:29:34 +00:00
}
2011-03-22 20:23:03 +00:00
nbclick = nbclick + 1
2010-11-28 22:29:34 +00:00
}
}
}
2010-12-06 17:18:39 +00:00
}