hi all, I am trying to design a game app that uses a swing gui as a main menu. so far I have been a little confused as to how I get my 'start new game' button to actually launch the game. I have googled event handlers and cannot seem to find anything. I have 2 classes for my project one is the gui, the other is the game itself. the gui class has an inner class that deals with events. the gui class is named menuGUI and the game class is just called game. I suppose the issue is that the event handler needs to call the 'game' class but im a little unsure as to how to call other classes from one class. any help would be much appreciated.

Your handler just needs to create an instance of the game class if you don't already have one and call whatever method starts the game

Game game = new Game();
game.start();

Hi there Ezzaral, thanks for the reply. I have tried your suggestion but it doesnt seem to do anything. ill upload the code so you can see for yourself.cheers.

Ah, your game is an applet. You should convert it to a JFrame or JPanel if you wish to run it from your GUI. That is only a minimal bit of work. You'll need to do the init() work in either your constructor or call the method yourself after you have created the NotPong object. You'll also need to create a run() method that creates and starts a new Animator thread.

HI there again, thanks for the advice ill give it a bash now. will let you know how it pans out, cheers again

Here are the minimal changes to get it running as a JFrame

/*Main Menu Graphical User Interface
 * Phillip Wells 10/12/07
 */

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


public class MenuGUI extends JFrame 
{
    public static void main (String [] args)
    {
    new MenuGUI();
    }


private JButton buttonnew, buttonexit;
private JLabel label1;

public MenuGUI()
    {
    this.setSize(250,100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Game");
    
    ButtonListener b1 = new ButtonListener();

    JPanel panel1 = new JPanel();
    buttonnew = new JButton("New Game");
    buttonnew.addActionListener(b1);
    panel1.add(buttonnew);
    this.add(panel1);
    
    buttonexit = new JButton("Exit Game");
    buttonexit.addActionListener(b1);
    panel1.add(buttonexit);
        
    label1 = new JLabel("Phillip Wells");
    panel1.add(label1);
      
    this.setVisible(true);
    }

private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == buttonnew)
            {
                NotPong game = new NotPong();
                game.init();
            }    
            else if (e.getSource() == buttonexit)
            {
                System.exit(0);
            }
        }
    }    


}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.concurrent.*;

public class NotPong extends JFrame {
    public static final int WIDTH = 400;
    public static final int HEIGHT = 400;
    
    private PaintSurface canvas;
    
    public void init() {
        this.setSize(WIDTH, HEIGHT);
        canvas = new PaintSurface();
        this.getContentPane().add(canvas, BorderLayout.CENTER);
        ScheduledThreadPoolExecutor executor =
                new ScheduledThreadPoolExecutor(3);
        executor.scheduleAtFixedRate(new AnimationThread(this),
                0L, 20L, TimeUnit.MILLISECONDS);
        setVisible(true);
    }
    
    
    
    class AnimationThread implements Runnable {
        JFrame c;
        
        public AnimationThread(JFrame c) {
            this.c = c;
        }
        
        public void run() {
            c.repaint();
        }
    }
    
    class PaintSurface extends JComponent {
        int paddle_x = 0;
        int paddle_y = 360;
        
        int score = 0;
        float english = 1.0f;
        
        Ball ball;
        
        Color[] color = {Color.RED, Color.ORANGE,
        Color.MAGENTA, Color.ORANGE,
        Color.CYAN, Color.BLUE};
        int colorIndex;
        
        public PaintSurface() {
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseMoved(MouseEvent e) {
                    if (e.getX() - 30 - paddle_x > 5)
                        english = 1.5f;
                    else if (e.getX() - 30 - paddle_x < -5)
                        english = -1.5f;
                    else
                        english = 1.0f;
                    paddle_x = e.getX() - 30;
                }
            } );
            ball = new Ball(20);
        }
        
        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            Shape paddle = new Rectangle2D.Float(
                    paddle_x, paddle_y, 60, 8);
            
            g2.setColor(color[colorIndex % 6]);
            
            if (ball.intersects(paddle_x, paddle_y, 60, 8)
            && ball.y_speed > 0) {
                ball.y_speed = -ball.y_speed;
                ball.x_speed = (int)(ball.x_speed * english);
                if (english != 1.0f)
                    colorIndex++;
                score += Math.abs(ball.x_speed * 10);
            }
            
            if (ball.getY() + ball.getHeight()
            >= NotPong.HEIGHT) {
                ball = new Ball(20);
                score -= 1000;
                colorIndex = 0;
            }
            ball.move();
            g2.fill(ball);
            
            g2.setColor(Color.BLACK);
            g2.fill(paddle);
            g2.drawString("Score: " + score, 250, 20);
            
        }
    }
    
    class Ball extends Ellipse2D.Float {
        public int x_speed, y_speed;
        private int d;
        private int width = NotPong.WIDTH;
        private int height = NotPong.HEIGHT;
        
        public Ball(int diameter) {
            super((int)(Math.random() * (NotPong.WIDTH - 20) + 1),
                    0, diameter, diameter);
            this.d = diameter;
            this.x_speed = (int)(Math.random() * 5 + 5);
            this.y_speed = (int)(Math.random() * 5 + 5);
        }
        
        public void move() {
            if (super.x < 0 || super.x > width - d)
                x_speed = -x_speed;
            if (super.y < 0 || super.y > height - d)
                y_speed = -y_speed;
            super.x += x_speed;
            super.y += y_speed;
        }
    }
}

THanks a lot, will check it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.