Solved

Java classes adding them to main class

Posted on 2004-08-24
13
265 Views
Last Modified: 2013-11-23
OK some of you know this code already so here goes with some more of it.  I am making a game and I have two files at present called JaySnake and Splash.

Jaysnake is my main game file and Splash is a Splash screen class.  I have put the code into two files as explained and I originally had the Splash screen working with another class both in the same file.  Both files compile ok!

How do I get the Splash class to work from my main JaySnake class?   Sounds simple then let me know the code for both files is below!  

JaySnake file
--------------------------------------


import java.awt.*;
import java.awt.event.*;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.image.* ;
import javax.imageio.* ;
import java.io.* ;
import javax.swing.* ;

public class JaySnake extends JFrame
{
  class Game extends JPanel implements Runnable
  {
    private BufferedImage backBuffer ;
    private Graphics myBackBuffer ;
   

    public Game()
    {

      /******Try read in backgroundImage file and catch any exception*/
      try
      {
        backgroundImage = ImageIO.read( new File( "default.jpg" ) );
        backgroundImage = ImageIO.read( new File( "blueGlass.jpg" ) );
        backgroundImage = ImageIO.read( new File( "grassBack.jpg" ) );
        backgroundImage = ImageIO.read( new File( "sand.jpg" ) );
      }
      catch( IOException e )
      {
        System.out.println( "Cannot load image file!" ) ;
      }
      backBuffer = new BufferedImage( DISPLAY_W, DISPLAY_H, BufferedImage.TYPE_INT_RGB ) ;
      myBackBuffer = backBuffer.getGraphics() ;

//set up the menubar
      menubar = new JMenuBar();
      setJMenuBar(menubar);

    }

    long startTime, waitTime, elapsedTime ;
    public void paintComponent( Graphics g )
    {
      g.drawImage( backBuffer, DISPLAY_X, DISPLAY_Y, null ) ;
    }

    public void run()
    {
      //     1000/25 Frames Per Second = 40millisecond delay
      int delayTime = 1000 / 25 ;

      Thread thisThread = Thread.currentThread() ;
      while( running )
      {
        startTime = System.currentTimeMillis() ;
        // render to back buffer now
        render( myBackBuffer ) ;

        repaint() ;
        //  handle frame rate
        elapsedTime = System.currentTimeMillis() - startTime ;
        waitTime = Math.max( delayTime - elapsedTime, 5 ) ;

        try
        {
          Thread.sleep( waitTime ) ;
        }
        catch( InterruptedException e )
        {}
      }
      System.out.println( "Program Exited" ) ;
    }

   
  }

  private Thread gamePlay ;
  private boolean running = true ;
  private Game game ;

  private final int DISPLAY_X ; // value assigned in constructor
  private final int DISPLAY_Y ; // value assigned in constructor

  private static final int DISPLAY_W = 600 ;
  private static final int DISPLAY_H = 600 ;

  int size, hsize = 0, score1 = 0, score2 = 0, highscore = 0 ;

  /****Creates JMenuBar */
  private JMenuBar menubar ;
  /****Creates JMenu to house items */
  private JMenu gameMenu = new JMenu( "Game" ) ;
  private JMenuItem gameStart = new JMenuItem( "Start" ) ;
  private JMenuItem gameQuit = new JMenuItem( "Quit" ) ;

  private JMenu bgMenu = new JMenu( "Backgrounds" ) ;
  private ButtonGroup bgGroup = new ButtonGroup() ;
  private JRadioButtonMenuItem bg1 = new JRadioButtonMenuItem( "Default" ) ;
  private JRadioButtonMenuItem bg2 = new JRadioButtonMenuItem( "Long grass" ) ;
  private JRadioButtonMenuItem bg3 = new JRadioButtonMenuItem( "Yellow Sand" ) ;
  private JRadioButtonMenuItem bg4 = new JRadioButtonMenuItem( "Blue Glass" ) ;

  private JMenu soundMenu = new JMenu( "Enable sounds" ) ;
  private JCheckBoxMenuItem soundMusic = new JCheckBoxMenuItem( "Music" ) ;
  private JCheckBoxMenuItem soundSound = new JCheckBoxMenuItem( "Sounds" ) ;

