Buttons

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?

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");
        }
    }
}

lol ok sorry for being a newb figured it out.

ok so i have gotten this far…

cardlayout with two cards…each in its own method

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…

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.

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:

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.

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.

Could you explain why the actionlistener goes in the parameter?

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

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.

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?

so basically each character will be a sprite. i figured it out! guess those tutorials help more than one might think.

fletchergames gave a very good sugestion.

You should not use Swing components for the buttons of your game.

Use image instead. A Sprite.
You could create a class button that basically contains the x,y axis of the button in the screen, it’s size.
You can twist the class adding a list of images this button can have, methods for positioning, etc…

Create a png file for the button ( bitmap, jpg, gif, whatever ). Use a BufferedImage to save that png on memory and then draw it on the screen using the drawImage method of your Graphics object in the x,y position.

Using mouse action listeners, you can get x,y output of a mousePressed event and then check if the button X postion plus its Width intersects X you got from mousePressed, same thing to Y axis of the button and its Height.
And do whatever action you need.

If you want to use Swing buttons, you can still give them images. Just override the paintComponent method of JButton. Or, since he seems to be using AWT, I think he would have to override the paint method of Button.

I went completely overboard and created my own “widgets” that are equivalent to components in most ways. The reason I went to all that trouble was because I wanted to make image buttons that stretched appropriately when the window was resized. This wasn’t too hard, but the layout I had created didn’t work too well with normal Swing components. So I wound up doing a couple more widgets.

By that point, it was always as easy to do “one more thing” as it was to go back. And then my 6-month project took over a year to complete (just finishing up now).

I like the result, but it probably wasn’t worth that much effort. Plain AWT/Swing image buttons are much easier to implement.

I plan to eventually make my widget library (such as it is) available on my website. Some people might find it useful. It runs on top of Swing, but my widgets generally have a much smaller memory footprint than Swing components. On the other hand, they aren’t as configurable. And I’ve only implemented so many different kinds of widgets.

Read http://fivedots.coe.psu.ac.th/~ad/jg/. Also, purchase the whole book, Killer Game Programming in Java. Also, purchase Developing Games in Java. These two books will teach you all about sprites. They have nothing to do with Awt or Swing.

ActionListener is a parameter just so that you can get it from your main class into the TitlePanel class. You have to send some kind of information from one class to the other.

so in effect you can make anything act as a button? create my sprite and then when the mouse is over its coordinates and clicked the applet takes action? in terms of animation it would be better to draw my characters rather than make them a png file right?

ok how about this. the only animations the character will need is to move arms and legs. so could i create a gif image of the character minus say his arm and then when i needed to move the arm i could just draw it? or would that look funny?

No. Just create complete frames of animation. You need to keep it simple at this point.

Here is an excellent tutorial on making sprites: http://www.zoggles.co.uk/asp/tutorials.asp?tut=17
Here is one on using them: http://www.cokeandcode.com/spaceinvaderstutorial

so far i created a class with bufferedimage methods for each character. the buffered image background is transparent and the character is all that shows up. so let me make sure im thinking this through correctly. during gameplay i want the characters to appear like they are walking only if a direction arrow(left or right) is pushed. this can just be a loop of sorts right? like while the key is pushed loop through the animation frames?

Lookup David Brackeen on google search for his 2003 book search for the Animation class, that’s all you need to animate a bunch of images.

when left is pressed(and hold) you use the leftAnimation
when released you use the standardAnimation.

and a sprite has 1 animation

No, you don’t want to make your sprites be buttons.

There’s nothing wrong with getting your characters from a .png file. Typically, that’s what you would do because an artist would be the one doing the art.

In theory, you could. I read something about this once, though I don’t have the URL any more. It’s just not something people normally do. Have the artist draw several frames so that you have animations.

You really need to read one of the books I mentioned or a tutorial or something. Most of your questions are about things that you should learn from reading these books.

fletcher you said i dont want my sprites to be buttons why is that? i didnt mean make them actual buttons but on the character select screen i want the user to click on the character they want to use. so far i have drawn the characters and then used mousemotionlistener and mouselistener so that when the user clicks in the coordinates of the character the applet shows the next panel or whatever. the animation doesnt occur until in the game is there anything wrong with my method so far?

No. You are on the right track.