Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 3 of 3 FirstFirst 123
Results 51 to 60 of 60

Thread: Trying to figure out how to make a screen saver.

  1. #51
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Sigh. What compiler are you using with BlueJ? What compiler were you using with the IDE you were using before? What happens when you compile and run from the command line?

    You STILL have not fixed the problem of adding components to a JFrame that has already been made visible.

    I stand by my statement- it's not the compiler. Blaming the compiler is meaningless. It's something you, or your IDE, or your OS, is doing- probably with the focus. Or you're misunderstanding what's going on- I see a MouseListener there again, even though your question was about a keyboard input?

    I know you want to be a good programmer, but I can tell you right now, if you don't start listening to the things people are saying (instead of just disagreeing and ignoring them for no reason), that will never happen.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  2. #52
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    My OS, Windows Vista, probably does a lot of crazy things.

    On the other hand, I never check what platform my compiler (i.e. JGrasp, Eclipse, BlueJ) has. What's an IDE? It's a something development environment but I doubt it means "Ideal Development Environment".

    Are there different types of Java?

    I think I've been using Java SE for a while, though sometimes I might be using the other platform that's only in the 2s, and maybe that's why code kept going weird in the past, though I doubt it.

    As for the problem of adding stuff to an already visible JFrame, I've no idea how to deal with that. In fact, I didn't even know, until you pointed it out, that that was a problem.

    That latest thing I said was my whole program. I can go back to the SSCCE if you'd like.

    Also, can't you add a component to the JFrame before it's made visible, or will that throw some error?

  3. #53
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Quote Originally Posted by javapenguin View Post
    My OS, Windows Vista, probably does a lot of crazy things.
    I'm still pretty sure that something with the focus is the problem- it's not a bug, it's not the compiler's fault, and it's pretty much expected behavior. You're probably doing something (or your IDE is) that causes the JFrame to not get the focus, or to lose it. That's why you have to click first- clicking gives the JFrame the focus, which then sends the key input to it.

    Quote Originally Posted by javapenguin View Post
    On the other hand, I never check what platform my compiler (i.e. JGrasp, Eclipse, BlueJ) has. What's an IDE? It's a something development environment but I doubt it means "Ideal Development Environment".
    Those are not compilers. A compiler is the "javac" part of the JDK. You can use it from the command line. You can use it from an IDE- did you google what that is? It's an editor, which I think you're calling compiler for some reason. BlueJ is an IDE. Netbeans is an IDE. BlueJ is an IDE. I think what's happening is you're doing something in your IDE (like clicking somewhere right before the JFrame pops up) that interferes with the focus- which is not unexpected.

    Quote Originally Posted by javapenguin View Post
    Are there different types of Java?
    I'm not even sure what this means. There are different versions, then there is Java SE, Java EE, Java ME, etc. Google is your friend. I don't know what this question has to do with your problem though.


    Quote Originally Posted by javapenguin View Post
    I think I've been using Java SE for a while, though sometimes I might be using the other platform that's only in the 2s, and maybe that's why code kept going weird in the past, though I doubt it.
    No idea what this means. What is the "other platform that's only in the 2s" and what does it have to do with your question?

    Quote Originally Posted by javapenguin View Post
    As for the problem of adding stuff to an already visible JFrame, I've no idea how to deal with that. In fact, I didn't even know, until you pointed it out, that that was a problem.
    When you set the content pane, you're adding components to the JFrame. You can't just do that to a JFrame that's already visible- there are methods you can call, but the best way to avoid the problem is by adding them before the JFrame is visible. I asked you to fix this because it's an obvious problem (it's why the background wasn't showing up), and I wanted to make sure we avoided its interference with the real issue. You still haven't fixed it. What do you get out of these forums, if you just ignore the advice you receive?

    Quote Originally Posted by javapenguin View Post
    That latest thing I said was my whole program. I can go back to the SSCCE if you'd like.
    It just gets confusing, because your problem descriptions are already unclear- you say you have a problem with getting keyboard input, and that it sometimes exits when the mouse moves. Then you post two pieces of code- one with a MouseListener, one without. Which one are you using? Which one are you talking about? We can't help you if you aren't using the code that you're talking about, which I suspect is the case.

    Quote Originally Posted by javapenguin View Post
    Also, can't you add a component to the JFrame before it's made visible, or will that throw some error?
    What happened when you tried?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #54
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    If I try to run Java SE on Java ME compiler for example, will it sometimes make errors?

    If so, that might explain some of my problems. I never know what platform I'm using on compiler, so it would explain some compiler problems I've had in the past, possibly even this current one.

    As for what happened when I added all the stuff before the JFrame was made visible, now the window will maximize, though it still takes about 2 seconds, possibly because of the compiler console screen, which is a popup on BlueJ, but still, it's another improvement.

    Here's the SSCCE you wanted:

    /**
     * Write a description of class KeyListenerTest here.
     * This should be an SSCCE to my problem.  Now it pretty much should be working.
     * Why it works in BlueJ but not JGrasp is still a mystery.  
     * @author (your name) javapenguin
     * @version (a version number or a date)
     */
     
       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, WindowFocusListener
       {
     
          public void windowGainedFocus(WindowEvent e)
          {
             System.out.println("Has focus");
     
          }
     
          public void windowLostFocus(WindowEvent e)
          {
             System.out.println("Lost focus");
     
          }
     
     
     
          public  void 	keyPressed(KeyEvent e) 
          {
     
     
              //   System.out.println("A key was pressed.");
     
     
             if (e.getKeyCode() == 0)
             {
                System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
                return;
             }
             System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
     
     
             exit();
     
     
          }
     
          private void exit()
          {
             System.exit(0);
          }
     
          public  void 	keyReleased(KeyEvent e) 
          {
             System.out.println("Released:");
          }
     
          public  void 	keyTyped(KeyEvent e) 
          {
            //  System.out.println("A key was typed.");
             System.exit(0);
          }
     
          private JPanel panel;
          public KeyListenerTest()
          {
     
     
             panel = new JPanel();
             panel.setOpaque(true);
     
             addWindowFocusListener(this);
             addKeyListener(this);
     
             panel.setBackground(Color.BLACK);
             add(panel);
             setUndecorated(true);
             // add(panel);
             setVisible(true);
          	// setFocusable(true);
             setExtendedState(JFrame.MAXIMIZED_BOTH);
          	//setFocusable(true);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
             System.out.println(isFocused());
     
            // setContentPane(panel);
     
          }
     
          public static void main(String[] args)
          {
             KeyListenerTest klt = new KeyListenerTest();
            // System.out.println(klt.getKeyListeners()[0]);
     
     
          }
     
       }

    So, is there anything still wrong, other than that it's not maximizing instantly?


    It no longer exists when no key is typed because I told it not to bother when the keycode was 0, which is an invalid key, which it might have been interpreting as no key typed. Once I told it to not use the key listener if the key code was 0, the exiting problem went away and hasn't come back.

    The background color changing thing appears to be gone too.

    Also, now it will self-maximize, though I'd like it to do it instantly and not show the window at the bottom of the screen for two seconds before maximizing.

    The key listener now causes the program to exit, like I wanted it to, when a key is pressed.

  5. #55
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Quote Originally Posted by javapenguin View Post
    If I try to run Java SE on Java ME compiler for example, will it sometimes make errors?
    What happened when you tried? Did you do any research at all? Did you read any tutorials?

    Quote Originally Posted by javapenguin View Post
    If so, that might explain some of my problems. I never know what platform I'm using on compiler, so it would explain some compiler problems I've had in the past, possibly even this current one.
    No. No no no. Stop blaming the compiler. Stop using the term "compiler" to describe things that aren't compilers. This "problem" is caused by you not understanding how focus works- with your OS, with your IDE, and in general. That's not a compiler problem.

    Quote Originally Posted by javapenguin View Post
    As for what happened when I added all the stuff before the JFrame was made visible, now the window will maximize, though it still takes about 2 seconds, possibly because of the compiler console screen, which is a popup on BlueJ, but still, it's another improvement.
    Ugh. Compiler console screen? Why do you set it to be visible and then maximize it, if what you want is for it to be maximized before it becomes visible?


    Quote Originally Posted by javapenguin View Post
    So, is there anything still wrong, other than that it's not maximizing instantly?
    You tell us. Have you stepped through it with a debugger to figure out why it's not maximizing instantly? See above.


    Quote Originally Posted by javapenguin View Post
    It no longer exists when no key is typed because I told it not to bother when the keycode was 0, which is an invalid key, which it might have been interpreting as no key typed. Once I told it to not use the key listener if the key code was 0, the exiting problem went away and hasn't come back.
    No. A KeyListener won't fire for "no key typed". I predict that you were mixing up MouseListener code in the version you were running, even though the one you posted did not contain that. Do you see why people hesitate to help you?

    Quote Originally Posted by javapenguin View Post
    The background color changing thing appears to be gone too.
    That problem would have gone away days ago if you had simply slowed down and read what I had said.

    Quote Originally Posted by javapenguin View Post
    Also, now it will self-maximize, though I'd like it to do it instantly and not show the window at the bottom of the screen for two seconds before maximizing.
    See above. See a debugger.


    Quote Originally Posted by javapenguin View Post
    The key listener now causes the program to exit, like I wanted it to, when a key is pressed.
    And do you understand your original problem, or did you just cut and paste randomly until it sorta worked without having any idea what was going on?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #56
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    This is totally bizzarre. Without that code to keep it from getting an invalid keycode, on BlueJ it won't maximize and it will exit if I try to maximize it manually.

    Something is causing an invalid keycode.

    So I need to block whatever that something is by telling it not to exit, which I think it's kinda doing, based on the output.

    Some of the problems are IDE related. There's no denying it.

    What could have caused that invalid keycode?

    Sometimes it acts like I added the MouseListener even though I didn't, as it stops the program when I try to maximize it manually, though that was after I got rid of the thing stopping the invalid keycode stuff.

    If it is an IDE glitch, that thing blocking all invalid keycodes should fix that too.

    Anyway, it appears to be going through the constructor till it reaches the isFocused() println thing.

    That prints out "false".

    Then apparently the focus is gained as my WindowFoucsListener windowFocusGained() returns that it was gained at that point.

    Then, without that thing keeping out invalid keys, it says a key, albeit an unknown key, was hit and it exits, unless I have that thing to block all keys that are unknown, i.e. keys that have a keycode of 0. With that thing, it still doesn't have the focus until it goes through the constructor.

    It adds the listeners before reaching that point in the constructor, so I'm not really sure why it's not focused from the start.

    I'm still as confused as a termite and a yo-yo, though I think part of it is IDE's fault.

    It had been creating the same problem when I had the mouse listener where the mouse listener was being activated without the mouse even moving, which is why I added that code so that it would work only if the mouse was moved in either x or y, to avoid it just being set off while not being moved out of its original position, that is in the full program that had the Mouse Listener, not the SSCCE.

    I just brought it up because now I'm thinking the false key press and the false mouse motion were being caused by the same thing, likely a glitch on my part.

    However, as it does activate the KeyListener, with the SSCCE and the full program too, with BlueJ, though it still won't with JGrasp, then I'm attributing that glitch to the IDE.

    Even with the new upgrade, which I got today, for JGrasp, it's still not executing the KeyListener like it should, or indeed, pretty much at all.

    Could the IDE being entering in something that the program thinks is a key or a mouse motion, causing it to set it off, albeit with an invalid keycode or a not moved mouse?

    Should the listeners be added AFTER the JFrame is made visible?

    Something in my setups of adding the listeners or something is causing some of my listeners, I think MouseMotionListener and KeyListener, to be set off by nothing.

    That may be my fault, somehow though I can't see how at the moment, or it could be the IDE's fault.

    The fact that it will work better on BlueJ than on JGrasp shows that the IDE is at least partially to blame.

    I'm thinking now that maybe I have a glitch with the listeners or how they're being added.

    Whatever I'm doing with the KeyListener is the same mistake I'm making with the Mouse ones.

    Check the SSCCE and see what I'm doing wrong with how I'm adding the listeners.
    Last edited by javapenguin; August 4th, 2011 at 03:46 PM.

  7. #57
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to figure out how to make a screen saver.

    It isn't a glitch in the IDE- it's just a different behavior. Focus is dependent on the OS, windowing system, what other programs are doing, etc.

    Have you tried printing out the stack trace in the key listener to figure out what's calling it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #58
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    I thought print stack trace was for Exceptions.

    None are being thrown.

    There is no printStackTrace for KeyEvent.

    I've used printlines but that isn't helping much other than to tell me that at some point in the beginning, my JFrame DOES NOT have the focus.

    If that's causing the problem, I'm really at a loss as to how to solve it.

  9. #59
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    What could be causing the Listeners to react to nothing sometimes? That's what's confusing for starters.

  10. #60
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Stack traces are indeed involved with Exceptions, but they are also useful when figuring out why a particular piece of code is running. Print the stack trace. Google is your friend for figuring out how to do that (there are a few ways).

    Or better yet, RUN YOUR PROGRAM THROUGH A DEBUGGER.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Page 3 of 3 FirstFirst 123

Similar Threads

  1. How to make a mouseclick somewhere on the screen?
    By Broodjie in forum Java Theory & Questions
    Replies: 4
    Last Post: March 22nd, 2011, 06:58 AM
  2. [SOLVED] can't figure out..
    By darego in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 10th, 2010, 02:26 PM
  3. I'm new at this and can't figure it out
    By jaheh06 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 22nd, 2010, 08:44 AM
  4. Unnable to make validations work for login screen
    By fierof2 in forum Web Frameworks
    Replies: 8
    Last Post: July 29th, 2010, 07:12 AM
  5. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM