Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Quick question about my alignment ... on: 2006-04-22 06:05:07
Why is my JMenuBar aligned in the centre?

As well as my buttons in the buttom panel ...

I would like to have the top menubar aligned to the left of the panel!

Any ideeas?



/*
 * Invaders.java
 *
 * Created on 27 May 2005, 15:06
 */

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

/**
 * This class creates the JFrame, adds a quit button
 * to it and contains the <b>main</b> method to start the
 * program
 *
 */
public class Invaders
    implements ActionListener
{
    //The game window
    private JFrame frame = null;
   
    //The buttons
    private JButton quit = null;
    private JButton restart = null;   
    private JLabel name = null;
    private JLabel score = null;
   
    private JMenuBar menubar;
    private JMenu file, options;   
    private JMenuItem restart_game, quit_game, change_speed, change_rows, change_name;
   
   
   
    //The JPanel which runs and draws the game
    private Game game = null;

    /** Creates a new instance of Invaders */
    public Invaders()
    {
        //Create the window and buttons
        createGUI();       
       
        //Make our window get the focus
        frame.getContentPane().requestFocusInWindow();

        //Don't allow the focus to move around in our window
        frame.getContentPane().setFocusTraversalKeysEnabled(false);

        //Show the frame to the player
        frame.setVisible( true );
    }

    /**
     * Create the JFrame and all user interface components
     */
    private void createGUI()
    {
        //Setup the frame with an appropriate title
        frame = new JFrame( "Invaders From Space!" );
       
        //Make the window a decent size and position it's top
        //left corner at 0,0
        frame.setBounds( 0, 0, Game.GAME_WIDTH, Game.GAME_HEIGHT );

        //Set the layout of the window
        frame.getContentPane().setLayout( new BorderLayout() );

        //Add the button to the window
        createButtons();       
       
        //Creates a dialog box with a dropdown menu asking the player to make a selection
        Object[] possibleValues = { "1", "2", "3", "4" };
        Object selectedValue = JOptionPane.showInputDialog(null,
            "Select number of rows of Aliens", "",
            JOptionPane.INFORMATION_MESSAGE, null,
            possibleValues, possibleValues[0]);
           
        if (selectedValue == null)
        {
            selectedValue = "1";
        }
       
        int numRows = Integer.parseInt((String)selectedValue);
       
        selectedValue = null;
       
        //Displays a drop-down dialogue box asking the player to select the speed.
        Object[] possibleSpeedValues = { "Slow", "Medium", "Fast" };
        selectedValue = JOptionPane.showInputDialog(null,
            "Select speed", "",
            JOptionPane.INFORMATION_MESSAGE, null,
            possibleSpeedValues, possibleSpeedValues[0]);
           
        if (selectedValue == null)
        {
            selectedValue = "Slow";
        }
        String speedString = (String)selectedValue;

        //Whenever a value is chosen from the dropdown box,
        //the speed is incremented by one
        int speed = 0;
        if (speedString.equals("Slow"))
        {
            speed = 1;
        }
        else if (speedString.equals("Medium"))
        {
            speed = 2;
        }
        else if (speedString.equals("Fast"))
        {
            speed = 3;
        }

        //Instatiate the class the runs the game
        game = new Game(score, numRows, speed);

        //Add the game to the middle of the JFrame
        frame.getContentPane().add(game, BorderLayout.CENTER );

        //Add a listener to the window so it closes
        //properly
        frame.addWindowListener(
            new WindowAdapter()
            {
                public void windowClosing( WindowEvent e )
                {
                    //Call the exit method
                    doExit();
                }
            }
        );

    }

    /**
     * Create buttons needed by the game
     */
    private void createButtons()
    {
        //Create a JPanel to put the buttons in
        JPanel p = new JPanel(  );
       
        //Create a JPanel to put the menu bar in
        JPanel t = new JPanel(  );
       
       
        //Initialise the buttons
        quit = new JButton( "Quit" );
        restart = new JButton( "Restart" );       
        name = new JLabel (JOptionPane.showInputDialog("Please type your name"));
        name.setForeground(Color.BLACK);
        name.setFont(new Font("Arial", Font.BOLD, 18));
   
        score = new JLabel ("0");
        score.setForeground(Color.RED);
        score.setFont(new Font("Arial", Font.BOLD, 18));

        //Add an action listener to the buttons
        //so we know when they are pressed
        quit.addActionListener( this );
        restart.addActionListener( this );
       
        //Add the buttons to the button panel
        p.add( quit );
        p.add( restart );
        p.add( name );
        p.add( score );

        //Add the button panel to the bottom of the
        //window
        frame.getContentPane().add( p, BorderLayout.SOUTH );
       
        //Initialise the menubar
        menubar = new JMenuBar();
       
        restart_game = new JMenuItem("Restart Game");
        quit_game = new JMenuItem("Quit Game");
        change_speed = new JMenuItem("Change Speed");
        change_rows = new JMenuItem("Change Rows");
        change_name = new JMenuItem("Change Name");
       
       
        //Add the panel to the top of the
        //window
        frame.getContentPane().add( t, BorderLayout.NORTH);
       
        //Add the menubar to the panel
        t.add( menubar ); 

        file = new JMenu("File");
        menubar.add(file, BorderLayout.WEST);
       
        menubar.add( new JSeparator());
       
        options = new JMenu("Options");
        menubar.add(options, BorderLayout.WEST);
       
        restart_game.addActionListener(this);
        quit_game.addActionListener(this);
       
        file.add(restart_game);
        file.add(quit_game);
        options.add(change_speed);
        options.add(change_rows);
        options.add(change_name);

    }

    /**
     * Make sure the application exits properly
     */
    private void doExit()
    {
        System.exit( 0 );
    }

    /**
     * Listen for button presses, etc.
     */
    public void actionPerformed( ActionEvent e )
    {
        //If the quit button is pressed
        if( e.getSource() == quit )
        {
            //Exit the program
            doExit();
        }
       
        //If the restart button is pressed
        if( e.getSource() == restart )
        {
            game.restart();
            //Screen focus changes to the game window
            game.grabFocus();
        }
       
        //If the restart button is pressed
        if( e.getSource() == restart_game )
        {
            game.restart();
            //Screen focus changes to the game window
            game.grabFocus();
        }
       
        //If the restart button is pressed
       
        if( e.getSource() == quit_game )
        {
            doExit();
        }
       
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        new Invaders();
    }

}
2  Game Development / Newbie & Debugging Questions / Re: Game scoreboard on: 2005-10-20 02:59:39
Im using java 2d
3  Game Development / Newbie & Debugging Questions / Game scoreboard on: 2005-10-19 08:33:41
Hi..

