Move samples to draw2d.samples

This commit is contained in:
Laurent Le Goff 2015-04-22 15:58:13 +02:00
parent b7cc153384
commit 78e0449cca
6 changed files with 0 additions and 429 deletions

View file

@ -1,27 +0,0 @@
#include $(GOROOT)/src/Make.inc
all: install test
install:
go install ./...
build:
go build ./...
test:
cd cmd && go build draw2dgl.go
cd cmd && go build gettingStarted.go
cd cmd && go build testandroid.go
cd cmd && go build testdraw2d.go
cd cmd && go build testgopher.go
cd cmd && go build testimage.go
#cd cmd && go build testX11draw.go
clean:
go clean ./...
# cd wingui && make clean
fmt:
gofmt -w .

View file

@ -1,47 +0,0 @@
// Copyright 2010 The draw2d Authors. All rights reserved.
// created: 21/11/2010 by Laurent Le Goff
// +build ignore
package main
import (
"bufio"
"fmt"
"image"
"image/png"
"log"
"os"
"github.com/llgcode/draw2d"
)
func saveToPngFile(filePath string, m image.Image) {
f, err := os.Create(filePath)
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)
}
func main() {
i := image.NewRGBA(image.Rect(0, 0, 200, 200))
gc := draw2d.NewGraphicContext(i)
gc.MoveTo(10.0, 10.0)
gc.LineTo(100.0, 10.0)
gc.Stroke()
saveToPngFile("TestPath.png", i)
}

View file

@ -1,60 +0,0 @@
// +build ignore
package main
import (
"exp/gui"
"exp/gui/x11"
"fmt"
"image"
"math"
"github.com/llgcode/draw2d"
)
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) {
case gui.KeyEvent:
if evt.Key == 'q' {
window.Close()
}
case gui.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()
}
nbclick = nbclick + 1
}
}
}
}

View file

@ -1,95 +0,0 @@
// +build ignore
package main
import (
"bufio"
"fmt"
"image"
"image/color"
"image/png"
"log"
"math"
"os"
"github.com/llgcode/draw2d"
)
const (
width, height = 178, 224
)
var (
lastTime int64
folder = "../resource/result/"
)
func initGc(w, h int) (image.Image, draw2d.GraphicContext) {
i := image.NewRGBA(image.Rect(0, 0, w, h))
gc := draw2d.NewGraphicContext(i)
gc.SetStrokeColor(image.Black)
gc.SetFillColor(image.White)
// fill the background
//gc.Clear()
return i, gc
}
func saveToPngFile(TestName string, m image.Image) {
filePath := folder + TestName + ".png"
f, err := os.Create(filePath)
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)
}
func android(gc draw2d.GraphicContext, x, y float64) {
gc.SetLineCap(draw2d.RoundCap)
gc.SetLineWidth(5)
gc.ArcTo(x+80, y+70, 50, 50, 180*(math.Pi/180), 360*(math.Pi/180)) // head
gc.FillStroke()
gc.MoveTo(x+60, y+25)
gc.LineTo(x+50, y+10)
gc.MoveTo(x+100, y+25)
gc.LineTo(x+110, y+10)
gc.Stroke()
draw2d.Circle(gc, x+60, y+45, 5) // left eye
gc.FillStroke()
draw2d.Circle(gc, x+100, y+45, 5) // right eye
gc.FillStroke()
draw2d.RoundRect(gc, x+30, y+75, x+30+100, y+75+90, 10, 10) // body
gc.FillStroke()
draw2d.Rect(gc, x+30, y+75, x+30+100, y+75+80)
gc.FillStroke()
draw2d.RoundRect(gc, x+5, y+80, x+5+20, y+80+70, 10, 10) // left arm
gc.FillStroke()
draw2d.RoundRect(gc, x+135, y+80, x+135+20, y+80+70, 10, 10) // right arm
gc.FillStroke()
draw2d.RoundRect(gc, x+50, y+150, x+50+20, y+150+50, 10, 10) // left leg
gc.FillStroke()
draw2d.RoundRect(gc, x+90, y+150, x+90+20, y+150+50, 10, 10) // right leg
gc.FillStroke()
}
func main() {
i, gc := initGc(width, height)
gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
android(gc, 10, 10)
saveToPngFile("TestAndroid", i)
}

View file