  private JMenu helpMenu = new JMenu( "Help" ) ;
  private JMenuItem userInstruct = new JMenuItem( "User Instructions" ) ;
  private JMenuItem helpAbout = new JMenuItem( "About" ) ;

  /******JaySnake constructor*/
  public JaySnake()
  {
    /*****Sets title for main window*/
    //setTitle("JaySnake Game");
    //getContentPane().setLayout( new BorderLayout() ) ;
    //setResizable( false ) ;
    //Causes Menu Titles to not show up when un commented
    //setIgnoreRepaint( true ) ;
    game = new Game() ;
    getContentPane().add( game, BorderLayout.CENTER ) ;
    this.setJMenuBar( menubar );

    menubar.add( gameMenu ) ;
    gameMenu.add( gameStart ) ;
    gameMenu.add( gameQuit ) ;

    // ActionListener to quit the game when quit is clicked
    gameQuit.addActionListener( new ActionListener(){  
   
               public void actionPerformed( ActionEvent e )
     {
        System.exit( 0 );
     }
      } ) ;                                                  
                         
    menubar.add( bgMenu ) ;
    bgMenu.add( bg1 ) ;
    bgMenu.add( bg2 ) ;
    bgMenu.add( bg3 ) ;
    bgMenu.add( bg4 ) ;

    bgGroup.add( bg1 ) ;
    bgGroup.add( bg2 ) ;
    bgGroup.add( bg3 ) ;
    bgGroup.add( bg4 ) ;

    menubar.add( soundMenu ) ;
    soundMenu.add( soundMusic ) ;
    soundMenu.add( soundSound ) ;

    menubar.add( helpMenu ) ;
    helpMenu.add( userInstruct ) ;
    helpMenu.add( helpAbout ) ;

    addWindowListener( new WindowAdapter()
    {
      public void windowClosing( WindowEvent e )
      {
        dispose();
        System.exit( 0 ) ;
      }
    } ) ;

    Insets insets = getInsets() ;
    DISPLAY_X = insets.left ;
    DISPLAY_Y = insets.top ;
    resizeToInternalSize( DISPLAY_W, DISPLAY_H ) ;
  }

  public void resizeToInternalSize( int internalWidth, int internalHeight )
  {
    Insets insets = getInsets() ;
    final int newWidth = internalWidth + insets.left + insets.right ;
    final int newHeight = internalHeight + insets.top + insets.bottom ;

    Runnable resize = new Runnable()
    {
      public void run()
      {
        setSize( newWidth, newHeight ) ;
      }
    } ;

    if( !SwingUtilities.isEventDispatchThread() )
    {
      try
      {
        SwingUtilities.invokeAndWait( resize ) ;
      }
      catch( Exception e )
      {}
    }
    else
      resize.run() ;

    validate() ;
  }

  public void start()
  {
    Thread gamePlay = new Thread( game ) ;
    gamePlay.start() ;
  }

  public static void main( String args[] )
  {
    System.out.println( "Starting JaySnake..." ) ;
    JaySnake app = new JaySnake() ;
    app.setSize( DISPLAY_W, DISPLAY_H ) ;
    app.setTitle( "JaySnake Game" ) ;
    app.setVisible( true ) ;
    app.start() ;
  }

/*Render method for graphics content*/
    public void render( Graphics g )
    {
     if( backgroundImage != null )
        g.drawImage( backgroundImage, 0, 0, this ) ;

      /******Draw scoreboard and player details, sets color for score and player
       ******draws rectangle in same green, draws strings and used variables score1
       ******and score2. Sets color again to black draws another rectangle
       ******that is used to highlight the playing area*/
      g.setColor( Color.green ) ;
      g.drawRect( 1, 0, 598, 32 ) ;
      g.drawString( "Player 1 score: " + score1, 10, 13 ) ;
      g.drawString( "Player 2 score: " + score2, 10, 27 ) ;
      g.drawString( "High score: " + highscore, 510, 15 ) ;
      g.setColor( Color.black ) ;
      g.drawRect( 1, 34, 598, 521 ) ;
     
    }

  /******declared class variables/containers*/
  private Image backgroundImage ;
}


Splash file
--------------------------

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

