处理代码不起作用(线程、draw()、noLoop() 和 loop()) [英] Processing code doesn't work (Threads, draw(), noLoop(), and loop())

查看:37
本文介绍了处理代码不起作用(线程、draw()、noLoop() 和 loop())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码应该使形状闪烁两次,我从根目录检查了三次方法,我 99% 确定这些方法是正确的(如果需要,我会发布该代码).使当前 root 状态在屏幕上暂停几秒钟的最佳方法是什么?

The code below should make a shape flash twice, I triple checked the methods from root and I'm 99% sure it's that those methods are correct (I will post that code if needed though). What's the best way to make the current state of root pause on screen for a few seconds?

    noLoop();      
root.setVal(newVal);
root.highlight(0,255,0);
root.setopacity(200);
redraw();
try {Thread.sleep((long)1500);} 
catch (InterruptedException ex) {println("Error!");}
root.setopacity(0);
redraw();
try {Thread.sleep((long)1500);} 
catch (InterruptedException ex) {println("Error!");}
root.setopacity(200);
root.clearHL();//just to make sure I repeated these methods
root.highlight(0,255,0);
redraw();
try {Thread.sleep((long)1500);} 
catch (InterruptedException ex) {println("Error!");}
root.clearHL();
redraw();
loop();
return root;

推荐答案

你只能让一个线程进行绘图,如果你用 sleep 等阻塞了那个线程,它会挂起"直到它有机会退出您的代码并返回到在 JRE 中呈现代码.有很多关于它的教程,谷歌是你的朋友!

You can only have one thread doing the drawing, and if you jam that thread up with sleep etc, it will "hang" until it gets a chance to get out of your code and back to rendering code inside the JRE. There are plenty of tutorials around about it, Google is your friend!

例如:http://www.java-tips.org/java-se-tips/java.awt/how-to-create-animation-paint-and-thread.html

想象一下,当您在页面上绘图时,页面会时不时地从笔记本中拉出以显示出来.如果你花10秒钟画一个圆圈,然后擦掉它,那也没关系.重要的是页面上显示的内容.

Think of it as you drawing onto a page, and every now and then the page is pulled out of your notebook to be displayed. Doesn't matter if you take 10 seconds to draw a circle, then rub it out. All that matters is what is on the page when it gets displayed.

这篇关于处理代码不起作用(线程、draw()、noLoop() 和 loop())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