pjcrooks2000
asked on
Adding Menu bar and items to a Java JFrame game
Here is my code you will notice I have declared a MenuBar and Items to be added to it ......
package myprojects.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 ;
/****Creates JMenuBar */
private JMenuBar menubar;
/****Creates JMenu to house items */
private JMenu menu;
/**********************Cre
/*------------------------
//To include tabs new Single Player Game, multiplayer game, quit
private JMenuItem menuTabStartTab;
//To include tabs default, sand, grass, snake glass
private JMenuItem menuTabChooseBackground;
//To include tabs default,
private JCheckBoxMenuItem menuTabGameMusicOnOff;
//To include tabs default, sand, grass, snake glass
private JCheckBoxMenuItem menuTabSoundEffectsOnOff;
//To include tabs default, sand, grass, snake glass
private JMenuItem userGuide;
//To include tabs default, sand, grass, snake glass
private JMenuItem aboutBox;
/*****Until i think of somemore items at least **************************
public Game()
{
/******Try read in backgroundImage file and catch any exception*/
try
{
backgroundImage = ImageIO.read( new File( "sand.jpg" ) ) ;
}
catch( IOException e )
{
System.out.println( "Cannot load image file- The one that got away" ) ;
}
backBuffer = new BufferedImage( DISPLAY_W, DISPLAY_H, BufferedImage.TYPE_INT_RGB
myBackBuffer = backBuffer.getGraphics() ;
//set up the menubar
menubar = new JMenuBar();
menubar.setBackground(new Color(237,239,218));
setJMenuBar(menubar);
}
public void run()
{
long startTime, waitTime, elapsedTime ;
// 1000/25 Frames Per Second = 40millisecond delay
int delayTime = 1000 / 25 ;
Thread thisThread = Thread.currentThread() ;
while( running )
{
startTime = System.currentTimeMillis()
//snake.movePosition(2);
//snake2.movePosition(4);
// handle mouse events in main gamePlay
//keyLinkedList.processKey
// render to back buffer now
render( myBackBuffer ) ;
// render back buffer image to screen
Graphics g = getGraphics() ;
g.drawImage( backBuffer, DISPLAY_X, DISPLAY_Y, null ) ;
g.dispose() ;
// handle frame rate
elapsedTime = System.currentTimeMillis()
waitTime = Math.max( delayTime - elapsedTime, 5 ) ;
try
{
Thread.sleep( waitTime ) ;
}
catch( InterruptedException e )
{}
}
System.out.println( "Program Exited" ) ;
}
/*Render method for graphics content*/
public void render( Graphics g )
{
/*g.setColor(Color.black);
g.fillRect(0,0,DISPLAY_WID
//not sue whether to use this aswell as background
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 ) ;
//snake.render(g);
//snake2.render(g);
}
}
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 ;
/******JaySnake constructor*/
public JaySnake()
{
/*****Sets title for main window*/
//setTitle("JaySnake Game");
getContentPane().setLayout
setResizable( false ) ;
setIgnoreRepaint( true ) ;
game = new Game() ;
getContentPane().add( game, BorderLayout.CENTER ) ;
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() ;
}
/******declared class variables/containers*/
private Image backgroundImage ;
}
I need some help on how I should create the items further and get them to show up on my game window. muchly appreciated.
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( "sand.jpg" ) ) ;
}
catch( IOException e )
{
System.out.println( "Cannot load image file- The one that got away" ) ;
}
backBuffer = new BufferedImage( DISPLAY_W, DISPLAY_H, BufferedImage.TYPE_INT_RGB ) ;
myBackBuffer = backBuffer.getGraphics() ;
//set up the menubar
menubar = new JMenuBar();
menubar.setBackground(new Color(237,239,218));
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() ;
//snake.movePosition(2);
//snake2.movePosition(4);
// handle mouse events in main gamePlay
//keyLinkedList.processKey EventList( );
// 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" ) ;
}
/*Render method for graphics content*/
public void render( Graphics g )
{
/*g.setColor(Color.black);
g.fillRect(0,0,DISPLAY_WID TH,DISPLAY _HEIGHT);* /
//not sue whether to use this aswell as background
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 ) ;
//snake.render(g);
//snake2.render(g);
}
}
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 menu = new JMenu( "Options" ) ;
/**********************Cre ate menu items********************* ********** ********/
/*------------------------ ---------- ---------- ---------- ---------- ---------- --*/
//To include tabs new Single Player Game, multiplayer game, quit
private JMenuItem menuTabStartTab = new JMenuItem( "Start" ) ;
//To include tabs default, sand, grass, snake glass
private JMenuItem menuTabChooseBackground = new JMenuItem( "Background" ) ;
//To include tabs default,
private JCheckBoxMenuItem menuTabGameMusicOnOff = new JCheckBoxMenuItem( "Music" ) ;
//To include tabs default, sand, grass, snake glass
private JCheckBoxMenuItem menuTabSoundEffectsOnOff = new JCheckBoxMenuItem( "Sounds" ) ;
//To include tabs default, sand, grass, snake glass
private JMenuItem userGuide = new JMenuItem( "Help" ) ;
//To include tabs default, sand, grass, snake glass
private JMenuItem aboutBox = new JMenuItem( "About" ) ;
/*****Until i think of somemore items at least ************************** ******/
/******JaySnake constructor*/
public JaySnake()
{
/*****Sets title for main window*/
//setTitle("JaySnake Game");
getContentPane().setLayout ( new BorderLayout() ) ;
setResizable( false ) ;
setIgnoreRepaint( true ) ;
game = new Game() ;
getContentPane().add( game, BorderLayout.CENTER ) ;
this.setJMenuBar( menubar );
menubar.add( menu ) ;
menu.add( menuTabStartTab ) ;
menu.add( menuTabChooseBackground ) ;
menu.add( menuTabGameMusicOnOff ) ;
menu.add( menuTabSoundEffectsOnOff ) ;
menu.add( userGuide ) ;
menu.add( aboutBox ) ;
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() ;
}
/******declared class variables/containers*/
private Image backgroundImage ;
}
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( "sand.jpg" ) ) ;
}
catch( IOException e )
{
System.out.println( "Cannot load image file- The one that got away" ) ;
}
backBuffer = new BufferedImage( DISPLAY_W, DISPLAY_H, BufferedImage.TYPE_INT_RGB
myBackBuffer = backBuffer.getGraphics() ;
//set up the menubar
menubar = new JMenuBar();
menubar.setBackground(new Color(237,239,218));
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()
//snake.movePosition(2);
//snake2.movePosition(4);
// handle mouse events in main gamePlay
//keyLinkedList.processKey
// 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" ) ;
}
/*Render method for graphics content*/
public void render( Graphics g )
{
/*g.setColor(Color.black);
g.fillRect(0,0,DISPLAY_WID
//not sue whether to use this aswell as background
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 ) ;
//snake.render(g);
//snake2.render(g);
}
}
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 menu = new JMenu( "Options" ) ;
/**********************Cre
/*------------------------
//To include tabs new Single Player Game, multiplayer game, quit
private JMenuItem menuTabStartTab = new JMenuItem( "Start" ) ;
//To include tabs default, sand, grass, snake glass
private JMenuItem menuTabChooseBackground = new JMenuItem( "Background" ) ;
//To include tabs default,
private JCheckBoxMenuItem menuTabGameMusicOnOff = new JCheckBoxMenuItem( "Music" ) ;
//To include tabs default, sand, grass, snake glass
private JCheckBoxMenuItem menuTabSoundEffectsOnOff = new JCheckBoxMenuItem( "Sounds" ) ;
//To include tabs default, sand, grass, snake glass
private JMenuItem userGuide = new JMenuItem( "Help" ) ;
//To include tabs default, sand, grass, snake glass
private JMenuItem aboutBox = new JMenuItem( "About" ) ;
/*****Until i think of somemore items at least **************************
/******JaySnake constructor*/
public JaySnake()
{
/*****Sets title for main window*/
//setTitle("JaySnake Game");
getContentPane().setLayout
setResizable( false ) ;
setIgnoreRepaint( true ) ;
game = new Game() ;
getContentPane().add( game, BorderLayout.CENTER ) ;
this.setJMenuBar( menubar );
menubar.add( menu ) ;
menu.add( menuTabStartTab ) ;
menu.add( menuTabChooseBackground ) ;
menu.add( menuTabGameMusicOnOff ) ;
menu.add( menuTabSoundEffectsOnOff ) ;
menu.add( userGuide ) ;
menu.add( aboutBox ) ;
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() ;
}
/******declared class variables/containers*/
private Image backgroundImage ;
}
ASKER
yes I think so I haven't tried it yet, I had to go out for a while but don't worry I will get around to it I think it will be better if I create my menu in a logical step to achieve the goal. Then I can crack on with getting it to work with my menu item
thanks gironis
thanks gironis
ASKER
Well that certainly was quick Tim you are a true expert ... How do you do that? but sorry I don't think I explained myself as I should have done
heres my menu or at lease a simulation of it
THE MENUBAR:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -
Game | Choose background | Sounds | Help
.......................... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .
THE TABS:
GAME
--------
Start
Single or multiplayer - Single player
- Multiplayer
quit
Choose Background
-----------------------
Default
Long grass
Yellow sand
Blue snake glass
Sounds = Checkboxes
--------------------------
Music on / off
Sound effects on /off
Help
-----
User instructions
About box
Does that make any sense ? thanks !
heres my menu or at lease a simulation of it
THE MENUBAR:
--------------------------
Game | Choose background | Sounds | Help
..........................
THE TABS:
GAME
--------
Start
Single or multiplayer - Single player
- Multiplayer
quit
Choose Background
-----------------------
Default
Long grass
Yellow sand
Blue snake glass
Sounds = Checkboxes
--------------------------
Music on / off
Sound effects on /off
Help
-----
User instructions
About box
Does that make any sense ? thanks !
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Absolouteley amazing man! Amazing --- ok I will close this one and I need to have titles for my menu on the next one :)
Regards