1

I am having trouble calling a java class from another class when a button is pressed.

When I press Play the other frame comes up with the screen is white and I can't even exit it. other than shutting the whole eclipse program off. The new frame that pop's up is just a white screen nothing else from Main Class is coming up like the character.

This is my Menu class code which contains the button Play which the user will press.

        import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.LayoutManager;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;







    import javax.swing.BoxLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class Menu implements ActionListener {

        JButton Play, Scoreboard;

        public static void main(String[] args) throws InterruptedException 
        {

            Menu myWindow = new Menu();

        }

        public Menu() {     

            JFrame frame = new JFrame("Fruit Catcher");
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());

            // Filler panel to fill in the empty space to get button panel centered.

            JPanel filler = new JPanel();
            filler.setPreferredSize(new Dimension(180,180));

            ImageIcon junglebackground = new ImageIcon("junglebackground.jpg");
            JLabel backgroundimage = new JLabel(junglebackground);

            frame.add(backgroundimage);
            frame.setSize(700,470);
            frame.setResizable(false);
            frame.setVisible(true);

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout((LayoutManager) new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));

            Play = new JButton("Play");
            Scoreboard = new JButton("Scoreboard");
            Play.setAlignmentX(Component.CENTER_ALIGNMENT);
            Scoreboard.setAlignmentX(Component.CENTER_ALIGNMENT);
            JLabel gap = new JLabel("\n");

            Play.addActionListener(this);
            Scoreboard.addActionListener(this);

            buttonPanel.add(Play);
            buttonPanel.add(gap);
            buttonPanel.add(Scoreboard);


            panel.add(buttonPanel, BorderLayout.CENTER);
            panel.add(filler, BorderLayout.NORTH);
            frame.add(panel);

        }

        public void actionPerformed(ActionEvent e) { 

            if(e.getSource().equals(Play))
            {   
                String[] args = {};
                try {
                    Main.main(args);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }

            }
            if(e.getSource().equals(Scoreboard))
            {   

                System.out.println("test");
            }

        }
    }

This is Main java class which is the Level . it contains character and fruit. the character can move.

    import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.*;

import javax.swing.*;

public class Main extends JPanel 
{
    Character player = new Character(this);
    movement fruit = new movement(this);

    public Main() 
    {
        addKeyListener(new KeyListener() //KeyListener for the main
        {
            public void keyTyped(KeyEvent e) 
            {

            }

            public void keyReleased(KeyEvent e) 
            {
                player.keyReleased(e);
            }

            public void keyPressed(KeyEvent e) 
            {
                player.keyPressed(e);
            }
        });
        setFocusable(true); //Allows the focus to be on the main and enables movement
    }

    private void move() 
    {
        player.move();
    }

    public void paint(Graphics g) 
    {
        super.paint(g);
        Graphics2D Player = (Graphics2D) g;
        player.paint(Player);   

        Graphics2D Fruit = (Graphics2D) g;
        fruit.paintComponent(Fruit);
    }

    public static void main(String[] args) throws InterruptedException 
    {

        JFrame frame = new JFrame("Fruit Catcher");
        Main game = new Main();
        frame.add(game);
        frame.setSize(700, 450);
        frame.setVisible(true);
        while (true) 
        {
            game.move();
            game.repaint();
            Thread.sleep(5); //Control speed of player
        }
    }
}
DontMatter
  • 11
  • 5

3 Answers3

0

Quick question, do you really sleep for only 5 ms? Perhaps 40-50 ms would be better (it is anyway 25 pfs!)

Thread.sleep(5); //Control speed of player
slux83
  • 686
  • 9
  • 20
0

The public static void main(String[] args) is a special method in Java. This is always the first method that gets called when the program start. You seem to be calling it from your click on Play.

Actually, the fact that you have 2 public static void main(String[] args) makes thing quite confusing. Which one is going to get called when the program starts? This is determined by the telling java which one to call from the .jar manifest file (a text file that tells java different things about your program).

I would personally revisit the design of the program to avoid having 2 main methods.

Math
  • 36
  • 2
  • Any tips on how to fix this issue or how to call a whole java class when button is pressed? – DontMatter Apr 09 '15 at 16:02
  • It's a bit ehh. I'm working with someone this is for a project. I worked on the menu. The Menu code should be run first when its run from eclipse. then when User clicks play it should somehow call the Main java class. It probably shouldn't be named Main but w.e – DontMatter Apr 09 '15 at 16:04
  • Probably deciding first what is your "most important" element. Looks to me that it is your JPanel. Afterwards, add the menu to the JPanel . As described here: http://stackoverflow.com/questions/4299846/add-jmenubar-to-a-jpanel – Math Apr 09 '15 at 16:04
  • So you would remove the main method from the Menu. When calling the constructor of the menu from the JPanel, you can pass the instance of the JPanel as a parameter. That will give you a way to call the methods on it. – Math Apr 09 '15 at 16:08
0

void main() is the method where your execution starts. A program can have only one main method.

The button etc. to start the game should be in the main method, and the other classes' methods should be changed to a different name, for example, game.

You can call class methods with(syntax): object.method(), or for static classes, class.method()

MerajA
  • 194
  • 5
  • 19