-
Trying to figure out how to make a screen saver.
I know that in order to make a good screensaver, I need to ditch the title bar and the minimizing components thing.
I've tried a few things including JWindow but it's not even showing up.
What class would be good as a sort of window or GUI that wouldn't have a minimize or maximize or title bar to it and yet would still let me add a Mouse Listener to it?
I tried these things below and nothing worked.
JPanel appears to have windows too. If not, how to you get one to show up without a window? And how do you add a Mouse Listener to it?
Most screensavers will exit when you click a mouse or hit a key. (Is there a way you can make it exit if you press a key?
Like if (KEY_PRESSED == true) or something like that)
JFrame is out.
Frame is also probably out.
JSplitPane is out.
JLayeredPane seems too strange.
JApplet also a title bar, and I don't want anything that's internet run at the moment anyway.
JViewPort, whatever that is, isn't working either.
Code java:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Demo5 extends JWindow
{
private JButton button, button2;
private JPanel p;
public Demo5()
{
setVisible(true);
button = new JButton("Button");
button2 = new JButton("Button 2");
p = new JPanel();
p.add(button);
p.add(button2);
setFocusable(true);
add(button);
setContentPane(p);
}
public static void main(String[] args)
{
Demo5 d5 = new Demo5();
d5.setVisible(true);
JLayeredPane jlp = new JLayeredPane();
jlp.setVisible(true);
}
}
-
Re: Trying to figure out how to make a screen saver.
Frame (which is extended by JFrame) has setUndecorated method. You should also be able to add Key and Mouse Listeners.
-
Re: Trying to figure out how to make a screen saver.
What's setUndecorated() do?
-
Re: Trying to figure out how to make a screen saver.
It causes the planet to be sucked into a blackhole. You can either trust me or write some code to experiment and find out for yourself.
-
Re: Trying to figure out how to make a screen saver.
Ok. It worked.
However, I'm trying a bunch of MouseListeners and MouseMotionListeners and I can't figure out what method I should put the code that will exit the program if the mouse is moved.
Code java:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
public class Demo5 extends JFrame implements MouseMotionListener
{
private JButton button, button2;
private JPanel p;
public Demo5()
{
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
setVisible(true);
p = new JPanel();
setContentPane(p);
}
public void mouseDragged(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
System.exit(0);
}
public static void main(String[] args)
{
Demo5 d5 = new Demo5();
}
}
Also, I figured out how to start the windows opened maximized.
However, if you compile it, you'll probably need Ctrl+Alt+Delete or something as there's no way of getting out, unless you have a Windows Button that's in the shape of a four piece flag or window on your computer.
It's supposed to be a screen saver.
-
Re: Trying to figure out how to make a screen saver.
Quote:
Originally Posted by
Junky
It causes the planet to be sucked into a blackhole. You can either trust me or write some code to experiment and find out for yourself.
Man, Java programming sure has gotten a lot more dangerous! :)):))
Ok, so it hides the title bar and window buttons. Done that.
Thanks.
-
Re: Trying to figure out how to make a screen saver.
Ok, I have fixed that problem, though why I had to take that route is beyond me.
I told it to add a MouseListener and told it to add the MouseListener the class implemented.
However, I had thought that, like when you implement ActionListener, the class would automatically add the MouseListener that the class implements to itself without me having to bother to do it manually. Any idea why that happened?
Now I can't figure out how to get it to exit if ANY key is pressed, rather than having to list all like 200 of them.
Is it even possible to do that?
So far, it's not working.
Also, if I try to set it so that I attach an event to mouseMoved(), it will exit immediately.
That's not quite what I had hoped for.
How do I get it so that it won't exit immediately but will exit if the mouse moves?
Code java:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Demo5 extends JFrame implements MouseInputListener, KeyListener
{
private JPanel p;
private int times;
public Demo5()
{
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
setVisible(true);
p = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
setContentPane(p);
}
public void mouseClicked(MouseEvent e)
{
System.exit(0);
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void keyPressed(KeyEvent e)
{
if (e.getModifiers() == KeyEvent.KEY_PRESSED)
System.exit(0);
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
if (e.getModifiers() == KeyEvent.KEY_TYPED)
System.exit(0);
}
public void mouseDragged(MouseEvent e)
{
System.exit(0);
}
public void mouseMoved(MouseEvent e)
{
int y = 0;
int y2 = 0;
if (times == 0)
{
y = e.getYOnScreen();
times++;
}
else
{
y2 = e.getYOnScreen();
if (y != y2)
System.exit(0);
}
}
public static void main(String[] args)
{
Demo5 d5 = new Demo5();
}
}
Ok, I've got it so that it now works if mouse is moved too, but still the exit for any key pressed isn't working.
-
Re: Trying to figure out how to make a screen saver.
Change this method
public void keyPressed(KeyEvent e)
{
if (e.getModifiers() == KeyEvent.KEY_PRESSED)
System.exit(0);
}
-
Re: Trying to figure out how to make a screen saver.
Quote:
Originally Posted by
javapenguin
I know that in order to make a good screensaver, I need to ditch the title bar and the minimizing components thing.
I've tried a few things including JWindow but it's not even showing up.
What class would be good as a sort of window or GUI that wouldn't have a minimize or maximize or title bar to it and yet would still let me add a Mouse Listener to it?
I tried these things below and nothing worked.
JPanel appears to have windows too. If not, how to you get one to show up without a window? And how do you add a Mouse Listener to it?
Most screensavers will exit when you click a mouse or hit a key. (Is there a way you can make it exit if you press a key?
Like
if (KEY_PRESSED == true) or something like that)
JFrame is out.
Frame is also probably out.
JSplitPane is out.
JLayeredPane seems too strange.
JApplet also a title bar, and I don't want anything that's internet run at the moment anyway.
JViewPort, whatever that is, isn't working either.
Code java:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Demo5 extends JWindow
{
private JButton button, button2;
private JPanel p;
public Demo5()
{
setVisible(true);
button = new JButton("Button");
button2 = new JButton("Button 2");
p = new JPanel();
p.add(button);
p.add(button2);
setFocusable(true);
add(button);
setContentPane(p);
}
public static void main(String[] args)
{
Demo5 d5 = new Demo5();
d5.setVisible(true);
JLayeredPane jlp = new JLayeredPane();
jlp.setVisible(true);
}
}
Quote:
Originally Posted by
javapenguin
I know that in order to make a good screensaver, I need to ditch the title bar and the minimizing components thing.
I've tried a few things including JWindow but it's not even showing up.
What class would be good as a sort of window or GUI that wouldn't have a minimize or maximize or title bar to it and yet would still let me add a Mouse Listener to it?
I tried these things below and nothing worked.
JPanel appears to have windows too. If not, how to you get one to show up without a window? And how do you add a Mouse Listener to it?
Most screensavers will exit when you click a mouse or hit a key. (Is there a way you can make it exit if you press a key?
Like
if (KEY_PRESSED == true) or something like that)
JFrame is out.
Frame is also probably out.
JSplitPane is out.
JLayeredPane seems too strange.
JApplet also a title bar, and I don't want anything that's internet run at the moment anyway.
JViewPort, whatever that is, isn't working either.
Code java:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Demo5 extends JWindow
{
private JButton button, button2;
private JPanel p;
public Demo5()
{
setVisible(true);
button = new JButton("Button");
button2 = new JButton("Button 2");
p = new JPanel();
p.add(button);
p.add(button2);
setFocusable(true);
add(button);
setContentPane(p);
}
public static void main(String[] args)
{
Demo5 d5 = new Demo5();
d5.setVisible(true);
JLayeredPane jlp = new JLayeredPane();
jlp.setVisible(true);
}
}
:)>-:)>-:)>-:)>-
-
Re: Trying to figure out how to make a screen saver.
Quote:
Originally Posted by
wookyoo
Change this method
public void keyPressed(KeyEvent e)
{
if (e.getModifiers() == KeyEvent.KEY_PRESSED)
System.exit(0);
}
Change it to what?
-
Re: Trying to figure out how to make a screen saver.
Ok, could somebody please explain what I'm doing wrong? I tried a key listener to get it to exit when any key is pressed.
It should be stopping invalid keys, which always seem to have a key code of 0.
However, now it won't even print the key text.
If I remove the str = bla bla bla and the if statement, it will print again.
Why I have to add key and mouse listeners to the class manually with addKeyListener(this), etc, is beyond me.
I already have the class implementing key listener and mouse motion listener, which is an interface that implements mouse listener and some other listener, yet I still have to add them.
Code java:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Demo5 extends JFrame implements MouseInputListener, KeyListener
{
private ScreenSaverPanel p;
private int times;
private class ScreenSaverPanel extends JPanel
{
public ScreenSaverPanel()
{
setBackground(Color.BLACK);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
}
protected void paintBorder(Graphics g)
{
super.paintBorder(g);
}
}
public Demo5()
{
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
setVisible(true);
p = new ScreenSaverPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
setContentPane(p);
}
public void mouseClicked(MouseEvent e)
{
System.exit(0);
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void keyPressed(KeyEvent e)
{
System.out.println("Key Code: " + e.getKeyCode());
System.out.println("Text: " + e.getKeyText(e.getKeyCode()));
String str = e.getKeyText(e.getKeyCode());
if (str.equals(str) && e.getKeyCode() != 0)
System.exit(0);
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
System.exit(0);
}
public void mouseMoved(MouseEvent e)
{
int y = 0;
int y2 = 0;
int x = 0;
int x2 = 0;
if (times == 0)
{
y = e.getYOnScreen();
x = e.getXOnScreen();
times++;
}
else
{
y2 = e.getYOnScreen();
x2 = e.getXOnScreen();
if (y != y2 || x != x2)
System.exit(0);
}
}
public static void main(String[] args)
{
Demo5 d5 = new Demo5();
}
}
-----Edit--------
Ok, by removing the two printlines, now it will exit if I hit one of the alphabetical, not the numerical or any other, keys. But I have to hit them first. I can't hit "1b" for instance.
Ok, it'll only work for about 10 seconds after the program is started. Otherwise, no key will work.
-
Re: Trying to figure out how to make a screen saver.
Seems to me that Full-screen Exclusive Mode would be ideally suited to screensavers.
-
Re: Trying to figure out how to make a screen saver.
What in the name of James Gosling do you think this is doing:
if (str.equals(str)
And why are you comparing the x and y values in the mouseMoved() function?
And if you don't care which key is pressed, why put any if statement in at all?
Also, check which component, if any, has the focus.
-
Re: Trying to figure out how to make a screen saver.
Just telling it to exit when I press a key either causes it to exit at nothing or not exit when I press a key.
I tried that Full Screen Exclusive Mode and the screen is flickering.
Also, why is my background sometimes starting out black like it's supposed to and other times starting out gray?
Is it one of my paint methods, though they're not being used, that's responsible for that?
-
Re: Trying to figure out how to make a screen saver.
I don't have time to look through the multiple code dumps and edits you've done- you should keep us up to date with, you guessed it, an SSCCE. Why do you have multiple paint methods? Flickering is usually caused by incorrectly handling painting, which I know we've talked about before.
I recommend splitting up your problem into multiple parts- create a normal program that responds to ANY keypress. Create another program, completely separately, that does full-screen painting. Create an SSCCE for each, and ask specific questions.
Only when you have both working should you worry about combining them.
-
Re: Trying to figure out how to make a screen saver.
Ok, here's on SSCCE. It still has the key problem in it.
Code java:
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.event.*;
import javax.swing.*;
public class KeyListenerTest extends JFrame implements KeyListener
{
private JPanel panel;
public KeyListenerTest()
{
panel = new JPanel();
panel.setBackground(Color.BLACK);
addKeyListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(panel);
}
public void keyPressed(KeyEvent e)
{
System.out.println("A key was pressed.");
System.exit(0);
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
System.out.println("A key was typed.");
System.exit(0);
}
public static void main(String[] args)
{
KeyListenerTest klt = new KeyListenerTest();
klt.setVisible(true);
}
}
Sometimes it'll exit without a key pressed and others it won't even exit at all.
Who's James Gosling?
-
Re: Trying to figure out how to make a screen saver.
Thanks for making the SSCCE. Sorry, but that code seems to work just fine for me. It doesn't exit until I press a key when the JFrame is focused. And it exits every time I press a key when the JFrame is in focus. It behaves exactly how I expect it to.
Are you using some kind of weird keyboard or anything like that?
-
Re: Trying to figure out how to make a screen saver.
Laptop keyboard. One of the keys is missing I'll admit, but the button that lets the key function is still functioning as I can still type a J.
However, I never called the method setFocusable(true). Let's see what that would do.
This is weird. I use setFocusable and it exits if I minimize it and sometimes exits when no key is pressed.
-
Re: Trying to figure out how to make a screen saver.
The code you posted does what it says on the tin here - it exits when a key is pressed (it never gets to the key typed handler, because it exits before that).
How can you tell it exits without a key being pressed? does it flash on the screen then exit before you can do anything?
What about it ignoring keys - do you make sure the frame has focus before pressing a key?
-
Re: Trying to figure out how to make a screen saver.
How do you make sure the frame has focus?
I tried setFocusable().
The thing is, at least it was for me, I can get it to exit if I click within like 10 seconds otherwise no.
Sometimes it exits immediately as soon as I start it.
As for how I can tell, it gives the println output for key pressed though none was and then exits.
-
Re: Trying to figure out how to make a screen saver.
Quote:
Originally Posted by
javapenguin
How do you make sure the frame has focus?
I tried setFocusable().
What did you expect that to do? What does the API say that does? Does the API contain any methods that returns whether a component is focused?
Quote:
Originally Posted by
javapenguin
The thing is, at least it was for me, I can get it to exit if I click within like 10 seconds otherwise no.
Sometimes it exits immediately as soon as I start it.
As for how I can tell, it gives the println output for key pressed though none was and then exits.
Are you sure you're running the code you posted, and not code with a MouseListener in it? Did you recompile everything, etc? What happens if, instead of exiting, you print out the key that was pressed?
-
Re: Trying to figure out how to make a screen saver.
It's focusable but the key listener isn't working.
I'm using a test program. No, it has no mouse listener.
Code java:
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.event.*;
import javax.swing.*;
public class KeyListenerTest extends JFrame implements KeyListener
{
private JPanel panel;
public KeyListenerTest()
{
panel = new JPanel();
setExtendedState(JFrame.MAXIMIZED_BOTH);
panel.setBackground(Color.BLACK);
setUndecorated(true);
setVisible(true);
addKeyListener(this);
//setFocusable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
System.out.println(isFocusable());
setContentPane(panel);
}
public void keyPressed(KeyEvent e)
{
if (isFocusable())
{
System.out.println("A key was pressed.");
System.exit(0);
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
System.out.println("A key was typed.");
System.exit(0);
}
public static void main(String[] args)
{
KeyListenerTest klt = new KeyListenerTest();
}
}
It seems it's not often going into the key listener.
A few times it goes into it without any key being pressed.
It'll print out "A key was pressed." if the key listener is called and then will exit.
Should I add a Key Listener to the JPanel instead.
-
Re: Trying to figure out how to make a screen saver.
if you want the program exits when any key is pressed then...
when keyPressed system exits, right?
i tried this and worked...
Quote:
public void keyPressed(KeyEvent e)
{
System.exit(0);
}
if i didn't understand then... please comment again.
-
Re: Trying to figure out how to make a screen saver.
Quote:
Originally Posted by
wookyoo
if i didn't understand then... please comment again.
If you read his code, I think you'll find he knows about System.exit(0) - and uses it. If you read his posts, I think you'll find that has nothing to do with his problem.
-
Re: Trying to figure out how to make a screen saver.
Still can't figure it out.
It's not my calling setExtendedState() is it?
Sometimes the panel doesn't even change to the background I told it to.
Of course, a couple of my programs have sometimes, until refreshed or minimized then maximized, shown improper stuff that's not always there or left out components that later appear once minimized and maximized.
Not often, but it happens every once in a while.
Why isn't it calling the keyListener?
I found it's calling none of the key listener methods.
Or rather, it's not usually calling them.
Ok, I made sure with a println.
It has the key Listener, but why won't it normally use it, and why, when it does, will it exit without any keys being pressed? ~X(~X(~X(