如何使用Go在X11中绘图

huangapple go评论133阅读模式
英文:

How to draw in X11 with Go

问题

我一直在研究Go语言中的drawdraw.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

我找到了答案,这里有一个简单的例子:

  1. package main
  2. import (
  3. "os"
  4. "time"
  5. "image"
  6. "exp/draw/x11"
  7. )
  8. func main() {
  9. win, _ := x11.NewWindow()
  10. color := image.RGBAColor{255, 255, 255, 255}
  11. img := win.Screen()
  12. for i, j := 0, 0; i < 100 && j < 100; i, j = i + 1, j + 1 {
  13. img.Set(i, j, color)
  14. }
  15. win.FlushImage()
  16. time.Sleep(10 * 1000 * 1000 * 1000)
  17. win.Close()
  18. os.Exit(0)
  19. }
英文:

I found myself the answer, here it goes a simple example:

  1. package main
  2. import (
  3. "os"
  4. "time"
  5. "image"
  6. "exp/draw/x11"
  7. )
  8. func main() {
  9. win, _ := x11.NewWindow()
  10. color := image.RGBAColor{255, 255, 255, 255}
  11. img := win.Screen()
  12. for i, j := 0, 0; i < 100 && j < 100; i, j = i + 1, j + 1 {
  13. img.Set(i, j, color)
  14. }
  15. win.FlushImage()
  16. time.Sleep(10 * 1000 * 1000 * 1000)
  17. win.Close()
  18. os.Exit(0)
  19. }

答案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)
}

  1. img :=win.Screen // 在这个新创建的屏幕上,你可以逐像素地绘制任何东西
  2. for i:=0;i<100;i++ { // 例如,这段代码在黑色的屏幕上绘制一个正方形
  3. for j:=0;j<100;j++ {
  4. img.Set(0+i,0+j,image.Black)
  5. }
  6. }
  7. win.FlushImage() // 刷新图像,然后才能绘制新的图像
  8. time.Sleep(time.Second*15) // 等待15秒

}

英文:
  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;code.google.ui/x11&quot; // i&#39;m not sure this is the actual package
  5. &quot;time&quot; // name u better refer the packages
  6. &quot;os&quot;
  7. )
  8. func main() {
  9. win,err := x11.NewWindowArea(600,600) // it creates a window with 600 width&amp;600
  10. if err != nil { // height
  11. fmt.Println(err)
  12. os.Exit(0) // if any err occurs it exits
  13. }
  14. img :=win.Screen // in this newly created screen u cn draw
  15. for i:=0;i&lt;100;i++ { // any thing pixel by pixel
  16. for j:=0;j&lt;100;j++ {
  17. img.Set(0+i,0+j,image.Black) // now this draws a square in the black
  18. } // color oo the created screen
  19. }
  20. win.FlushImage() // its for flushing the image then only new
  21. time.Sleep(time.Second*15) // image cn be draw
  22. }

huangapple
  • 本文由 发表于 2011年2月5日 21:42:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/4907149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定