2013-02-27 85 views
0

下面的代碼應該使形狀閃爍兩次,我從根檢查方法三倍,我99%肯定它是那些方法是正確的(如果需要,我會發布該代碼)。在屏幕上暫停當前狀態幾秒鐘的最佳方式是什麼?處理代碼不起作用(線程,繪製(),noLoop()和循環())

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; 
+0

在完全不同的一面注意,您不需要使用'(long)'強制轉換'1500'。只需使用'L'而不是像'1500L'那樣。 L表示這是一個長文字。 – jluzwick 2013-02-27 05:06:28

回答

2

你只能有一個線程執行的繪圖,如果你果醬線程了sleep等,它會「掛起」,直到它得到一個機會來擺脫你的代碼,並返回到內呈現代碼JRE。 有很多關於它的教程,Google是你的朋友!

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

把它想成你畫到頁面上,並時不時的頁面被拔出你的筆記本的顯示。如果你花10秒鐘繪製一個圓圈,然後擦掉它,無所謂。重要的是顯示頁面上的內容。

+0

你建議我用什麼? – Runeony1 2013-02-27 02:40:53

+0

你不能去{draw();等待();畫();在一個方法中等待()...}。它只是不這樣工作。你需要研究用Java做動畫的正確方法。 – John3136 2013-02-27 03:58:46

1

我不確定我是否得到了您的問題,並且代碼無法運行,但是......也許您需要更簡單的方法?自己製作一個小計時器?事情就是draw()在draw()結束之前渲染一個幀之前執行所有指令。所以如果你停止繪製(),它會暫停,而不進行任何繪製,然後在最後繼續進行所有更改和繪製。我的意思是,如果我做的:

draw(){ 
fill(0); 
ellipse(10,10,10,10); 
delay(1000); 
fill(255,255,0); 
ellipse(10,10,10,10); 
} 

我將永遠不會看到黑色的橢圓形,因爲它是由黃色的覆蓋渲染之前發生......在平局結束。但程序會掛在每幀一秒...

所以也許一個簡單的計時器可以爲你做。這裏有一個計時器的一般示例,您可以嘗試適應您的需求:

PFont font; 
String time = "000"; 
int initialTime; 
int interval = 1000; 
int times; 
color c = color(200); 


void setup() { 
    size(300, 300); 
    font = createFont("Arial", 30); 
    background(255); 
    fill(0); 
    smooth(); 

    //set the timer as setup is done 
    initialTime = millis(); 
} 

void draw() 
{ 
    background(255); 

    //compare elapsed time if bigger than interval... 
    if (millis() - initialTime > interval) 
    { 
    //display the time 
    time = nf(int(millis()/1000), 3); 

    // reset timer 
    initialTime = millis(); 

    //increment times 
    times++; 
    } 

    // an arbitrary ammount 
    if (times == 3) { 

    //do somethng different 
    c = color(random(255), random(255), random(255)); 

    // reset times 
    times = 0; 
    } 

    //draw 
    fill(0); 
    text(time, width/2, height/2); 
    fill(c); 
    ellipse(75, 75, 30, 30); 
}