小编asd*_*jkl的帖子

Clojure dothreads!功能

我正在阅读Fogus关于Clojure Joy的书,在并行编程章节中,我看到了一个函数定义,它肯定想要说明一些重要但我无法找到的东西.而且,我看不出这个函数是什么 - 当我执行时,它什么也没做:

(import '(java.util.concurrent Executors))
  (def *pool* (Executors/newFixedThreadPool
    (+ 2 (.availableProcessors (Runtime/getRuntime)))))

(defn dothreads! [f & {thread-count :threads
                       exec-count :times
                       :or {thread-count 1 exec-count 1}}]
  (dotimes [t thread-count]
    (.submit *pool* #(dotimes [_ exec-count] (f)))))
Run Code Online (Sandbox Code Playgroud)

我试图以这种方式运行:

(defn wait [] (Thread/sleep 1000))
(dothreads! wait :thread-count 10 :exec-count 10)
(dothreads! wait)
(dothreads! #(println "running"))
Run Code Online (Sandbox Code Playgroud)

......但它返回零.为什么?

clojure

4
推荐指数
1
解决办法
700
查看次数

Java Swing - 闪烁的Canvas图形

我必须编写一个简单的Java应用程序,它可以加载图片,以GUI形式显示,允许用户应用一些转换,并显示转换后的图片.我的解决方案工作正常,但UI有点闪烁,因为重绘方法调用太频繁(例如当用户使用JSlider缩放图像时)

我的代码看起来像这样:

public class ImageCanvas extends Canvas
{
    private BufferedImage image;
    // ...

    @Override
    public void paint(Graphics g) 
    {
        Graphics2D g2d = (Graphics2D) g;
        if(image != null)
        {
             // I draw out the image...
        }
    }

    public void setImage(BufferedImage image)
    {
        this.image = image;
        this.repaint();
    }

    public void setRotation(double rotation)
    {
        this.rotation = rotation;
        this.repaint();
    }

    public void setScale(double scaleX, double scaleY) 
    { 
       //set the scaling field, then repaint ....
    }    

    // and so on...
}
Run Code Online (Sandbox Code Playgroud)

当然,我在我的主UI上有一个ImageCanvas控件,我只是调用公共方法(参见上面的"setRotation"方法),它重绘了画布区域.我知道这是一个简单的问题,但我甚至没有在Canvas上找到DoubleBuffered属性......

任何帮助赞赏.

java swing canvas repaint

3
推荐指数
1
解决办法
3551
查看次数

标签 统计

canvas ×1

clojure ×1

java ×1

repaint ×1

swing ×1