Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (482)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Stop old Thread when a new one will be initialized  (Read 1393 times)
0 Members and 1 Guest are viewing this topic.
Offline xRot

Senior Newbie





« Posted 2009-06-17 21:20:15 »

Hi

I got several Threads:
one to Listen the Joystick input and fire an event when a significant change was detected, works very good with jinput.
the other one is a swing jFrame shown for several seconds when a event was fired.

The basic was my app does is listen to the joystick -> execute a action and displays a JFrame what happend.

I dont wanna that 2 JFrames show up at the same time, when a new JFrame should be opened the older one should dispose before the new one shows up.

1  
2  
3  
4  
5  
6  
7  
...
while(queue.getNextEvent(event)) {
      StringBuffer buffer = new StringBuffer();
      Component comp = event.getComponent();
      JoyStickAction.action(comp, event.getValue());
}
...

the joystick listener executing the action class

1  
2  
3  
4  
5  
...
if(display_window) {
            new DisplayInfo("title", window_text, 250, 50, 1000).start();
}
...

at the end of the action class

DisplayInfo is the class that opens the JFrame I wanna close when already a instance was createt

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
import javax.swing.*;
import java.awt.*;
public class DisplayInfo extends Thread {
    //title and text
   String title;
    String text;

    //size in pixels
   int width = 400;
    int height = 100;

    //time to display in ms
   int time = 3000;

    //class infos
   static int instances = 0;
    static DisplayInfo instance;

    public DisplayInfo(String title, String text, int width, int height, int time) {
        if(instances>0) {
            instance.stopInstance();
        }
        ++instances;
        instance = this;
        this.title = title;
        this.text = text;
        this.width = width;
        this.height = height;
        this.time = time;
    }

    public void run() {
        //Create and set up the window.
       JFrame frame = new JFrame(title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true); //removes the borders

        //add text to the frame
       JLabel label = new JLabel(instances+" - "+text, JLabel.CENTER);
        label.setForeground(new Color(0xBB, 0xBB, 0xBB));
        frame.getContentPane().add(label);

        //set size
       frame.setSize(width, height);

        //get screen size
       Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();

        //Calculate the frame location
       int x = (screenSize.width - frame.getWidth()) / 2;
        int y = ((screenSize.height - frame.getHeight()) / 3) * 2;

        //color
       frame.getContentPane().setBackground(new Color(0x33, 0x33, 0x33));

        //Display the window.
       frame.setLocation(x ,y);
        frame.setVisible(true);

        //wait
       try {
            Thread.currentThread().sleep(time);
        } catch(InterruptedException ie) { }
        //close
       frame.dispose();
    }

    public void stopInstance() {
        --instances;
        this.stop();
    }
}

I already tested something with instance counting and trying to close the old one but that wont work.
Probably the the thread wont be passed by reference to "instance"?
It's the way I would do it in PHP Smiley

How could I get it work?

Hopefully you understand what I wanna say...
Offline Json

Junior Member


Exp: 7 years



« Reply #1 - Posted 2009-06-18 09:28:29 »

Maybe you can alter your code somewhat like this.

At the end of your action class.

1  
2  
3  
4  
5  
6  
7  
8  
if(display_window) {
    if(this.displayInfo != null && this.displayInfo.isAlive()) {
        this.displayInfo.getFrame().dispose();
    }

    this.displayInfo = new DisplayInfo("title", window_text, 250, 50, 1000);
    this.displayInfo.start();
}


Also make sure you add a new member to that class called displayInfo which is of type DisplayInfo.

You will also need to modify your DisplayInfo class and add a reference to the created JFrame.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
import javax.swing.*;
import java.awt.*;
public class DisplayInfo extends Thread {
    //title and text
   String title;
    String text;

    //size in pixels
   int width = 400;
    int height = 100;

    //time to display in ms
   int time = 3000;

    // The frame
   JFrame frame;

    //class infos
   static int instances = 0;
    static DisplayInfo instance;

