pjcrooks2000
asked on
Java classes adding them to main class
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().setLayo ut( 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.isEventDis patchThrea d() )
{
try
{
SwingUtilities.invokeAndWa it( 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( ).getScree nSize();
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.wid th/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("Settin g 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!
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()
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().setLayo
//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
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.isEventDis
{
try
{
SwingUtilities.invokeAndWa
}
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(
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.wid
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("Settin
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!
> 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(nu ll);
height = img.getImage().getHeight(n ull);
x = 300;
y = 300;
Dimension sd = Toolkit.getDefaultToolkit( ).getScree nSize();
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.invokeAndWa it(closerR unner);
// 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();
}
}
> 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(nu
height = img.getImage().getHeight(n
x = 300;
y = 300;
Dimension sd = Toolkit.getDefaultToolkit(
splash.setBounds((sd.width
pause = waitTime;
closerRunner = new Runnable()
{
public void run()
{
splash.setVisible(false);
splash.dispose();
}
};
waitRunner = new Runnable()
{
public void run()
{
try
{
Thread.sleep(3000);
SwingUtilities.invokeAndWa
// 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
}
public void paint(Graphics g) {
splashImage.paintIcon(this
}
}
public static void main(String args[]) {
Splash x = new Splash();
}
}
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( ).getScree nSize();
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.wid th/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("Settin g 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();
}
}
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(
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.wid
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("Settin
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();
}
}
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.
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.
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).
ASKER
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().setLayo ut( 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.isEventDis patchThrea d() )
{
try
{
SwingUtilities.invokeAndWa it( 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( ).getScree nSize();
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.wid th/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("Settin g Visable");
dispose();
}
catch(Exception e)
{
e.printStackTrace();
// can catch InvocationTargetException
// can catch InterruptedException
}
}
};
setVisible(true);
Thread splashThread = new Thread(waitRunner, "SplashThread");
splashThread.start();
}
}
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()
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().setLayo
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
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.isEventDis
{
try
{
SwingUtilities.invokeAndWa
}
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(
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.wid
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("Settin
dispose();
}
catch(Exception e)
{
e.printStackTrace();
// can catch InvocationTargetException
// can catch InterruptedException
}
}
};
setVisible(true);
Thread splashThread = new Thread(waitRunner, "SplashThread");
splashThread.start();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
points on the way, sorry to all other girionis method worked for me, maybe next time
ASKER
Now time to catch up on some other stuff i was supposed to do too :) I may post another about another class soon.
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( ).getScree nSize();
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.wid th/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();
:)
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(
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.wid
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();
:)
ASKER
Girionis I am getting an error at a line in the code
this one
:) <-------- \\\\|||////
\ -- -- /
| * * |
| ^ |
\ O /
------
He!
this one
:) <-------- \\\\|||////
\ -- -- /
| * * |
| ^ |
\ O /
------
He!
Hmm.. I never saw that error before!! :)
ASKER
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 :(
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.