-1

I am programming a top-down shooter game, and I want to open a new window between each level of the game. After each level, if a certain criteria is met then the following class is implemented:

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Image.*;
import java.awt.image.BufferedImage.*;
import java.text.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.text.*;
import javax.imageio.ImageIO;
import java.io.InputStream.*;
import java.io.*;

public class Shop extends JFrame implements Runnable
{
    private static java.awt.image.BufferedImage shotgun, zombie, armor, aRifle;
    static ArrayList<Image> stock = new ArrayList<Image>();
    private ColorPanel contentPane3;
    private JPanel container = new JPanel();
    private JPanel contentPane4;
    private int item=0;
    private boolean broke = false;
    private boolean newb = true;
    private boolean reg = false;
    private boolean stay = true;

    public static void main(String [] args)
    {
        System.out.println("started");
        Thread s = new Thread( new Shop () );
        System.out.println("running");
        s.start();
        System.out.println("ended");
    }

    public Shop()
    {        
        super("Monster Escape v4");

        try
        {
            shotgun = ImageIO.read(getResource("shotgun.png"));        
            zombie = ImageIO.read(getResource("zombie.png"));        
            armor = ImageIO.read(getResource("armor.png"));
            aRifle = ImageIO.read(getResource("assaultRifle.png"));
        }catch (IOException e){System.exit(0);}

        stock.clear();
        stock.add(armor);
        if(!(Client.inventory.contains("shotgun")))
            stock.add(shotgun);            
        if(!(Client.inventory.contains("assaultRifle")))
            stock.add(aRifle);

        container.removeAll();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane4 = new JPanel();
        contentPane4.setPreferredSize(new Dimension( 700,100 ));
        contentPane3 = new ColorPanel(Color.WHITE);
        contentPane3.setPreferredSize(new Dimension( 700,700 ));
        setBounds( 100,100,606,719 );
        setResizable(false);

        GridLayout g1 = new GridLayout(1, 3);
        contentPane4.setLayout(g1);
        container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));

        JButton sleft = new JButton("←");
        JButton sright = new JButton("→");
        JButton buy = new JButton("buy");
        JButton leave = new JButton("leave");

        contentPane4.add(sleft);
        contentPane4.add(buy);
        contentPane4.add(sright);
        contentPane4.add(leave);

        sleft.addActionListener((e) -> {
            broke = false;
            newb = false;
            if(item==0)
                item = stock.size()-1;
            else
                item--;
        });

        sright.addActionListener((e) -> {
            broke = false;
            newb = false;
            if(item==stock.size()-1)
                item=0;
            else
                item++;
        });

        buy.addActionListener((e) -> {
            broke = false;
            newb = false;
            if(stock.get(item) == armor)
            {
                if(Client.cash >= 100)
                {
                    Client.maxHealth += 4;
                    Client.cash -= 100;
                    reg = true;
                }
                else
                    broke = true;
            }
            else
            {
                if(Client.cash >= 250)
                {
                    Client.inventory.add(getName(item));
                    stock.remove(item);
                    Client.cash -= 250;
                    item--;
                    reg = true;
                }
                else
                    broke = true;
            }

        });

        leave.addActionListener((e) -> {
            stay = false;
        });

        container.add(contentPane3, BorderLayout.EAST);
        container.add(contentPane4, BorderLayout.WEST);
        setContentPane(container);



        setVisible(true);
    }

    public static String getName(int i)
    {
        String temp = null;
        if(stock.get(i) == armor)
            temp = "armor";
        else if(stock.get(i) == shotgun)
            temp = "shotgun";
        else if(stock.get(i) == aRifle)
            temp = "assaultRifle";
        return temp;
    }

    private InputStream getResource(String ref) throws IOException
    {
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(ref);
        if( in != null )
            return in;
        return new FileInputStream(ref);

    }

    public void run()
    {


        while(stay)
        {
            repaint();

            try
            {
                Thread.sleep(10);
            }catch( InterruptedException e ) { }

        }
        dispose();
    }


}

This includes a ColorPanel subclass that isn't shown. In previous versions of my game, this has worked fine. However, I recently added a KeyListener subclass to my Client, and now nothing happens when I try to instantiate the thread. I created a main method in the Shop class itself to see where the issue is, and it seems that when the code reaches Thread s = new Thread( new Shop () ); the system terminates entirely, since when I run main it only prints "started." What is causing this?

  • Does the program actually terminate? – Evan Knowles May 10 '17 at 14:01
  • I checked the method calls and it says" VM terminated." However, a closer look at my code revealed that it was catching an IOException when I tried to instantiate my images, an IOException that I had told the program to exit if it encountered it. The method is running fine. – Jack Burraston May 10 '17 at 14:03
  • 2
    Yes, this line isn't helping. `catch (IOException e){System.exit(0);}`. – Evan Knowles May 10 '17 at 14:03
  • The issue was that I had moved the package to somewhere else on my computer, so it had lost the locations for the images and was throwing an IOException. That line was exactly what was causing the problem. I moved the images and now it works. – Jack Burraston May 10 '17 at 14:04
  • 1
    Always at least log exceptions, even if there's "no way" they could be thrown. – Evan Knowles May 10 '17 at 14:06

1 Answers1

0

add s.join();

after start. In this case main thread will wait until s thread will be finished.

Maxim Tulupov
  • 1,621
  • 13
  • 8