0

To simply the question here, for the following code, after mouse clicking, instead of executing the code one line by another, the following happens: 1. program paused for 1 second; 2. GLabel "CLICKED" and GLabel "PAUSE FINISHED" are added to canvas at the same time after the pause.

Could someone explain this for me? Thank you very much.

public void mouseClicked(MouseEvent e){
    add(new GLabel("CLICKED"),200,200);
    pause(1000);
    add(new GLabel("PAUSE FINISHED"),200,300);
}

While this code would work as expected:

public void run(){

    add(new GLabel("CLICKED"),20,20);
    pause(1000);
    add(new GLabel("PAUSE FINISHED"),20,50);
}
jren
  • 33
  • 1
  • 1
  • 5
  • 1
    ...What is `PAUSE_TIME`? That's likely the cause of your long wait. Better yet, where does `pause` come from? – Makoto May 22 '15 at 20:27
  • PAUSE_TIME is just a constant defined in another part of codes (which is set to 500); – jren May 22 '15 at 20:43
  • I'm not sure what do you mean with "pause come from", I'm quite new to java and following the book The art and science of Java. So I'm basically using the acm package. I think the pause method is from the package, is that right? – jren May 22 '15 at 20:45
  • I reckon so; I was at least able to Google the Javadoc for it. So right now that code states that it'll pause for half a second. Is that pause too long? Have you tried adjusting it to something smaller to see if that's the root cause? – Makoto May 22 '15 at 20:47
  • Yes, I have tried. Because the if(){} part calls the lightTower(), so in my run, the lightTower() method is executed for 4 times, so is the pause(). So the problem is that everything paused for about 2s after I click the mouse, and then everything gets released and happens at the same time. Do you think it's because that the lightTower() is being called inside lightTower() itself? – jren May 22 '15 at 20:52
  • It's probably pausing the GUI event thread, which is generally not the right thing to do. In Swing, for example, you want to schedule and event to occur 500 ms. later and not block the Swing event thread. – BillRobertson42 May 22 '15 at 21:17
  • Well, I tried deleting the if sentence. Still, the add(new GLabel("CLICKED"),200,200) is executed after the pause time. – jren May 22 '15 at 21:18
  • If it is pausing the GUI event thread, what should I do here instead? Thanks! – jren May 22 '15 at 21:21
  • most GUI frameworks has background workers. but it is also possible to make own with https://www.google.com/search?q=executor+service+java – msangel May 22 '15 at 21:27

2 Answers2

0

Try using handler to run the pause on next frame: like this

public void mouseClicked(MouseEvent e){
    add(new GLabel("CLICKED"), 200, 200);
    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            pause(1000);
            add(new GLabel("PAUSE FINISHED"), 200, 300);
        }
    }, 1);
}
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • I would like to try this, but could you tell me which imports I should have for this "Handler"? And also could you help explain what causes the mistakes in my code? Thank you very much! – jren May 22 '15 at 22:47
  • @jren its android handler, I missed the part that you are speaking only about Java. While I believe that it can fix your problem, its only a guess because your question is not very clear(Actually you forgot to ask it). And I don't know what pause() do. Any way, you are definitely having some synchronization problem. The question is where? – Ilya Gazman May 22 '15 at 23:40
  • Sorry about the confusion here. I guess my question here is that why the same codes give different results (specifically the time pause() is executed ) when one is in **public void run()** and the other is in **public void mouseClicked(MouseEvent e)**. I'm looking for a simple method to pause my program for a while to make animation. – jren May 23 '15 at 04:09
0

Finally this solved my problem (Thanks @Charles Goodwin for his answer to the question: Java MouseEvent, check if pressed down)

public void mouseClicked(MouseEvent e) {
    x=e.getX();
    y=e.getY();
    if(getElementAt(x,y)==null) return;
    currentTower = (GCompound) getElementAt(x,y);
    initThread();
}


private void initThread() {
    new Thread() {
        public void run() {
            ((SignalTower) currentTower).lightTower();
        }
    }.start();
 }

public void lightTower(){
    beacon.setFillColor(Color.red);
    beacon.setFilled(true);
    pause(500);
    beacon.setFilled(false);
    if(this.next!=null) this.next.lightTower();

}
Community
  • 1
  • 1
jren
  • 33
  • 1
  • 1
  • 5