    public DisplayInfo(String title, String text, int width, int height, int time) {
        if(instances>0) {
            instance.stopInstance();
        }
        ++instances;
        instance = this;
        this.title = title;
        this.text = text;
        this.width = width;
        this.height = height;
        this.time = time;
    }

    public void run() {
        //Create and set up the window.
       this.frame = new JFrame(title);
        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.frame.setUndecorated(true); //removes the borders

        //add text to the frame
       JLabel label = new JLabel(instances+" - "+text, JLabel.CENTER);
        label.setForeground(new Color(0xBB, 0xBB, 0xBB));
        this.frame.getContentPane().add(label);

        //set size
       this.frame.setSize(width, height);

        //get screen size
       Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();

        //Calculate the frame location
       int x = (screenSize.width - frame.getWidth()) / 2;
        int y = ((screenSize.height - frame.getHeight()) / 3) * 2;

        //color
       this.frame.getContentPane().setBackground(new Color(0x33, 0x33, 0x33));

        //Display the window.
       this.frame.setLocation(x ,y);
        this.frame.setVisible(true);

        //wait
       try {
            Thread.currentThread().sleep(time);
        } catch(InterruptedException ie) { }

        //close
       this.frame.dispose();
    }

    public void stopInstance() {
        --instances;
        this.stop();
    }

    public JFrame getFrame() {
        return this.frame;
    }
}


Now that will give you a solution for closing the actual frame and the thread should die automatically after your set time anyway. However you might want to consider implementing Runnable instead of extending thread and also adding a boolean with a while loop in the run method because thread.stop() has been deprecated.

// Json
Offline xRot

Senior Newbie





« Reply #2 - Posted 2009-06-18 19:31:56 »

I implemented like you postet it.

A small problem the
1  
2  
3  
4  
if(display_window) {
    if(this.displayInfo != null && this.displayInfo.isAlive()) {
        this.displayInfo.getFrame().dispose();
    }

wont be executed, as well there is another frame/instance...
I cant see the problem on my code!

I attached my complete code Wink
with ./b I compile it and with ./r I run it, its linux only (the Jinput plugin is only included with linux sources...) Wink

EDIT:/
my ass, why I cant upload a zip file?
You can find my code here -> http://www.rrelmy.ch/code.zip
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SimonH
« Reply #3 - Posted 2009-06-19 00:15:58 »

I dont wanna that 2 JFrames show up at the same time, when a new JFrame should be opened the older one should dispose before the new one shows up.
So just have one!

1  
2  
3  
if(display_window) {
            new DisplayInfo("title", window_text, 250, 50, 1000).start();
}

This is bad practice as you're creating a self-governing entity (ie, you have no control over it).
How about;

1  
2  
3  
4  
5  
6  
7  
8  
9  
DisplayInfo currentDisplayInfo=null;
...
if(display_window) {
            if (currentDisplayInfo!=null && currentDisplayInfo.isAlive()) {
                    currentDisplayInfo.dispose();
            }    
            currentDisplayInfo=new DisplayInfo("title", window_text, 250, 50, 1000);
            currentDisplayInfo.start();
}

People make games and games make people
Offline Json

Junior Member


Exp: 7 years



« Reply #4 - Posted 2009-06-19 12:40:16 »

Maybe you should try and take another approach and not use a separate thread. You could just initialise a frame and keep it referenced and whenever you get a joystick action you set the labels and whatnot on the frame and then you make it visible. Once the 1 second time limit is up you hide the window and start all over. If there is a new action while the window is already open, just reset the timer and change the information in the window.

// Json
Offline xRot

Senior Newbie





« Reply #5 - Posted 2009-06-20 13:04:24 »

Thanks you guys, it works Cheesy

I store the JFrame and the JLabel in a static variable and "hide -> update -> show" the JFrame whitout create a new one.

next step: semi transparent JFrame Cheesy
Offline Json

Junior Member


Exp: 7 years



« Reply #6 - Posted 2009-06-22 07:26:55 »

Brilliant Cheesy

// Json
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (11 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!