File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Java Screensaver! ... almost ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Screensaver! ... almost ..." Watch "Java Screensaver! ... almost ..." New topic
Author

Java Screensaver! ... almost ...

Dan Temple
Ranch Hand

Joined: Jul 10, 2001
Posts: 93
Hello all you Java Wizards!
I found a very interesting site, http://kevinkelley.mystarband.net/java/sava.html, which allows you to write screen savers in java on Windows platforms. It uses compiled native code to invoke the java virtual machine, consults the control.ini to determine which class to load and run using that virtual machine. I was overjoyed since this is exactly what I was looking for, for a project I am working on. I tried it out, choose the native code as a screensaver within the control panel -> display, and it indeed worked! However I noticed that though it worked about 75% of the time, it would cease working after a while. Hitting-alt-ctrl-delete I saw that there were several instances of Java tasks running. I consulted the java code for the classes and saw that it was using the now deprecated stop() method. So I wrote my own classes to be called by the compiled native code. And this worked too! Except again it only worked 75% of the time and would cease to work after a bit. Hitting-alt-ctrl-delete again there were several instances of Java tasks running. Consequently I think one of two things are occurring:
1) Despite the fact that there are only two threads running in my java code (a main thread and a worker thread) somehow there is a deadlock, hence the java tasks that continue to run and somehow inhibit further java program starts by the native method. I�m not sure how this would be possible, but it is consistent with the �usually works, but sometimes doesn�t� property of my java code. However there is no Hanging of the GUI or anything that is run.
2) The problem lies not in the java code but with the compiled native code and somehow in the invocation API the DestroyJava function is failing to execute. This would explain why there is no Hanging of the GUI, but only Java tasks that fail to terminate. However I don�t know why the Java tasks would only fail to terminate sometimes.
I really, REALLY hope it is the first possibility (but I think it is number 2). I don�t know any C++ or C, and I really don�t want to spend 6 months learning it just to write some native code that will run a JVM as if it were a screen saver.
I tried to contact Mr Kevin Kelley, but the email on the site is no longer working. Does anyone have any ideas as to what I should do here? I have included my java code in the hopes that the flaw lies within it. I have not included the native code, since I only have the compiled native code ... unfortunately.
My Java classes are quite simple. A small window with a panel is created. There is a JTextField in the Panel, and a custom event source (Counter) starts looping in a for loop, and each loop number is passed to the JtextField so that the Panel begins counting up. A MouseMove over the Panel informs the Counting thread to cease, dispose methods are called and the thread exits. Here are the classes
----------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

public class DanScreen
{
public static void main(String args[])
{

Frame frame=new Frame("Dan's Mad Project");
frame.setSize(200,200);
SaverWindow dan=new SaverWindow(frame);
dan.show();
frame.show();
}
}
---------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
public class SaverWindow extends Window implements Runnable
{
private Frame frame;
private Counter c;
private int count=0;
private CountPanel panel;

public SaverWindow(Frame aframe)
{

super(aframe);
frame=aframe;
setLocation(0,0);
setSize(300,300);

panel=new CountPanel();
c=new Counter();
CountListener tListen=new CountListener();
c.addTimerListener(tListen);
panel.add(c);
ScreenMouseListener sml=new ScreenMouseListener();

panel.addMouseMotionListener(sml);
add(panel);

c.startNow();

}

public void panelCleanUp()
{
dispose();
frame.dispose();
System.exit(1);
}

public void run()
{
panel.setCount(""+count);
}
class CountListener implements TimerListener
{
public void countIncrement(TimerEvent te)
{
count=te.getCount();
EventQueue.invokeLater(SaverWindow.this);
}
}
class ScreenMouseListener extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent me)
{
System.out.println("MouseMotion event");
c.stopCount();
panelCleanUp();
}
}
}
---------------------------------------------------
import java.awt.*;
import java.awt.event.*;

import java.util.*;
import javax.swing.*;
import java.io.*;
public class CountPanel extends JPanel
{
JTextField countField;


public CountPanel()
{
countField=new JTextField(20);
add(countField);

}
public void setCount(String s)
{
countField.setText(s);
}
}
--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
public class Counter extends JComponent implements Runnable
{
private int count=0;
private EventListenerList listenerList;
private boolean stopRequested=false;
private Thread t;
public Counter()
{
listenerList=new EventListenerList();
count=0;
t=new Thread(this);
}
public void startNow()
{
t.start();
}
public int getCount()
{
return count;
}
public synchronized void run()
{
for(;;count++)
{
try
{
Thread.sleep(5);
}
catch (InterruptedException e)
{
}
EventQueue queue=Toolkit.getDefaultToolkit().getSystemEventQueue();
TimerEvent event=new TimerEvent(this);
queue.postEvent(event);
if(stopRequested)
break;
}
}
public void stopCount()
{
stopRequested=true;
}
public void addTimerListener(TimerListener listener)
{
listenerList.add(TimerListener.class, listener);
}
public void removeTimerListener(TimerListener listener)
{
listenerList.remove(TimerListener.class, listener);
}
public void processEvent(AWTEvent event)
{
if (event instanceof TimerEvent)
{
EventListener[] listeners=listenerList.getListeners(TimerListener.class);
for(int i=0;i<listeners.length;i++)>
{
((TimerListener)listeners[i]).countIncrement((TimerEvent)event);
}
}
else super.processEvent(event);
}

}

Well there it is. I would really welcome any suggestions that the good people on this site might have, I really have no idea how to proceed here.
Dan
Dan Temple
Ranch Hand

Joined: Jul 10, 2001
Posts: 93
Sorry for the length of the post. I guess I should also say that the Java Tasks appear only when it is launched from the native code, not when I run the classes from the command prompt. I guess that means its definitely a problem with the native code.
Dan
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java Screensaver! ... almost ...