Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Buttons  (Read 9546 times)
0 Members and 1 Guest are viewing this topic.
Offline neoskunk

Junior Member





« Posted 2008-09-23 02:55:41 »

Hi, im very new to java programming but i was wondering how do you make a button that will go to a new screen?


for example i want to make a title screen for my game with a "Play Now" button.  How would i make said button go to a new screen?  Say the choose character screen or something of that nature.  I already now how to make the button just not how to make the button do anything.  This is all in an applet and im trying to only use awt not swing.


thanks for any help,
scov
Offline fletchergames

Senior Member





« Reply #1 - Posted 2008-09-23 02:58:32 »

One way is to use a CardLayout.  Put your title panel (or whatever) in one card.  Then put your game panel in another card.  CardLayout allows you to switch between different cards.  This allows you to switch between any number of screens.
Offline neoskunk

Junior Member





« Reply #2 - Posted 2008-09-23 03:09:25 »

that sounds like exactly what i need.   could you explain how that is done?  sorry im a newb.


thanks,
scov
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline neoskunk

Junior Member





« Reply #3 - Posted 2008-09-23 04:35:16 »

Maybe i was unclear i just want to understand how to make a button open a new panel. 
Offline neoskunk

Junior Member





« Reply #4 - Posted 2008-09-23 05:39:50 »

maybe its not called a panel but you know what i mean.  and im not opposed to using swing if that makes it easier. 
Offline fletchergames

Senior Member





« Reply #5 - Posted 2008-09-23 14:51:29 »

You probably mean you want to open a new Frame.  You mean you want it to be a separate window, right?

1  
2  
3  
Frame f = new Frame();
f.setSize(frameWidth, frameHeight);
f.setVisible(true);


Obviously, you need to add some stuff to the frame if you want it to have anything on it, but that's the basic idea.

I'm assuming you know how to create the ActionListener for the button.

All of this is covered in the Java Tutorial at http://java.sun.com/docs/books/tutorial/.
Offline neoskunk

Junior Member





« Reply #6 - Posted 2008-09-23 15:00:42 »

thanks for the help but actually i do not want to open a new frame.  i would like to keep it all in the applet.  basically i want to be able to create several different screens individually and then be able to link them together with buttons.   is this possible?  and yes i do know how to use the actionlistener.

thanks,
scov
Offline neoskunk

Junior Member





« Reply #7 - Posted 2008-09-23 15:04:56 »

i also do not want to buttons to stay visible after being pushed.  Once the button is pushed the existing card should disappear completely giving way to a completely new screen.
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #8 - Posted 2008-09-23 15:15:01 »

CardLayout is what you want to use.  You create a number of "cards" that are just Panels that contain your screens.  You then tell CardLayout which one you want to display.  You can only display one at a time.  You add an ActionListener to the buttons you want to use to switch screens with.  When the button is clicked you make the switch in the actionPerformed() method.

Offline neoskunk

Junior Member





« Reply #9 - Posted 2008-09-23 15:20:33 »

ok so how do i go about editing each panel and implementing card layout?   

also i found a game that is like what im trying to do.

http://www.javaonthebrain.com/java/3dblox/

you can push each button but then when play is pushed the buttons disappear and the game begins.

thanks,
scov
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline neoskunk

Junior Member





« Reply #10 - Posted 2008-09-23 16:29:59 »

if its easier than explaining you could just post the source for a simple applet that has a button and the button opens a new panel.
Offline Majick

Senior Newbie





« Reply #11 - Posted 2008-09-23 17:05:00 »

simple google search yielded these results:

http://72.5.124.55/docs/books/tutorial/uiswing/layout/card.html

http://journals.ecs.soton.ac.uk/java/tutorial/ui/layout/card.html

you should be able to figure it out from there......
Offline neoskunk

Junior Member





« Reply #12 - Posted 2008-09-23 17:40:42 »