Im making this Mario Brother's dtyle game and im looking to add a high score board at the end of each game.

I want it to appear on top of the graphics in the main screen.

Anyone has any documentation or ideeas on how it could be done?

Cheers
4  Game Development / Newbie & Debugging Questions / Re: Developing space invaders... one issue. pls help!! on: 2005-08-27 09:56:30
Thanks, I followed some of your advice and figured it out, but now i stumbled into another issue.

Once my aliens have reached the edges I want them to move down once, then move to the left /right.

So what I came up with is:

/**
     * Checks the position of the alien, then moves them to the right and left
     * until they hit the boundaries. Then they rebound
     *
     */   
    private void checkPosition()
    {
        int maxLeft = 300;
        int maxRight = 300;
        int currentY = 0;
       
        for ( int ee=0; ee<32; ee++)
        {
            alien = (Alien)aliens.get(ee);

            if( alien.getXPos() > maxRight)
            maxRight = alien.getXPos();
            else if( alien.getXPos() < maxLeft )
            maxLeft = alien.getXPos();
           
            currentY = alien.getYPos();
        }
       
        //If they hit the right edge, they will stop moving right and start moving left.
        if ( maxRight >= 600)
        {
           
            for ( int ee=0; ee<32; ee++)
            {
               
                alien = (Alien)aliens.get(ee);
                alien.setMovingRight(false);
                alien.setMovingDown(true);
                               
                if (currentY > 0)
                {
                    alien.setMovingLeft(true);
                    alien.setMovingDown(false);
                }
               
               
            }
           
        }
       
        //If they hit the left edge, they will stop moving left and start moving right
        if ( maxLeft <= 10)
        {
            for ( int ee=0; ee<32; ee++)
            {
               
                alien = (Alien)aliens.get(ee);
                alien.setMovingLeft(false);
                alien.setMovingRight(true);     
               
            }
        }
    }
   
But I think my 'currentY' methodology isnt working. Anyone can bring some light into this?

Thanks
5  Game Development / Newbie & Debugging Questions / Developing space invaders... one issue. pls help!! on: 2005-08-23 06:45:24
Im just trying to finish off this space invaders game, but i ran into a problem.

Basically Ive got the aliens to move left-right on the top of the screen, but when they hit the left/right boundary they dont rebound as a block. They rebound separately.. and then while doing that, they overlap eachother.

How can I make them rebound as a group rather than separately. Eg: The whole group to move from left to right.



public void alienArray()
    {
        alienCount = 0;
        aliens = new ArrayList();

        for (int i=0; i<4; i++)
        {
            for (int ii=0; ii<8; ii++)
            {
                Alien alienA = new Alien(10+ii*30,10+i*30);
                alienA.setMovingRight( true );               
               
                aliens.add( alienA );               
                alienCount++;
            }
        }

....
...

 private void checkPosition()
   
    {
        //Alien alienB = new Alien(10, 10);
       
       
       
        for ( int ee=0; ee<32; ee++)
        {
            alien = (Alien)aliens.get(ee);
           
            if ( alien.getXPos() == 600)
            {
               
                alien.setMovingRight(false);
                alien.setMovingLeft(true);
            }
           
            if ( alien.getXPos() == 10)
            {
               
                alien.setMovingLeft(false);
                alien.setMovingRight(true);
            }
           
           
        }

Any ideeas?

Thanks
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (59 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!