@ -1,132 +0,0 @@
// +build ignore
package main
import (
"bufio"
"fmt"
"image"
"image/color"
"image/png"
"log"
"math"
"os"
"github.com/llgcode/draw2d"
)
const (
width, height = 300, 200
)
var (
lastTime int64
folder = "../resource/result/"
)
func initGc(w, h int) (image.Image, draw2d.GraphicContext) {
i := image.NewRGBA(image.Rect(0, 0, w, h))
gc := draw2d.NewGraphicContext(i)
gc.SetStrokeColor(image.Black)
gc.SetFillColor(image.White)
// fill the background
//gc.Clear()
return i, gc
}
func saveToPngFile(TestName string, m image.Image) {
filePath := folder + TestName + ".png"
f, err := os.Create(filePath)
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)
}
func gordon(gc draw2d.GraphicContext, x, y, w, h float64) {
h23 := (h * 2) / 3
blf := color.RGBA{0, 0, 0, 0xff}
wf := color.RGBA{0xff, 0xff, 0xff, 0xff}
nf := color.RGBA{0x8B, 0x45, 0x13, 0xff}
brf := color.RGBA{0x8B, 0x45, 0x13, 0x99}
brb := color.RGBA{0x8B, 0x45, 0x13, 0xBB}
gc.MoveTo(x, y+h)
gc.CubicCurveTo(x, y+h, x+w/2, y-h, x+w, y+h)
gc.Close()
gc.SetFillColor(brb)
gc.Fill()
draw2d.RoundRect(gc, x, y+h, x+w, y+h+h, 10, 10)
gc.Fill()
draw2d.Circle(gc, x, y+h, w/12) // left ear
gc.SetFillColor(brf)
gc.Fill()
draw2d.Circle(gc, x, y+h, w/12-10)
gc.SetFillColor(nf)
gc.Fill()
draw2d.Circle(gc, x+w, y+h, w/12) // right ear
gc.SetFillColor(brf)
gc.Fill()
draw2d.Circle(gc, x+w, y+h, w/12-10)
gc.SetFillColor(nf)
gc.Fill()
draw2d.Circle(gc, x+w/3, y+h23, w/9) // left eye
gc.SetFillColor(wf)
gc.Fill()
draw2d.Circle(gc, x+w/3+10, y+h23, w/10-10)
gc.SetFillColor(blf)
gc.Fill()
draw2d.Circle(gc, x+w/3+15, y+h23, 5)
gc.SetFillColor(wf)
gc.Fill()
draw2d.Circle(gc, x+w-w/3, y+h23, w/9) // right eye
gc.Fill()
draw2d.Circle(gc, x+w-w/3+10, y+h23, w/10-10)
gc.SetFillColor(blf)
gc.Fill()
draw2d.Circle(gc, x+w-(w/3)+15, y+h23, 5)
gc.SetFillColor(wf)
gc.Fill()
gc.SetFillColor(wf)
draw2d.RoundRect(gc, x+w/2-w/8, y+h+30, x+w/2-w/8+w/8, y+h+30+w/6, 5, 5) // left tooth
gc.Fill()
draw2d.RoundRect(gc, x+w/2, y+h+30, x+w/2+w/8, y+h+30+w/6, 5, 5) // right tooth
gc.Fill()
draw2d.Ellipse(gc, x+(w/2), y+h+30, w/6, w/12) // snout
gc.SetFillColor(nf)
gc.Fill()
draw2d.Ellipse(gc, x+(w/2), y+h+10, w/10, w/12) // nose
gc.SetFillColor(blf)
gc.Fill()
}
func main() {
i, gc := initGc(width, height)
gc.Clear()
gc.Translate(-75, 58)
gc.Rotate(-30 * (math.Pi / 180.0))
gordon(gc, 48, 48, 240, 72)
saveToPngFile("TestGopher", i)
}

View file

@ -1,68 +0,0 @@
// +build ignore
package main
import (
"bufio"
"fmt"
"image"
"image/draw"
"image/png"
"log"
"math"
"os"
"github.com/llgcode/draw2d"
)
func saveToPngFile(filePath string, m image.Image) {
f, err := os.Create(filePath)
if err != nil {
log.Println(err)
return
}
defer f.Close()
b := bufio.NewWriter(f)
err = png.Encode(b, m)
if err != nil {
log.Println(err)
return
}
err = b.Flush()
if err != nil {
log.Println(err)
return
}
fmt.Printf("Wrote %s OK.\n", filePath)
}
func loadFromPngFile(filePath string) image.Image {
f, err := os.OpenFile(filePath, 0, 0)
if f == nil {
log.Printf("can't open file; err=%s\n", err)
return nil
}
defer f.Close()
b := bufio.NewReader(f)
i, err := png.Decode(b)
if err != nil {
log.Println(err)
os.Exit(1)
}
fmt.Printf("Read %s OK.\n", filePath)
return i
}
func main() {
source := loadFromPngFile("../resource/image/TestAndroid.png")
dest := image.NewRGBA(image.Rect(0, 0, 1024, 768))
width, height := float64(source.Bounds().Dx()), float64(source.Bounds().Dy())
tr := draw2d.NewIdentityMatrix()
tr.Translate(width/2, height/2)
tr.Rotate(30 * math.Pi / 180)
//tr.Scale(3, 3)
tr.Translate(-width/2, -height/2)
tr.Translate(200, 5)
draw2d.DrawImage(source, dest, tr, draw.Over, draw2d.BilinearFilter)
saveToPngFile("../resource/result/TestDrawImage.png", dest)
}