so i have this code so far....

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MegaShot extends Applet implements ActionListener
{

    Button playButton;
    Image backGround;
   
    Panel cardPanel;
    Panel firstP, secondP;
    CardLayout ourLayout;
   

    public void init()
    {
        /*backGround = getImage(getCodeBase(), "background.gif");*/
        cardPanel = new Panel();
        ourLayout = new CardLayout();
        cardPanel.setLayout(ourLayout);
       
        playButton = new Button("Play!");
        playButton.addActionListener(this);
       
        firstP = new Panel();
        firstP.setBackground(Color.blue);
        firstP.add(playButton);
       
        secondP = new Panel();
        secondP.setBackground(Color.green);
       
       
        this.setLayout(new BorderLayout());
        this.add(cardPanel, BorderLayout.CENTER);
       
        cardPanel.add(firstP, "First");
        cardPanel.add(secondP, "Second");
         
    }
       
       
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == playButton)
        {   
            ourLayout.show(cardPanel,"Second");
           
        }
           
    }
   
    public void start()
    {
    }
   
    public void stop()
    {
    }
   
    /*public void paint (Graphics g)
    {
        g.drawImage(backGround, 0, 0, 1000,600, this);
       
       
       
    }*/
   
}


it yeilds a panel with a button that leads to another panel.....however if i leave out the this.setLayout part nothing shows up.  why is this?   also the button will only appear in the top center.  i knew how to move buttons around with setLayout(null);  but how would i go about moving buttons around on separate panels?  also how do i edit individual panels? such as painting and animations etc.


thanks,
scov
         
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #13 - Posted 2008-09-24 02:09:26 »

This is a tutorial on layout managers
http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html

This is The Java Tutorial
http://java.sun.com/docs/books/tutorial/

You can learn all you need to know from these 2 links.  I would recommend doing them both from start to finish.

Offline fletchergames

Senior Member





« Reply #14 - Posted 2008-09-24 02:25:38 »

First of all, your code would be more modular if you separated the code for each panel into separate methods or even separate classes.  Create the first panel with a createFirstPanel method, and create the second panel with a createSecondPanel method.  Put all the code for each panel in its method.

That shouldn't affect how your code works, but it would make it easier to read.

Quote from: neoskunk
however if i leave out the this.setLayout part nothing shows up.  why is this?
Without the call to setLayout, your applet most likely has a FlowLayout.  cardPanel's minimum, preferred, and maximum sizes are probably set to strange values.  And FlowLayout probably uses those sizes.

I think the best thing you could do is look up layouts in the Java Tutorial I linked to before.  You can mix and match layouts to get the effect you want.

Mixing null layouts with other layouts can cause problems.  However, you can create a class that acts like a null layout but can work well with other layouts.  You can learn about this from http://www.swingwiki.org/workaround:fixed_position_layout.  I used to use similar code until I coded my own special set of layouts for my own special widgets.
Offline neoskunk

Junior Member





« Reply #15 - Posted 2008-09-24 03:53:12 »

i have attempted to put each card in a separate method and idealy yes i would love to make them each a seperate class so they could be tested individually.  however i guess i dont quite understand the language yet.  I'm going to go over those links but once i create a panel in say a method or class how do i call it or get it to show?  it can't be the same as just cardLayout.show(....);.   seems like a method call or something is necessary.   anyways thanks for the help.    i'm going to go over those links hopefully i wont be back with such newb questions.

thanks,
scov
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #16 - Posted 2008-09-24 12:28:14 »

Here is a sample of using CarLayout.  Now you should read those tutorials to understand how it works.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TestApplet extends JApplet implements ActionListener {
   JButton button1;
   JButton button2;
   CardLayout card;
   Container contentPane;

   public void init() {
      card = new CardLayout();
      setLayout(card);
      contentPane = getContentPane();

      JPanel panel1 = new JPanel();
      add(panel1, "card1");
      button1 = new JButton("Go to panel 2");
      panel1.add(button1);
      button1.addActionListener(this);

      JPanel panel2 = new JPanel();
      add(panel2, "card2");
      button2 = new JButton("Go to panel 1");
      panel2.add(button2);
      button2.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e){
      Object source = e.getSource();

      if(source.equals(button1)) {
         card.show(contentPane, "card2");
      }
      else if(source.equals(button2)) {
         card.show(contentPane, "card1");
      }
   }
}

