2010-12-06 11:44:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"bufio"
|
|
|
|
"time"
|
2010-12-06 17:18:39 +00:00
|
|
|
|
2010-12-06 11:44:36 +00:00
|
|
|
"math"
|
|
|
|
"image"
|
|
|
|
"image/png"
|
2011-03-17 22:32:06 +00:00
|
|
|
"draw2d.googlecode.com/hg/draw2d"
|
2010-12-06 11:44:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2010-12-06 13:57:26 +00:00
|
|
|
width, height = 178, 224
|
2010-12-06 11:44:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
lastTime int64
|
2011-03-17 22:32:06 +00:00
|
|
|
folder = "../resource/result/"
|
2010-12-06 11:44:36 +00:00
|
|
|
)
|
|
|
|
|
2011-03-20 21:31:54 +00:00
|
|
|
func initGc(w, h int) (image.Image, draw2d.GraphicContext) {
|
2010-12-06 11:44:36 +00:00
|
|
|
i := image.NewRGBA(w, h)
|
2011-03-20 21:31:54 +00:00
|
|
|
gc := draw2d.NewImageGraphicContext(i)
|
2010-12-06 11:44:36 +00:00
|
|
|
lastTime = time.Nanoseconds()
|
|
|
|
|
|
|
|
gc.SetStrokeColor(image.Black)
|
|
|
|
gc.SetFillColor(image.White)
|
|
|
|
// fill the background
|
|
|
|
//gc.Clear()
|
|
|
|
|
|
|
|
return i, gc
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveToPngFile(TestName string, m image.Image) {
|
|
|
|
dt := time.Nanoseconds() - lastTime
|
2011-02-02 21:38:17 +00:00
|
|
|
fmt.Printf("%s during: %f ms\n", TestName, float64(dt)*10e-6)
|
2010-12-06 11:44:36 +00:00
|
|
|
filePath := folder + TestName + ".png"
|
|
|
|
f, err := os.Open(filePath, os.O_CREAT|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
b := bufio.NewWriter(f)
|
|
|
|
err = png.Encode(b, m)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err = b.Flush()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Printf("Wrote %s OK.\n", filePath)
|
|
|
|
}
|
|
|
|
|
2011-03-20 21:31:54 +00:00
|
|
|
func android(gc draw2d.GraphicContext, x, y float64) {
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.SetLineCap(draw2d.RoundCap)
|
|
|
|
gc.SetLineWidth(5)
|
2010-12-06 17:18:39 +00:00
|
|
|
gc.ArcTo(x+80, y+70, 50, 50, 180*(math.Pi/180), 360*(math.Pi/180)) // head
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.FillStroke()
|
|
|
|
gc.MoveTo(x+60, y+25)
|
|
|
|
gc.LineTo(x+50, y+10)
|
|
|
|
gc.MoveTo(x+100, y+25)
|
2010-12-06 17:18:39 +00:00
|
|
|
gc.LineTo(x+110, y+10)
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.Stroke()
|
2010-12-06 17:18:39 +00:00
|
|
|
draw2d.Circle(gc, x+60, y+45, 5) // left eye
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.FillStroke()
|
2010-12-06 17:18:39 +00:00
|
|
|
draw2d.Circle(gc, x+100, y+45, 5) // right eye
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.FillStroke()
|
2010-12-06 17:18:39 +00:00
|
|
|
draw2d.RoundRect(gc, x+30, y+75, x+30+100, y+75+90, 10, 10) // body
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.FillStroke()
|
2010-12-06 12:11:57 +00:00
|
|
|
draw2d.Rect(gc, x+30, y+75, x+30+100, y+75+80)
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.FillStroke()
|
2010-12-06 17:18:39 +00:00
|
|
|
draw2d.RoundRect(gc, x+5, y+80, x+5+20, y+80+70, 10, 10) // left arm
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.FillStroke()
|
2010-12-06 12:11:57 +00:00
|
|
|
draw2d.RoundRect(gc, x+135, y+80, x+135+20, y+80+70, 10, 10) // right arm
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.FillStroke()
|
2010-12-06 12:11:57 +00:00
|
|
|
draw2d.RoundRect(gc, x+50, y+150, x+50+20, y+150+50, 10, 10) // left leg
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.FillStroke()
|
2010-12-06 12:11:57 +00:00
|
|
|
draw2d.RoundRect(gc, x+90, y+150, x+90+20, y+150+50, 10, 10) // right leg
|
2010-12-06 11:44:36 +00:00
|
|
|
gc.FillStroke()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
i, gc := initGc(width, height)
|
2010-12-06 13:57:26 +00:00
|
|
|
gc.SetFillColor(image.RGBAColor{0xff, 0x44, 0x44, 0xff})
|
|
|
|
gc.SetStrokeColor(image.RGBAColor{0x44, 0x44, 0x44, 0xff})
|
|
|
|
android(gc, 10, 10)
|
2010-12-06 11:44:36 +00:00
|
|
|
saveToPngFile("TestAndroid", i)
|
|
|
|
}
|