-4

I created a GUI named Menu class for my Start, Help and Exit button. my problem is my game won't start, it hangs everytime I press the Start button. Can someone check my error and explain it to me because its my case study at school.


package spaceSip;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;




public class Menu extends JFrame implements ActionListener
{
    private JLabel titleL;
    private JButton startB, helpB, exitB;

    static JFrame frame1 = new JFrame();

    public Menu()
    {
        frame1.setSize(500,250);

        Container mainP = frame1.getContentPane();
        mainP.setLayout(null);

        titleL = new JLabel("WELCOME");
        startB = new JButton("Start");
        helpB = new JButton("Help");
        exitB = new JButton("Exit");


        mainP.add(titleL);
        titleL.setFont(new Font("Chiller",Font.BOLD,50));
        titleL.setBounds(100, 30, 200, 50);

        mainP.add(startB);
        startB.setMnemonic(KeyEvent.VK_S);
        startB.setBounds(200, 80, 100, 20);

        mainP.add(helpB);
        helpB.setMnemonic(KeyEvent.VK_H);
        helpB.setBounds(200, 100, 100, 20);


        mainP.add(exitB);
        exitB.setMnemonic(KeyEvent.VK_E);
        exitB.setBounds(200, 120, 100, 20);


        startB.addActionListener(this);
        helpB.addActionListener(this);
        exitB.addActionListener(this);

        frame1.setVisible(true);
        frame1.setResizable(false);


    }


    public void actionPerformed(ActionEvent e)
    {
        String key = e.getActionCommand();

        if(key == "Start")
        {
            frame1.dispose();
            new SpaceShipMain();
        }

        else if(key == "Help")
        {

        }

        else
            System.exit(0);
    }

    public static void main(String[]args)
    {
        new Menu();
    }

}

package spaceSip;

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

public class SpaceShipMain implements MouseListener
{

    private JFrame background;
    private SpaceShipPanel back;

    public static boolean paused;
    public static boolean crashed;
    public static boolean started;
    public static boolean playedOnce;        

    public boolean goingUp;
    private double upCount;

    public static int distance;
    public static int maxDistance;

    public final int XPOS;
    public final int NUMRECS;
    public final int RECHEIGHT;
    public final int RECWIDTH;

    private int moveIncrement;
    private int numSmoke;

    private ArrayList<SpaceShipImage> toprecs;
    private ArrayList<SpaceShipImage> bottomrecs;
    private ArrayList<SpaceShipImage> middlerecs;
    private ArrayList<SpaceShipImage> smoke;
    private SpaceShipImage helicopter;


    public SpaceShipMain()
    {
        NUMRECS = 100;
        RECHEIGHT = 70;
        RECWIDTH = 30;
        XPOS = 200;
        playedOnce = false;
        maxDistance = 0;

        load(new File("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/Best.txt"));

        initiate();
    }

    public void load(File file)
    {
        try
        {
            Scanner reader = new Scanner(file);
            while(reader.hasNext())
            {
                int value = reader.nextInt();
                if(value > maxDistance)
                    maxDistance = value;
            }
        }
        catch(IOException i )
        {
            System.out.println("Error. "+i);
        }
    }

    public void save()
    {
        FileWriter out;
        try
        {
            out = new FileWriter("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/Best.txt");
            out.write("" + maxDistance);
            out.close();
        }
        catch(IOException i)
        {
            System.out.println("Error: "+i.getMessage());
        }
    }

