英文:
How to draw in X11 with Go
问题
我一直在研究Go语言中的draw和draw.x11包。我没有找到在X11窗口上绘制直线的简单方法。
我在哪里可以找到一些简单的2D绘图示例?
英文:
I've been looking into draw and draw.x11 packages that come with Go. I didn't find out a simple way to draw a line on a X11 window.
Where I can find some simple 2D drawing examples?
答案1
得分: 4
我找到了答案,这里有一个简单的例子:
package main
import (
	"os"
	"time"
	"image"
	"exp/draw/x11"
)
func main() {
	win, _ := x11.NewWindow()
	color := image.RGBAColor{255, 255, 255, 255}
	img := win.Screen()
	for i, j := 0, 0; i < 100 && j < 100; i, j = i + 1, j + 1 {
		img.Set(i, j, color)
	}
	win.FlushImage()
	time.Sleep(10 * 1000 * 1000 * 1000)
	win.Close()
	os.Exit(0)
}
英文:
I found myself the answer, here it goes a simple example:
package main
import (
	"os"
	"time"
	"image"
	"exp/draw/x11"
)
func main() {
	win, _ := x11.NewWindow()
	color := image.RGBAColor{255, 255, 255, 255}
	img := win.Screen()
	for i, j := 0, 0; i < 100 && j < 100; i, j = i + 1, j + 1 {
		img.Set(i, j, color)
	}
	win.FlushImage()
	time.Sleep(10 * 1000 * 1000 * 1000)
	win.Close()
	os.Exit(0)
}
答案2
得分: 3
虽然你的解决方案有效,但我认为你真正需要的是X Go Binding。
英文:
While your solution works, I think what you're really looking for is X Go Binding
答案3
得分: 1
package main
import (
"fmt"
"code.google.ui/x11"    // 我不确定这是实际的包名
"time"                  // 最好参考包的名称
"os"
)
func main() {
win,err := x11.NewWindowArea(600,600)  // 创建一个宽度为600、高度为600的窗口
if err != nil {                        // 如果有错误发生,则退出程序
fmt.Println(err)
os.Exit(0)
}
img :=win.Screen                       // 在这个新创建的屏幕上,你可以逐像素地绘制任何东西
for i:=0;i<100;i++ {                   // 例如,这段代码在黑色的屏幕上绘制一个正方形
    for j:=0;j<100;j++ {
        img.Set(0+i,0+j,image.Black)
    }
}
win.FlushImage()                       // 刷新图像,然后才能绘制新的图像
time.Sleep(time.Second*15)             // 等待15秒
}
英文:
package main
import (    
    "fmt"
    "code.google.ui/x11"    // i'm not sure this is the actual package 
    "time"                  // name u better refer the packages
    "os"
)
func main() {
    win,err := x11.NewWindowArea(600,600)  // it creates a window with 600 width&600
    if err != nil {                        // height
        fmt.Println(err)  
        os.Exit(0)                         // if any err occurs it exits
    } 
    img :=win.Screen                       // in this newly created screen u cn draw
    for i:=0;i<100;i++ {                   // any thing pixel by pixel 
        for j:=0;j<100;j++ {
            img.Set(0+i,0+j,image.Black)   // now this draws a square in the black
        }                                  // color oo the created screen
    }
    win.FlushImage()                       // its for flushing the image then only new 
    time.Sleep(time.Second*15)             // image cn be draw
}                                                     
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论