JFrame and Timers

First Question:

I'm trying to make my 'Intro' JFrame go away after the user presses any key on the keyboard, however its not working out as easy as I thought it would have. (Of course I've probably just overlooked something simple)

Here is what im working with that matters:

      public introImg()
      {
            Img = new ImageIcon("Intro.GIF");
            
            lig = new JLabel();
            lig.setOpaque(true);
            lig.setHorizontalAlignment(JLabel.CENTER);
            
            
            JFrame intro = new JFrame();
            JFrame.setDefaultLookAndFeelDecorated(true);
            
            
            JLabel label = new JLabel(Img);
            intro.getContentPane().add(label);
            
            intro.addKeyListener(this);
            
            Image icon = Toolkit.getDefaultToolkit().getImage("orange_E.gif");
            
            intro.setTitle("Welcome to Chomper!");
            intro.setSize(360, 180);
            intro.setLocationRelativeTo(null);                        // Centering JFrame
            intro.setIconImage(icon);                              // Setting an *.ICON
            intro.setUndecorated(true);
            intro.setVisible(true);
            intro.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            
      
      }

      public void keyReleased(KeyEvent arg0) {
            g.window();                                       // Displays Main UI
      }

----------------------------------------------------------------------
Another question... I've never learned anything about timers (except for my use of Visual Basic, but from what ive looked at, its nothing like the same).

Could anybody explain it a LITTLE what the basics are to know about using timers?

For example.. How about, I would like to wait for 10 seconds before I want my application to do something (i.e move a character, move to another JFrame, etc). Does it involve multiple methods or just a few lines?  What ive found as examples are really confusing, so barney style would be welcomed. *laughs* Or heck, even more of a thorough example could be given to me on AIM (aim marine). Regardless, points will be distributed fairly (per response here)

Any input would be kindly appreciated.

Thank you all for your time

Regards,
David
LVL 3
ildave1Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
your code should be more like

     private JFrame intro = new JFrame();

     public introImg()
     {
          Img = new ImageIcon("Intro.GIF");
         
          lig = new JLabel();
          lig.setOpaque(true);
          lig.setHorizontalAlignment(JLabel.CENTER);
         
         
          JFrame.setDefaultLookAndFeelDecorated(true);
         
         
          JLabel label = new JLabel(Img);
          intro.getContentPane().add(label);
         
          intro.addKeyListener(this);
         
          Image icon = Toolkit.getDefaultToolkit().getImage("orange_E.gif");
         
          intro.setTitle("Welcome to Chomper!");
          intro.setSize(360, 180);
          intro.setLocationRelativeTo(null);                     // Centering JFrame
          intro.setIconImage(icon);                         // Setting an *.ICON
          intro.setUndecorated(true);
          intro.setVisible(true);
     }

     public void keyReleased(KeyEvent arg0) {
          g.window();                                       // Displays Main UI
          intro.dispose();
     }

And add your mouse listener to whatever you want the user to click on to close it

0
 
suprapto45Connect With a Mentor Commented:
Hi David,

The timer tutorial is in
http://www.javaalmanac.com/cgi-bin/search/find.pl?words=Timer

I'll try to answer your first question in a moment.

Regards
Dave
0
 
zzynxSoftware engineerCommented:
Am I right when I think you're trying to make a splash screen?
Maybe this can help:
* Make a splash with Swing: http://www.javaworld.com/javaworld/javatips/jw-javatip104.html
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
objectsCommented:
the following gives a good intro to swing timers

http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
0
 
ildave1Author Commented:
Splash Screen, Yes... I should have referenced it like that.

Im sorry for the confusion.  Im going through the Links right now.

Regards,
David
0
 
suprapto45Commented:
Hi,

Well, object has covered it. It is just a matter of second objects :). I lost to you in few seconds away.

Regards
Dave
0
 
ildave1Author Commented:
Objects,
    When I do that, "<intro>.dispose();"  isn't being recognized.

Regards,
Dave
0
 
suprapto45Commented:
Hi Dave,

What do you mean by "isn't being recognized"? Is it syntax error or what? Actually JFrame has the dispose() method in it and in your codes, intro is accessible from keyReleased.

Regards
Dave
0
 
zzynxSoftware engineerCommented:
I think he means "intro" is no valid variable name in the keyReleased() function.
oldave1, can we see all your code please?
0
 
zzynxSoftware engineerCommented:
Is introImg also your KeyListener?
0
 
ildave1Author Commented:
http://www.pastebin.com/278972

zzynx -- Right...