class Splash extends JWindow {
public Splash(String filename, Frame f, int waitTime)
{
  super(f);
  JLabel l = new JLabel(new ImageIcon("splash.gif"));
  //JLabel l = new JLabel(new ImageIcon("splash.jpg"));
  getContentPane().add(l, BorderLayout.CENTER);
  pack();
                 
  Dimension screenSize =
  Toolkit.getDefaultToolkit().getScreenSize();
  Dimension labelSize = l.getPreferredSize();
  setLocation(screenSize.width/2 - (labelSize.width/2),
  screenSize.height/2 - (labelSize.height/2));
                 
  addMouseListener(new MouseAdapter()
  {
    public void mousePressed(MouseEvent e)
   {
     setVisible(false);
     dispose();
          }
                     });
                     
    final int pause = waitTime = 3000;
           
                 Runnable waitRunner = new Runnable()
               {
                         public void run()
                         {
                             try
                                 {
                                     Thread.sleep(pause);
                                     setVisible(false);
                                     System.out.println("Setting Visable");
                                       dispose();
                                 }
                             catch(Exception e)
                                 {
                               e.printStackTrace();
                                     // can catch InvocationTargetException
                                     // can catch InterruptedException
                                 }
                         }
                     };
                     
                 setVisible(true);
                 Thread splashThread = new Thread(waitRunner, "SplashThread");
                 splashThread.start();
             }
   }

telling me what to do and where to put the code would be very very nice as I am feeling really dumb at the moment :)

I thank you very much!
0
Comment
Question by:pjcrooks2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 3

Expert Comment

by:Gunt
ID: 11888694
Just call it as if they were in the same file.