Offline neoskunk

Junior Member





« Reply #17 - Posted 2008-09-24 14:31:23 »

thank you very much for the example.  i have a pretty good understanding of the layouts now.   could you adapt that example to create the 2 cards in seperate methods or even classes?    also what are the advantages and disadvantages of swing vs regular awt?  i know swing is better for GUI but in terms of games would you suggest using it?
Offline fletchergames

Senior Member





« Reply #18 - Posted 2008-09-24 15:00:32 »

Look, if you're still having problems with the syntax for classes and methods, you really need to read the Java Tutorial or find some other source of information.  All the information is there, and there's got to be plenty of examples already posted on the Internet.

Swing is far more customizable than AWT.  There may be a speed difference between the two, but both are pretty slow.

Try to avoid them both for the actual game in your program.  If you're making a Super Mario clone, there should be one panel (or whatever) containing the entire game area (i.e. where Mario can move around).  You should store all the objects in the game yourself and draw them to this panel.  Mario should not be a Swing component!!!

You can use components for things like the buttons on the title screen.  They work great for that.  There's no way that your title screen is going to be too slow.  You can even have buttons like that on your main screen, so long as you don't do something silly like make your game characters components.

If you're going to actually use AWT/Swing buttons, tables, etc., I would go with Swing.  If you just need a frame to draw in, I would go with AWT because you shouldn't need any of Swing's features.
Offline neoskunk

Junior Member





« Reply #19 - Posted 2008-09-24 18:06:21 »

alright ill go read up.   

thanks for the help.
Offline neoskunk

Junior Member





« Reply #20 - Posted 2008-09-24 23:04:16 »

ok im making progress now.    i understand how to call methods and classes now and i understand the layout.   however once i put the different cards into their own methods the buttons won't work.   buttons have to stay separate or am i doing something wrong?




1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MegaShot extends Applet implements ActionListener
{
    Button playButton;
   
    Panel cardPanel, titleScreen, charSelect;
    CardLayout myLayout;
   
    public void init()
    {
        cardPanel = new Panel();
        myLayout = new CardLayout();
        cardPanel.setLayout(myLayout);
        this.setLayout(new BorderLayout());
        this.add(cardPanel, BorderLayout.CENTER);
        createTitleScreen(titleScreen,playButton);
        createCharSelect(charSelect);  
       
    }
   
    public void createTitleScreen(Panel p, Button b)
    {
        p = new Panel();
        b = new Button("Play!");
        b.addActionListener(this);
        p.add(b);
        p.setBackground(Color.blue);
        cardPanel.add(p,"title");
    }
   
    public void createCharSelect(Panel p)
    {
        p = new Panel();
        p.setBackground(Color.red);
        cardPanel.add(p,"select");
    }
   
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == playButton)
        {
            myLayout.show(cardPanel,"select");
        }
    }
}
Offline neoskunk

Junior Member





« Reply #21 - Posted 2008-09-25 02:29:08 »

lol ok sorry for being a newb figured it out.
Offline neoskunk

Junior Member





« Reply #22 - Posted 2008-09-25 03:49:33 »

ok so i have gotten this far.....