This is my Main.java and introImg.java

Regards,
Dave
0
 
zzynxConnect With a Mentor Software engineerCommented:
move
>> JFrame intro = new JFrame();

after

>> private JLabel lig;

Then you will be able to write

public void keyReleased(KeyEvent arg0) {
                g.window();
                intro.dispose();
}

0
 
ildave1Author Commented:
.... Ugh... I love programming, cant wait to do this for a full time job...  :)

1 Minor Detal.... 2 1/2 hours of work trying to find it.  *Laughs*

Thanks a lot zzynx... A savior yet again.

----------------------------------
About the timer links.. Those are the same links i've found that confused me when I went to code it (as a sample)...  I'll mess with some more I suppose.  Thanks anyway though!

----------------------------------
If anybody reads this after I award points.  Are there any fancy tutorials on how to make a text appear on the screen for maybe 5 seconds and then fade out?  Would I need to create another JFrame that housed the label and somehow minipulate it from there?

Thanks a lot for all of you that helped! Your time is greatly appreciated.

Regards,
David
0
 
zzynxSoftware engineerCommented:
thanks
0
 
zzynxSoftware engineerCommented:
>> Are there any fancy tutorials on how to make a text appear on the screen for maybe 5 seconds and then fade out?

Here's some demo that'll certainly inspire you:

/*
 * FadingTest.java
 *
 *
 * NB: I override "paint" in the panel rather than the recomended
 *     "paintComponent", as then it draws on top of the other swing controls too
 *     (causing everything on that panel to "fade in")
 *
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.* ;
import java.text.* ;

public class FadingTest extends JFrame
{
  class FadingPanel extends JPanel implements Runnable
  {
    AlphaComposite comp = makeComp( 1.0f ) ;

    public FadingPanel()
    {
      setLayout( new FlowLayout( FlowLayout.CENTER ) ) ;
      add( new JLabel( "Woo" ) ) ;
      add( new JButton( "Components!" ) ) ;
    }

    private AlphaComposite makeComp( float alpha )
    {
      return AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha ) ;
    }

    public void paint( Graphics g )
    {
      super.paint( g ) ;
      if( comp != null )
      {
        Graphics2D g2d = (Graphics2D)g ;
        Composite oldComp = g2d.getComposite() ;
        g2d.setComposite( comp );
        g2d.setColor( Color.white );
        g2d.fill( new java.awt.geom.Rectangle2D.Float( 0, 0, getWidth(), getHeight() ) );
        g2d.setComposite( oldComp );
      }
    }

    public void run()
    {
      for( int i = 0 ; i < 10 ; i++ )
      {
        comp = makeComp( (float)( 9 - i ) / 10.0f ) ;
        paintImmediately( 0, 0, getWidth(), getHeight() ) ;
        try { Thread.currentThread().sleep( 200 ); } catch( InterruptedException ignored ) {}
      }
    }
  }

  BorderLayout borderLayout1 = new BorderLayout();
  FadingPanel panel = new FadingPanel() ;

  public FadingTest()
  {
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception
  {
    this.setSize( new Dimension( 400, 300 ) ) ;
    this.setTitle( "Frame Title" ) ;
    this.getContentPane().setLayout( borderLayout1 ) ;
    this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    this.setLocationRelativeTo( null );
    JButton go = new JButton( "Go" ) ;
    this.getContentPane().add( go, BorderLayout.NORTH ) ;
    this.getContentPane().add( panel, BorderLayout.CENTER ) ;
    go.addActionListener( new ActionListener()
                          {
                            public void actionPerformed( ActionEvent e )
                            {
                              Thread t = new Thread( panel ) ;
                              t.start() ;
                            }
                          } );
  }

  public static void main( String[] args ) throws Exception
  {
    FadingTest t = new FadingTest() ;
    t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    t.show() ;
  }
}
0
 
suprapto45Commented:
Hi Dave,

Glad I could help.

>> Are there any fancy tutorials on how to make a text appear on the screen for maybe 5 seconds and then fade out?
http://javaboutique.internet.com/text/fade.html

But try the zzynx's solution first :).

Regards
Dave
0
 
ildave1Author Commented:
WOW... Inspired = True....

Thanks a lot! Best of all, I can understand it!! (at my level, sort of)..

I appreciate that!

Regards,
Dave
0
 
zzynxSoftware engineerCommented:
>> WOW... Inspired = True....
>> Best of all, I can understand it!!
:°)
Success
0
 
suprapto45Commented:
Hehe

Good luck and happy programming

Regards
Dave
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.