    public void initiate()
    {
        if(!playedOnce)
        {
            background = new JFrame("Space Ship Game"); 
            background.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes the program when the window is closed
            background.setResizable(false); //don't allow the user to resize the window
            background.setSize(new Dimension(800,500));
            background.setVisible(true);

            back = new SpaceShipPanel("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/starfield.jpg");
            background.add(back);

            back.addMouseListener(this);
        }
        playedOnce = true;
        goingUp = false;
        paused = false;
        crashed = false;
        started = false;

        distance = 0;
        upCount = 0;

        moveIncrement = 2;
        numSmoke = 15;


        toprecs = new ArrayList<SpaceShipImage>();
        middlerecs = new ArrayList<SpaceShipImage>();
        bottomrecs = new ArrayList<SpaceShipImage>();
        smoke = new ArrayList<SpaceShipImage>();

        helicopter = new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rocketship.GIF",XPOS,200);

        for(int x = 0; x < NUMRECS; x++)
            toprecs.add(new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rec2.JPG",RECWIDTH*x,5));
        for(int x = 0; x < NUMRECS; x++)
            bottomrecs.add(new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rec2.JPG",RECWIDTH*x,410));

        middlerecs.add(new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/asteroid2.jpg",1392,randomMidHeight()));
        middlerecs.add(new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/asteroid2.jpg",1972,randomMidHeight()));


        drawRectangles();
    }

    public void drawRectangles()
    {
        long last = System.currentTimeMillis();
        long lastCopter = System.currentTimeMillis();
        long lastSmoke = System.currentTimeMillis();
        long lastSound = System.currentTimeMillis();
        int firstUpdates = 0;
        double lastDistance = (double)System.currentTimeMillis();
        while(true)
        {
            if(!paused && !crashed && started && (double)System.currentTimeMillis() - (double)(2900/40) > lastDistance)
            {   
                lastDistance = System.currentTimeMillis();
                distance++;
            }   


            if(!paused && !crashed && started && System.currentTimeMillis() - 10 > lastCopter)
            {
                lastCopter = System.currentTimeMillis();
                updateCopter();
                updateMiddle();
            }
            if(!paused && !crashed && started && System.currentTimeMillis() - 100 > last)
            {
                last = System.currentTimeMillis();
                updateRecs();
            }
            if(!paused && !crashed && started && System.currentTimeMillis() - 75 > lastSmoke)
            {
                lastSmoke = System.currentTimeMillis();
                if (firstUpdates < numSmoke)
                {
                    firstUpdates++;
                    smoke.add(new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/smoke.GIF",187,helicopter.getY()));
                    for(int x = 0; x < firstUpdates; x++)
                        smoke.set(x,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/smoke.GIF",smoke.get(x).getX() - 12, smoke.get(x).getY()));
                }
                else
                {
                    for(int x = 0; x < numSmoke - 1; x++)
                        smoke.get(x).setY(smoke.get(x+1).getY());
                    smoke.set(numSmoke - 1,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/smoke.GIF",187,helicopter.getY()));
                }
                    }
                    back.updateImages(middlerecs,helicopter,smoke);
                }
    }

    public void updateRecs()
    {
        for(int x = 0; x < (NUMRECS - 1); x++) //move all but the last rectangle 1 spot to the left
        {
            toprecs.set(x,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rec2.JPG",RECWIDTH*x,toprecs.get(x+1).getY()));
            bottomrecs.set(x,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rec2.JPG",RECWIDTH*x,bottomrecs.get(x+1).getY()));
        }

    }



    public void randomDrop()
    {
        toprecs.get(26).setY(toprecs.get(26).getY() + (463 - bottomrecs.get(26).getY()));
        bottomrecs.get(26).setY(463);
    }




    public int randomMidHeight()
    {
        int max = 10000;
        int min = 0;

        for(int x = 0; x < NUMRECS; x++)
        {
            if(toprecs.get(x).getY() > min)
                min = (int)toprecs.get(x).getY();
            if(bottomrecs.get(x).getY() < max)
                max = (int)bottomrecs.get(x).getY();
        }
        min += RECHEIGHT;
        max -= (RECHEIGHT + min);
        return min + (int)(Math.random() * max);
    }

    //moves the randomly generated middle rectangles
    public void updateMiddle()
    {
        if(middlerecs.get(0).getX() > -1 * RECWIDTH)
        {
            middlerecs.set(0,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/asteroid.gif",middlerecs.get(0).getX() - (RECWIDTH/5), middlerecs.get(0).getY()));
            middlerecs.set(1,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/asteroid.gif",middlerecs.get(1).getX() - (RECWIDTH/5), middlerecs.get(1).getY()));
        }
        else
        {
            middlerecs.set(0,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/asteroid.gif",middlerecs.get(1).getX() - (RECWIDTH/5), middlerecs.get(1).getY()));
            middlerecs.set(1,new SpaceShipImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/asteroid.gif",middlerecs.get(0).getX() + 580,randomMidHeight()));
        }
    }

    public boolean shoot()
    {
        for(int x = 3; x <= 7; x++)
            if(helicopter.getY() >= bottomrecs.get(x).getY())
                return true;

        for(int y = 3; y <= 7; y++)
                if(helicopter.getY() <= toprecs.get(y).getY())
                    return true;
        for(int z = 0; z <= 1; z++)
            if(isInMidRange(z))
                return true;
        return false;
    }

    public boolean isInMidRange(int num)
    {
        Rectangle middlecheck = new Rectangle((int)middlerecs.get(num).getX(),(int)middlerecs.get(num).getY(),RECWIDTH,RECHEIGHT);
        Rectangle coptercheck = new Rectangle((int)helicopter.getX(),(int)helicopter.getY(),70,48); //asteroid X and y bump
        return middlecheck.intersects(coptercheck);
    }

    public void crash()
    {
        crashed = true;
        if(distance > maxDistance) 
        {
            maxDistance = distance;
            save();
        }
        int reply = JOptionPane.showConfirmDialog(null, "               RESTART ?", "GAME OVER", JOptionPane.YES_NO_OPTION);    
        if(reply == JOptionPane.YES_OPTION)
            initiate();

        else 
            System.exit(0);


        initiate();
    }

    //moves the spaceship
    public void updateCopter()
    {
        upCount += .10;
        if(goingUp)
        {
            if(upCount < 3.5)
                helicopter.setPosition(XPOS,(double)(helicopter.getY() - (.3 + upCount)));
            else
                helicopter.setPosition(XPOS,(double)(helicopter.getY() - (1.2 + upCount)));
            helicopter.setImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rocketship.GIF"); 
        }
        else
        {
            if(upCount < 1)
                helicopter.setPosition(XPOS,(double)(helicopter.getY() + upCount));
            else
                helicopter.setPosition(XPOS,(double)(helicopter.getY() + (1.2 + upCount)));
            helicopter.setImage("C:\\Users/Travelmate/workspace/GAME/src/spaceSip/rocketship.GIF");
        }
        if(shoot())
            crash();
    }

    //Called when the mouse exits the game window
    public void mouseExited(MouseEvent e)
    {
            paused = true;
    }

    //Called when the mouse enters the game window
    public void mouseEntered(MouseEvent e)
    {

    }

    //Called when the mouse is released
    public void mouseReleased(MouseEvent e)
    {
        goingUp = false;
        upCount = -1;
        if(paused)
            paused = false;
    }

    //Called when the mouse is pressed
    public void mousePressed(MouseEvent e)
    {
        if (!started)
            started = true;
        goingUp = true;
        upCount = 0;
    }

    //Called when the mouse is released
    public void mouseClicked(MouseEvent e)
    {

    }
}

package spaceSip;

import java.awt.Image;

import javax.swing.ImageIcon;

public class SpaceShipImage
{
    private Image image;        //The picture
    private double x;           //X position
    private double y;           //Y position

    //Construct a new Moving Image with image, x position, and y position given
    public SpaceShipImage(Image img, double xPos, double yPos)
    {
        image = img;
        x = xPos;
        y = yPos;
    }

    //Construct a new Moving Image with image (from file path), x position, and y position given
    public SpaceShipImage(String path, double xPos, double yPos)
    {
        this(new ImageIcon(path).getImage(), xPos, yPos);   
            //easiest way to make an image from a file path in Swing
    }

    //They are set methods.  I don't feel like commenting them.
    public void setPosition(double xPos, double yPos)
    {
        x = xPos;
        y = yPos;
    }

    public void setImage(String path)
    {
        image = new ImageIcon(path).getImage();
    }

    public void setY(double newY)
    {
        y = newY;
    }

    public void setX(double newX)
    {
        x = newX;
    }

    //Get methods which I'm also not commenting
    public double getX()
    {
        return x;
    }

    public double getY()
    {
        return y;
    }

    public Image getImage()
    {
        return image;
    }
}

package spaceSip;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

class SpaceShipPanel extends JPanel 
{
    private Image background;           
    private ArrayList<SpaceShipImage> middle;
    private SpaceShipImage copter;
    private ArrayList<SpaceShipImage> smoke;

    //Constructs a new ImagePanel with the background image specified by the file path given
    public SpaceShipPanel(String img) 
    {
        this(new ImageIcon(img).getImage());    
            //The easiest way to make images from file paths in Swing
    }

    //Constructs a new ImagePanel with the background image given
    public SpaceShipPanel(Image img)
    {
        background = img;
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));    
            //Get the size of the image
        //Thoroughly make the size of the panel equal to the size of the image
        //(Various layout managers will try to mess with the size of things to fit everything)
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);


        middle = new ArrayList<SpaceShipImage>();
        smoke = new ArrayList<SpaceShipImage>();
    }

    //This is called whenever the computer decides to repaint the window
    //It's a method in JPanel that I've overwritten to paint the background and foreground images
    public void paintComponent(Graphics g) 
    {
        //Paint the background with its upper left corner at the upper left corner of the panel
        g.drawImage(background, 0, 0, null); 
        //Paint each image in the foreground where it should go

        for(SpaceShipImage img : middle)
            g.drawImage(img.getImage(), (int)(img.getX()), (int)(img.getY()), null);
        for(SpaceShipImage img : smoke)
            g.drawImage(img.getImage(), (int)(img.getX()), (int)(img.getY()), null);
        if(copter != null)
            g.drawImage(copter.getImage(), (int)(copter.getX()), (int)(copter.getY()), null);
        drawStrings(g);
    }

    public void drawStrings(Graphics g)
    {
        g.setColor(Color.WHITE);
        g.setFont(new Font("Arial",Font.BOLD,20));
        g.drawString("Distance: " + SpaceShipMain.distance,30,440);
        g.setFont(new Font("Arial",Font.BOLD,20));
        if (SpaceShipMain.distance > SpaceShipMain.maxDistance)
            g.drawString("Best: " + SpaceShipMain.distance,650,440);
        else
            g.drawString("Best: " + SpaceShipMain.maxDistance,650,440);
        if(SpaceShipMain.paused)
        {

                g.setFont(new Font("Chiller",Font.BOLD,72));
                g.drawString("Paused",325,290);
                g.setFont(new Font("Chiller",Font.BOLD,30));
                g.drawString("Click to unpause.",320,340);
        }
    }

    //Replaces the list of foreground images with the one given, and repaints the panel
    public void updateImages(ArrayList<SpaceShipImage> newMiddle,SpaceShipImage newCopter,ArrayList<SpaceShipImage> newSmoke)
    {
        copter = newCopter;
        middle = newMiddle;
        smoke = newSmoke;
        repaint();  //This repaints stuff... you don't need to know how it works
    }
}
jan kryss
  • 19
  • 1
  • 8
  • 2
    Please post relevant parts only. Your question is vague. – Maroun Feb 11 '14 at 13:32
  • And Nulllayout is Evil : http://www.leepoint.net/notes-java/GUI/layouts/nulllayout.html .... Use LayoutManager :) ... and why you extends from a JFrame... use it as field or something else but not extend it when you gave him no special things. – pL4Gu33 Feb 11 '14 at 13:35
  • hmmn, i guess i dont have any problem at that sir. Do you think my program has a bug ? i can run my SpaceShipMain() in a main method but not in the Button – jan kryss Feb 11 '14 at 13:43
  • I extends it from JFrame because I need that GUI for creating my Menu game sir. – jan kryss Feb 11 '14 at 13:45

1 Answers1

2

You compare Strings with equals(value equality) not with ==(reference equality). For more information read this previous question How do I compare strings in Java? As @AndrewThompson always advice don't use NullLayout

Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.

As you say that your gui is blocked, that may be cause some of your execution code takes too more time and it's executed in the same thread as gui stuff (The Event Dispatch Thread). As you have a while(true) that's block the gui so use a SwingTimer for execute repeatidly task or if that code that takes long time execute it in a background thread using a Swing Worker. Here you have a complete example. When you perform custom painting you should override paintComponent and in first line you have to call super.paintComponent(..) to follow correct chaining. Read more in Painting in AWT-Swing. Another error i see is that you are adding components after calling setVisible(true) without calling revalidate() repaint(). So i recommend to first add components to container then call setVisible(true) at final step.

Community
  • 1
  • 1
nachokk
  • 14,363
  • 4
  • 24
  • 53
  • i have no problem at == sign sir. my problem is. when i call the constructor SpaceShipMain() from the Start button, the game hangs – jan kryss Feb 11 '14 at 13:38
  • @jankryss i edited but you should still compare string with `equals` . The problem may rely in that you are blocking the EDT, you have to use another thread to call `load`. – nachokk Feb 11 '14 at 13:48
  • i already did what you said @nachokk but still hangs. i added all the class in my question, can run it for me and check my error ? – jan kryss Feb 11 '14 at 14:12
  • @jankryss i found another error... you have to call `super.paintComponent()` when you make custom painting. – nachokk Feb 11 '14 at 14:17
  • ok got it sir, so what is the main error in my program causing the game to hang ? you will notice it when you press the Start button – jan kryss Feb 11 '14 at 14:26
  • @jankryss i edited you have a lot of errors. but when you put `while(true)` that's block your gui. Use a swing timer for that!. I edited answer. – nachokk Feb 11 '14 at 14:40