cardlayout with two cards....each in its own method

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MegaShot extends Applet implements ActionListener
{
    Panel cardPanel, titleScreen, charSelect;
    Button playButton;
    CardLayout myLayout;

    public void init()
    {
        cardPanel = new Panel();
        myLayout = new CardLayout();
        cardPanel.setLayout(myLayout);
        this.setLayout(new BorderLayout());
        this.add(cardPanel, BorderLayout.CENTER);
        createTitleScreen();
        createCharSelect();
    }
   
    public void createTitleScreen()
    {
        titleScreen = new Panel();
        titleScreen.setLayout(null);
        playButton = new Button("Play!");
        playButton.setBounds(600,500,80,30);
        playButton.addActionListener(this);
        titleScreen.add(playButton);
        titleScreen.setBackground(Color.blue);
       
       
        cardPanel.add(titleScreen,"title");
    }
   
       
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == playButton)
        {
            myLayout.show(cardPanel,"select");
        }
    }
 
   
    public void createCharSelect()
    {
        charSelect = new Panel();
        charSelect.setBackground(Color.red);
        cardPanel.add(charSelect,"select");
    }
}



could someone explain a little about how i would make those methods into classes of their own?  also how do i implement graphics?  such as drawRect(....); etc....
Offline neoskunk

Junior Member





« Reply #23 - Posted 2008-09-25 16:08:38 »

Could someone please explain how to make each panel into its own class.   i cannot figure it out or how to implement buttons and graphics from a separate class.
Offline fletchergames

Senior Member





« Reply #24 - Posted 2008-09-25 16:37:20 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public class TitlePanel extends Panel {
   public TitlePanel(ActionListener playButtonListener) {
        super();
        setLayout(null);
        Button playButton = new Button("Play!");
        playButton.setBounds(600,500,80,30);
        playButton.addActionListener(playButtonListener);
        add(playButton);
        setBackground(Color.blue);
   }

   public void paint(Graphics g) {
       //do all the stuff that paint is suposed to do
      super.paint(g);

       //do all your custom graphics here
  }
}


This class just puts all the code for defining the title panel in its constructor.  TitlePanel extends Panel, so it is a Panel, just with additional behavior.  You will need to look up subclassing in the Java Tutorial.

Do all the graphics within the paint method.  This method already exists within Panel.  It gets called whenever the applet is drawn.  By overriding this method in the TitlePanel class, you're just changing its behavior.

Within your original class, you just need to add the code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
ActionListener playButtonListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
            myLayout.show(cardPanel,"select");
    }
};

titleScreen = new TitlePanel(playButtonListener);

//and don't forget to add it to the card layout


I changed the ActionListener from being one listener for all the buttons (of which there was only one) to be one listener for each button.  This makes it easier because you don't have to store playButton or check which button was pressed any more.

When you define a class like I did there, that's called an anonymous class.  That's something else you should look up.

Look, you should just read the whole Java Tutorial, and then you won't need to ask all these questions.
Offline neoskunk

Junior Member





« Reply #25 - Posted 2008-09-25 17:42:31 »

im sorry for all the questions i have been through the java tutorial more than once and i have tried to do things myself but it gets frustrating. 

anyways i think i understand thanks for the examples.
Offline neoskunk

Junior Member





« Reply #26 - Posted 2008-09-25 20:22:24 »

Could you explain why the actionlistener goes in the parameter?

also is this supposed to be a nested class or outside the applet class?
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #27 - Posted 2008-09-25 21:51:13 »

That's called an anonymous class.  You can create a class on the fly without giving it a name.  It is useful in these situations where you might have a lot of ActionListeners.

Offline neoskunk

Junior Member





« Reply #28 - Posted 2008-09-25 22:16:09 »

ok all goes well.  now im creating a character select screen.  the characters i draw here i would like to implement in game play and other panels.   what would be the best way to create these?   put them in their own classs?   method?     maybe put each character on a transparent panel? 

any thoughts? suggestions?


also why is actionlistener a parameter?
Offline neoskunk

Junior Member





« Reply #29 - Posted 2008-09-26 06:51:53 »

so basically each character will be a sprite.   i figured it out!    guess those tutorials help more than one might think.
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (44 views)
2014-09-23 14:38:19

radar3301 (25 views)
2014-09-21 23:33:17

BurntPizza (62 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (39 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!