If both classes are in the same package (or in default package, as yours are, since I don't see package declaration), then you don't have to import them. If they are in a different package, just import the class you want to do in the class you want to use it from.

Keep in mind visibility of methods, constructors and class.
public: can be seen from anywere
protected: can be seen from same package and from subclases
default: Default is used when nothing is specified. With this, only can be seen from same package (package visibility).
private: Can only be seen from the same class.

Set the Splash class, constructors, and methods visibility according to your package layout and then you use it as you need.

Good luck.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 11888949
> How do I get the Splash class to work from my main JaySnake class?  Sounds simple then let me know
> the code for both files is below!  

Are you saying that you want your spash screen to come-out 1st before the main program ? If I'm correct then
run this code for your splash 1st and you'll see that it will show the main screen afterwards w/ 3 seconds:


/**
 * Splash Screen Class  
 *
 * Author : Javatm
 * Date    : 08/25/2004
 * Time   : 11:31 PM
 *
 */

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

public class Splash {

    private SplashWindow splash;
    private ImageIcon img;

    private Runnable closerRunner;
    private Runnable waitRunner;
    private Thread splashThread;

    private int waitTime;
    private int width;
    private int height;
    private int pause;
    private int x;
    private int y;

           public Splash() {

                     img = new ImageIcon("Splash.gif");
           splash = new SplashWindow(img);

               width = img.getImage().getWidth(null);
               height = img.getImage().getHeight(null);

           x = 300;
           y = 300;

             Dimension sd = Toolkit.getDefaultToolkit().getScreenSize();
                     splash.setBounds((sd.width - width)/2, (sd.height - height)/2, width, height);

             pause = waitTime;
             closerRunner = new Runnable()
             {
             public void run()
             {
             splash.setVisible(false);
             splash.dispose();
             }
             };

             waitRunner = new Runnable()
             {
             public void run()
             {
             try
             {
             Thread.sleep(3000);
             SwingUtilities.invokeAndWait(closerRunner);

                     // Here we are calling JaySnake class . . .

             new JaySnake();
             }
             catch(Exception e)
             {
             e.printStackTrace();

             // can catch InvocationTargetException
             // can catch InterruptedException
             }
             }
             };

               splash.setVisible(true);
             splashThread = new Thread(waitRunner, "SplashThread");
             splashThread.start();
           }

           class SplashWindow extends JWindow {

             private ImageIcon splashImage;

             public SplashWindow(ImageIcon icon) {
               splashImage = icon;
             }

             public void update(Graphics g) {
               splashImage.paintIcon(this,g,0,0);
             }

             public void paint(Graphics g) {
               splashImage.paintIcon(this,g,0,0);
             }
                     }

           public static void main(String args[]) {

           Splash x = new Splash();
           }
}

0
 
LVL 15

Expert Comment

by:Javatm
ID: 11888974
If you want to use your own codes for splash then I've fixed it, just run this 1st and after that it will
automatically show the Jaysnake program :

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

class Splash extends JWindow {
public Splash(String filename, Frame f, int waitTime)
{
  super(f);
  JLabel l = new JLabel(new ImageIcon("splash.gif"));
  //JLabel l = new JLabel(new ImageIcon("splash.jpg"));
  getContentPane().add(l, BorderLayout.CENTER);
  pack();
                 
  Dimension screenSize =
  Toolkit.getDefaultToolkit().getScreenSize();
  Dimension labelSize = l.getPreferredSize();
  setLocation(screenSize.width/2 - (labelSize.width/2),
  screenSize.height/2 - (labelSize.height/2));
                 
  addMouseListener(new MouseAdapter()
  {
    public void mousePressed(MouseEvent e)
   {
     setVisible(false);
     dispose();
    }
   });
                     
    final int pause = waitTime = 3000;
           
                 Runnable waitRunner = new Runnable()
               {
                         public void run()
                         {
                             try
                                 {
                                  Thread.sleep(pause);
                                   setVisible(false);
                                   System.out.println("Setting Visable");
                                   dispose();

                                   // We call the JaySnake class . . .
                                   new JaySnake();
                                 }
                                 catch(Exception e)
                                 {
                                 e.printStackTrace();
                                     // can catch InvocationTargetException
                                     // can catch InterruptedException
                                 }
                         }
                     };
                     
                 setVisible(true);
                 Thread splashThread = new Thread(waitRunner, "SplashThread");
                 splashThread.start();
             }
   }
0
Technology Partners: 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 35

Expert Comment

by:girionis
ID: 11889882
What I would be is to call the splash screen from inside your JaySnake class, in the main method. Then pass as a parameter to the splash screen the JaySnake itself. Then in the mouseCLicked method start the JaySnake thread:

JaySnake:

public static void main(String [] arguments)
{
    System.out.println( "Starting JaySnake..." ) ;
    JaySnake app = new JaySnake() ;
    app.setSize( DISPLAY_W, DISPLAY_H ) ;
    app.setTitle( "JaySnake Game" ) ;
    app.setVisible( true ) ;
    Splash splash = new Splash(app);
}

Spash

public Splash(JaySnake snake)
    {
        super(snake);
        ...
        ...
        final JaySnake sn = snake;
        addMouseListener(new MouseAdapter()
            {
                public void mousePressed(MouseEvent e)
                {
                    setVisible(false);
                    dispose();
                    sn.start();
                }
            });
        setVisible(true);
    }

Then your JaySnake would show up (just the window), then the splash will show up and when you click on the splash screen the JaySnake will start up. Not sure thought if this is the way you want to go.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11889886
Also I do not see why you need to have the splash screen in a separate thread. It should way for user's input anyway (and dispose of it when the user clicks on it).
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11890592
Giroionis I have decided to do as you said there, but still it will not work for some weird reason I am obviously doing it wrong.  Also it would be nice for the Splash screen to appear on it's own first, abd then once the windows is clicked it takes me to my JaySnake frame.      

Also if possible I want to reserve all naming conventions for the word snake as I will be using a class of this nature withing my play area.      Can you do me a favour and check over this code... thanks


JaySnake
---------------

import java.awt.*;
import java.awt.event.*;
import java.awt.* ;
import java.awt.event.* ;
import java.awt.image.* ;
import javax.imageio.* ;
import java.io.* ;
import javax.swing.* ;

public class JaySnake extends JFrame
{
  class Game extends JPanel implements Runnable
  {
    private BufferedImage backBuffer ;
    private Graphics myBackBuffer ;
   

    public Game()
    {

      /******Try read in backgroundImage file and catch any exception*/
      try
      {
        backgroundImage = ImageIO.read( new File( "default.jpg" ) );
        backgroundImage = ImageIO.read( new File( "blueGlass.jpg" ) );
        backgroundImage = ImageIO.read( new File( "grassBack.jpg" ) );
        backgroundImage = ImageIO.read( new File( "sand.jpg" ) );
      }
      catch( IOException e )
      {
        System.out.println( "Cannot load image file!" ) ;
      }
      backBuffer = new BufferedImage( DISPLAY_W, DISPLAY_H, BufferedImage.TYPE_INT_RGB ) ;
      myBackBuffer = backBuffer.getGraphics() ;

//set up the menubar
      menubar = new JMenuBar();
      setJMenuBar(menubar);

    }

    long startTime, waitTime, elapsedTime ;
    public void paintComponent( Graphics g )
    {
      g.drawImage( backBuffer, DISPLAY_X, DISPLAY_Y, null ) ;
    }

    public void run()
    {
      //     1000/25 Frames Per Second = 40millisecond delay
      int delayTime = 1000 / 25 ;

      Thread thisThread = Thread.currentThread() ;
      while( running )
      {
        startTime = System.currentTimeMillis() ;
        // render to back buffer now
        render( myBackBuffer ) ;

        repaint() ;
        //  handle frame rate
        elapsedTime = System.currentTimeMillis() - startTime ;
        waitTime = Math.max( delayTime - elapsedTime, 5 ) ;

        try
        {
          Thread.sleep( waitTime ) ;
        }
        catch( InterruptedException e )
        {}
      }
      System.out.println( "Program Exited" ) ;
    }

   
  }

  private Thread gamePlay ;
  private boolean running = true ;
  private Game game ;

  private final int DISPLAY_X ; // value assigned in constructor
  private final int DISPLAY_Y ; // value assigned in constructor

  private static final int DISPLAY_W = 600 ;
  private static final int DISPLAY_H = 600 ;

  int size, hsize = 0, score1 = 0, score2 = 0, highscore = 0 ;

  /****Creates JMenuBar */
  private JMenuBar menubar ;
  /****Creates JMenu to house items */
  private JMenu gameMenu = new JMenu( "Game" ) ;
  private JMenuItem gameStart = new JMenuItem( "Start" ) ;
  private JMenuItem gameQuit = new JMenuItem( "Quit" ) ;

  private JMenu bgMenu = new JMenu( "Backgrounds" ) ;
  private ButtonGroup bgGroup = new ButtonGroup() ;
  private JRadioButtonMenuItem bg1 = new JRadioButtonMenuItem( "Default" ) ;
  private JRadioButtonMenuItem bg2 = new JRadioButtonMenuItem( "Long grass" ) ;
  private JRadioButtonMenuItem bg3 = new JRadioButtonMenuItem( "Yellow Sand" ) ;
  private JRadioButtonMenuItem bg4 = new JRadioButtonMenuItem( "Blue Glass" ) ;

  private JMenu soundMenu = new JMenu( "Enable sounds" ) ;
  private JCheckBoxMenuItem soundMusic = new JCheckBoxMenuItem( "Music" ) ;
  private JCheckBoxMenuItem soundSound = new JCheckBoxMenuItem( "Sounds" ) ;

  private JMenu helpMenu = new JMenu( "Help" ) ;
  private JMenuItem userInstruct = new JMenuItem( "User Instructions" ) ;
  private JMenuItem helpAbout = new JMenuItem( "About" ) ;

  /******JaySnake constructor*/
  public JaySnake()
  {
    /*****Sets title for main window*/
    //setTitle("JaySnake Game");
    //getContentPane().setLayout( new BorderLayout() ) ;
    setResizable( false ) ;
 
    game = new Game() ;
    getContentPane().add( game, BorderLayout.CENTER ) ;
    this.setJMenuBar( menubar );

    menubar.add( gameMenu ) ;
    gameMenu.add( gameStart ) ;
    gameMenu.add( gameQuit ) ;

    // ActionListener to quit the game when quit is clicked
    gameQuit.addActionListener( new ActionListener(){  
   
               public void actionPerformed( ActionEvent e )
     {
        System.exit( 0 );
     }
      } ) ;                                                  
                         
    menubar.add( bgMenu ) ;
    bgMenu.add( bg1 ) ;
    bgMenu.add( bg2 ) ;
    bgMenu.add( bg3 ) ;
    bgMenu.add( bg4 ) ;

    bgGroup.add( bg1 ) ;
    bgGroup.add( bg2 ) ;
    bgGroup.add( bg3 ) ;
    bgGroup.add( bg4 ) ;

    menubar.add( soundMenu ) ;
    soundMenu.add( soundMusic ) ;
    soundMenu.add( soundSound ) ;

    menubar.add( helpMenu ) ;
    helpMenu.add( userInstruct ) ;
    helpMenu.add( helpAbout ) ;

    addWindowListener( new WindowAdapter()
    {
      public void windowClosing( WindowEvent e )
      {
        dispose();
        System.exit( 0 ) ;
      }
    } ) ;

    Insets insets = getInsets() ;
    DISPLAY_X = insets.left ;
    DISPLAY_Y = insets.top ;
    resizeToInternalSize( DISPLAY_W, DISPLAY_H ) ;
  }

  public void resizeToInternalSize( int internalWidth, int internalHeight )
  {
    Insets insets = getInsets() ;
    final int newWidth = internalWidth + insets.left + insets.right ;
    final int newHeight = internalHeight + insets.top + insets.bottom ;

    Runnable resize = new Runnable()
    {
      public void run()
      {
        setSize( newWidth, newHeight ) ;
      }
    } ;

    if( !SwingUtilities.isEventDispatchThread() )
    {
      try
      {
        SwingUtilities.invokeAndWait( resize ) ;
      }
      catch( Exception e )
      {}
    }
    else
      resize.run() ;

    validate() ;
  }

  public void start()
  {
    Thread gamePlay = new Thread( game ) ;
    gamePlay.start() ;
  }

  public static void main( String args[] )
  {
    System.out.println( "Starting JaySnake..." ) ;
    JaySnake app = new JaySnake() ;
    app.setSize( DISPLAY_W, DISPLAY_H ) ;
    app.setTitle( "JaySnake Game" ) ;
    app.setVisible( true ) ;
    app.start() ;
    Splash splash = new Splash(app) ;
  }

/*Render method for graphics content*/
    public void render( Graphics g )
    {
     if( backgroundImage != null )
        g.drawImage( backgroundImage, 0, 0, this ) ;

      /******Draw scoreboard and player details, sets color for score and player
       ******draws rectangle in same green, draws strings and used variables score1
       ******and score2. Sets color again to black draws another rectangle
       ******that is used to highlight the playing area*/
      g.setColor( Color.green ) ;
      g.drawRect( 1, 0, 598, 32 ) ;
      g.drawString( "Player 1 score: " + score1, 10, 13 ) ;
      g.drawString( "Player 2 score: " + score2, 10, 27 ) ;
      g.drawString( "High score: " + highscore, 510, 15 ) ;
      g.setColor( Color.black ) ;
      g.drawRect( 1, 34, 598, 521 ) ;
     
    }

  /******declared class variables/containers*/
  private Image backgroundImage ;
}

Splash
-------------

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

class Splash extends JWindow {
public Splash(String filename, Frame f, int waitTime, JaySnake snake)
{
  super(snake);
  JLabel l = new JLabel(new ImageIcon("splash.gif"));
  //JLabel l = new JLabel(new ImageIcon("splash.jpg"));
  getContentPane().add(l, BorderLayout.CENTER);
  pack();
                 
  Dimension screenSize =
  Toolkit.getDefaultToolkit().getScreenSize();
  Dimension labelSize = l.getPreferredSize();
  setLocation(screenSize.width/2 - (labelSize.width/2),
  screenSize.height/2 - (labelSize.height/2));
  final JaySnake sn = snake;
                 
  addMouseListener(new MouseAdapter()
  {
    public void mousePressed(MouseEvent e)
   {
     setVisible(false);
     dispose();
          }
                     });
                     
    final int pause = waitTime = 3000;
           
                 Runnable waitRunner = new Runnable()
               {
                         public void run()
                         {
                             try
                                 {
                                     Thread.sleep(pause);
                                     setVisible(false);
                                     System.out.println("Setting Visable");
                                       dispose();
                                 }
                             catch(Exception e)
                                 {
                               e.printStackTrace();
                                     // can catch InvocationTargetException
                                     // can catch InterruptedException
                                 }
                         }
                     };
                     
                 setVisible(true);
                 Thread splashThread = new Thread(waitRunner, "SplashThread");
                 splashThread.start();
             }
   }



0
 
LVL 35

Accepted Solution

by:
girionis earned 500 total points
ID: 11890664
That's what I would do. I;d chaneg the main method of your JaySnake to this:

public static void main( String args[] )
  {
    System.out.println( "Starting JaySnake..." ) ;
    JaySnake app = new JaySnake() ;
      app.setSize( DISPLAY_W, DISPLAY_H ) ;
      app.setTitle( "JaySnake Game" ) ;
      app.setVisible(true);
    Splash splash = new Splash(app) ;
  }

and then I'd make my Splash screen like this:

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

class Splash extends JWindow
{
      public Splash(JaySnake snake)
      {
            super(snake);
            final JaySnake sn = snake;
            JLabel l = new JLabel(new ImageIcon("splash.gif"));
            //JLabel l = new JLabel(new ImageIcon("splash.jpg"));
            getContentPane().add(l, BorderLayout.CENTER);
            pack();

            Dimension screenSize =
            Toolkit.getDefaultToolkit().getScreenSize();
            Dimension labelSize = l.getPreferredSize();
            setLocation(screenSize.width/2 - (labelSize.width/2),
            screenSize.height/2 - (labelSize.height/2));

            addMouseListener(new MouseAdapter()
            {
                  public void mousePressed(MouseEvent e)
                  {
                        setVisible(false);
                        dispose();
                        sn.start();
                  }
            });
            
            setVisible(true);
      }
}

Try it and tell us if it works (or if it is what you are looking for).
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11890710
Fantastic there girionis....  I always have a problem with this sort of stuff :)

