javascript to java communications

I hava an applet that has a timer. I would like to pause the timer when the browser is minimized and restart it when the browser is opened again. To do this I have a javascript that notices when the browser is minimized/opened and then calls minimized()/opened() in my applet. The problem that I'm having is when the user clicks on the applet the javascript doesn't notice when it is being minimized/reopened anymore. Does anyone have any ideas on how I could do this? The javascript is listed below:

<html>

<head>

<title>MyApp</title>

</head>

<body>

<applet code="Main.class" archive="MyApp.jar" width="500" height="450" name ="MyApp">

</applet>

<script language="javascript">

window.onfocus = function(){ setTimeout("CheckWindowState()",1000);}

window.onblur = function(){ setTimeout("CheckWindowState()",1000);}

var c=false;

function CheckWindowState(){

var b=document.applets["MyApp"];

if (window.screenTop<=0){

b.minimized();

c =true;

}

if ((window.screenTop>0) && (c ==true)){

b.opened();

c =false;

}

}

</script>

</body>

</html>

Thanks,

Geo

[1994 byte] By [geokria] at [2007-9-27 23:37:59]
# 1

> I hava an applet that has a timer. I would like to

> pause the timer when the browser is minimized and

> restart it when the browser is opened again.

You don't even need to do that. Why don't you use isShowing() for your applet withing your run loop? If it is not showing, it means the browser is minimized. Then you can pause the timer. And once the browser is restored applets start method will be called. Overwrite start to check if your timer has been paused, if it has start again.

This is the basic idea. But I am not sure if any other window somehow blocks the browser that has the applet and causes for isShowing return false. Then start will never be called. But I guess it is worth to try either case.

ibosana at 2007-7-7 16:08:49 > top of Java-index,Other Topics,Java Game Development...
# 2

Thanks for the reply. I tried the isShowing() and it returned true no matter if the window was iconified or not. I also tried running the applet as an application and the isShowing() again returned true when iconified. When running as an applet the start() routine was only called once, at the start of the run. It was not called when the browser was opened after being iconified. I must be missing something here.

geokria at 2007-7-7 16:08:49 > top of Java-index,Other Topics,Java Game Development...
# 3

> Thanks for the reply. I tried the isShowing() and it

> returned true no matter if the window was iconified or

> not. I also tried running the applet as an

> application and the isShowing() again returned true

> when iconified. When running as an applet the start()

> routine was only called once, at the start of the run.

> It was not called when the browser was opened after

> being iconified. I must be missing something here.

Yes, I tried it too. Previous sugestion worked only with appletviewer. I mean the start method being called. Sorry for misleading you. I feel obliged to find an answer for you now. I remember once though there was this concept called live connection which allowed an applet to talk to the browser. But that required browser specific java files to be added to your applet. Not a good solution. I'll have something for you by the end of today.

ibosana at 2007-7-7 16:08:49 > top of Java-index,Other Topics,Java Game Development...
# 4

I tell you what is going: When you click on the applet, the window looses its focus causing onBlur to be called. So what you have to do in your applet is to check the focus, if you have it don't stop the timer. I did a simple test with jdk 1.1. Here are the html code and java code which keeps incrementing a value and displays it while thread is running:

<html>

<head>

<script language="JavaScript">

function res(){document.applets["app"].resume()}

function sus(){document.applets["app"].suspend()}

</script>

</head>

<body onFocus="res()" onBlur="sus()">

<applet name="app" code="Ball.class" width=100 height=100></applet>

</body>

</html>

import java.awt.event.*;

import java.applet.Applet;

import java.awt.*;

public class Ball extends Applet implements Runnable, MouseListener

{

Thread game;

int y;

boolean hasFocus=true;

public void init()

{

addMouseListener(this);

(game = new Thread(this)).start();

}

public void resume()

{

game.resume();

}

public void suspend()

{

if (!hasFocus)game.suspend();

}

public void run()

{

Thread me = Thread.currentThread();

while(game == me)

{

y++;

repaint();

try{Thread.sleep(50);}catch(Exception e){}

}

}

public void paint(Graphics g)

{

g.drawString(""+y, 20, 40);

}

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e){hasFocus = true;}

public void mouseExited(MouseEvent e) {hasFocus = false;}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

ibosana at 2007-7-7 16:08:49 > top of Java-index,Other Topics,Java Game Development...
# 5

Actually it is more complicated than that. It is a hard problem actually. Focus problems. If you click on applet right. OnBlur is called. But if you click outside to another window OnBlur is not going to be called again. Because it had never had the focus then. So I modified the last code & html. It is easier to test with 2 windows.

This following code ensures thread stops when you leave the applet window as well as minimized. It will start again one the window is reactivated.

But make sure you click on the applet when it first starts...

Hopefully this is it...:)

<html>

<head>

<script language="JavaScript">

function res(){ document.applets["app"].setFocus() }

function sus(){ document.applets["app"].unsetFocus()}

</script>

</head>

<body onFocus="res()" onBlur="sus()">

<applet name="app" code="Ball.class" width=300 height=100></applet>

</body>

</html>

import java.awt.event.*;

import java.applet.Applet;

import java.awt.*;

public class Ball extends Applet implements Runnable, FocusListener

{

Thread game;

int y;

// focus on applet

boolean hasFocus=true;

// focus on window

boolean pHasFocus=true;

public void init()

{

addFocusListener(this);

(game = new Thread(this)).start();

}

// for parent window to notify its focus

public void setFocus()

{

pHasFocus = true;

game.resume();

}

public void unsetFocus()

{

pHasFocus = false;

}

public void run()

{

Thread me = Thread.currentThread();

while(game == me)

{

y++;

Graphics g = getGraphics();

if (g!=null) paint(g);

// stop only when both doesn't has focus

if (!hasFocus && !pHasFocus)game.suspend();

try{Thread.sleep(100);}catch(Exception e){}

}

}

public void paint(Graphics g)

{

g.clearRect(0,0, getSize().width, getSize().height);

g.drawString(""+y + "applet has focus: " + hasFocus +

"window has focus: " + pHasFocus, 20, 40);

}

public void focusGained(FocusEvent e)

{

hasFocus=true;

game.resume();

}

public void focusLost(FocusEvent e)

{

hasFocus=false;

}

}

,ibosan

ibosana at 2007-7-7 16:08:49 > top of Java-index,Other Topics,Java Game Development...
# 6
Thankyou so much for the help. I was able to get it working.
geokria at 2007-7-7 16:08:49 > top of Java-index,Other Topics,Java Game Development...