?
Solved

JFrame and Timers

Posted on 2005-05-03
20
Medium Priority
?
1,123 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:ildave1
  • 8
  • 5
  • 5
  • +1
20 Comments
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 200 total points
ID: 13915627
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13915630
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
 
LVL 92

Expert Comment

by:objects
ID: 13915637
the following gives a good intro to swing timers

http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:ildave1
ID: 13915640
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13915641
0
 
LVL 92

Accepted Solution

by:
objects earned 1200 total points
ID: 13915644
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 13915656
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
 
LVL 3

Author Comment

by:ildave1
ID: 13915659
Objects,
    When I do that, "<intro>.dispose();"  isn't being recognized.

Regards,
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13915678
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13915687
I think he means "intro" is no valid variable name in the keyReleased() function.
oldave1, can we see all your code please?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13915695
Is introImg also your KeyListener?
0
 
LVL 3

Author Comment

by:ildave1
ID: 13915709
http://www.pastebin.com/278972

zzynx -- Right...

This is my Main.java and introImg.java

Regards,
Dave
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 600 total points
ID: 13915716
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
 
LVL 3

Author Comment

by:ildave1
ID: 13915764
.... 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
 
LVL 37

Expert Comment

by:zzynx
ID: 13915810
thanks
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13915820
>> 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
 
LVL 16

Expert Comment

by:suprapto45
ID: 13915925
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
 
LVL 3

Author Comment

by:ildave1
ID: 13915935
WOW... Inspired = True....

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

I appreciate that!

Regards,
Dave
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13916020
>> WOW... Inspired = True....
>> Best of all, I can understand it!!
:°)
Success
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13916362
Hehe

Good luck and happy programming

Regards
Dave
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question