points on the way, sorry to all other girionis method worked for me, maybe next time
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11890715
Now time to catch up on some other stuff i was supposed to do too :)  I may post another about another class soon.  
0
 
LVL 35

Expert Comment

by:girionis
ID: 11890778
Thank you for accepting. The other thing you could do is the one suggested by also the javatm, but get rid of the thread in your splash screen and make the splash screen your starting point:

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

class Splash extends JWindow
{
      public static void main(String [] args)
      {
            new Splash(new JFrame());
      }
      
      public Splash(JFrame frame)
      {
            super(frame);
            JLabel l = new JLabel(new ImageIcon("splash.gif"));
            //JLabel l = new JLabel(new ImageIcon("splash.jpg"));
            getContentPane().add(l, BorderLayout.CENTER);
            pack();

            Dimension screenSize =
            Toolkit.getDefaultToolkit().getScreenSize();
            Dimension labelSize = l.getPreferredSize();
            setLocation(screenSize.width/2 - (labelSize.width/2),
            screenSize.height/2 - (labelSize.height/2));

            addMouseListener(new MouseAdapter()
            {
                  public void mousePressed(MouseEvent e)
                  {
                        setVisible(false);
                        dispose();
                        new JaySnake() ;
                  }
            });
            
            setVisible(true);
      }
}

and then in your JaySnake get rid of the main method and move main's code inside your constructor:

...
...
Insets insets = getInsets() ;
DISPLAY_X = insets.left ;
DISPLAY_Y = insets.top ;
resizeToInternalSize( DISPLAY_W, DISPLAY_H ) ;
      
setSize( DISPLAY_W, DISPLAY_H ) ;
setTitle( "JaySnake Game" ) ;
setVisible(true);
start();

 :)
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11890897
Girionis I am getting an error at a line in the code

this one

:)  <--------   \\\\|||////
                    \  --   --  /                                                                                          
                     | *    * |
                     |    ^    |
                      \    O   /
                        ------

He!
0
 
LVL 35

Expert Comment

by:girionis
ID: 11890924
Hmm.. I never saw that error before!! :)
0
 
LVL 8

Author Comment

by:pjcrooks2000
ID: 11893369

Aye me neither

Hey while I am on the subject have you seen the only problem I am having with the menu backgrounds... ?   The first one default changes to blue glass image when clicked :(
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Way to decrease size of apk file 9 109
pagenation logic how it is working in my code 1 71
sql import cannot be resolved jsp 3 76
Print Rhino Java Array in Javascript 1 39
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

726 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