Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Fort Forwarding on routers required by Socket connections? on: 2009-07-14 21:31:21
So I just wanted to know if there was a way to "ignore" routers on home networks, or commercial ones at that.

If I'm using a port that has no specification (43144) and trying to communicate with someone behind a router, I always have to ask them to port forward on their routers so that the computer can get ahold of it.

Is there a way to force Java connections to not require port forwarding on their LAN's network? (ie: The type of connection being made perhaps? Currently using the Socket and ServerSocket class)

My code works, in that I've tested two computers on the same LAN, the SAME computer as well as a computer on a different network in another city. Everything worked great, but only after Port Forwarding was physically done by the user on their home router.)
2  Game Development / Newbie & Debugging Questions / Re: Embedded JPanel as it's own thread? on: 2009-07-09 02:32:14
Well, this is the Clueless Newbie forum, and I don't make any claims to being an expert. I'll provide you the (abeit trimmed) code of the code in question class, and any other code that I feel is important to it in my JFrame class.

So I'll try to explain everything here. So the way this works is you have Dorothy, and you have the Wizard. The Wizard controls the game that Dorothy is playing. (Currently the Game "Simon" 4 coloured squares, and a sequence to remember)

THe communication that I have designed works perfectly and the Zone Attributes, update exactly as planned, without any issues.

The ONLY problem I'm having is whenever a "newGame()" gets called. At the start of each new game, a sequence is supposed to be shown to the user, by drawing a brighter coloured zone (triangle, square, circle, whatever it may be at that time) for the time specified of "Thread.sleep(300)". This should be doing it for each letter(see "Zone") in the sequence. However, when I run the program, when I click the start button, the game hangs for a moment...then waits for the users input. (Instead of highlighting each zone)

Now that you see the code, do you see any glaring problems?

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  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
public class Dorothy extends JFrame{

    public  ConcurrentLinkedQueue changes = new  ConcurrentLinkedQueue();
     /*
    *Variable definitions
    */

    private Zones gameArea;

    public Dorothy() {
    //Code that builds the frame
   buildUI();
    setVisible(true);

    //code for setting up socket communication (this works just fine)

    }


    private void buildUI() {
        //other components(a couple of labels, and a button or two.) PERHAPS THESE INTERFERE SOMEHOW?

        gameArea = new Zones(this);
        add(gameArea);
        gameArea.setBounds(20,40,360,360);
        gameArea.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        gameArea.setBackground(Color.BLACK);
        gameArea.setOpaque(true);
        gameArea.setVisible(true);      
    }

    private void butStartActionPerformed(ActionEvent evt) {                                        
        if(butStart.getText().equals("Retry?")){
            //if player loses
           gameArea.newGame();

        }else{
            //first time user plays.
           gameArea.newGame();
        }
}


    public void addEvent(String s){
       //this is for the communication via sockets
      //this works as intended
      changes.add(s);
       gameArea.repaint();
    }
    public String updateBoard(){
//adds new "Zone attribute change via socket communication with another user
//This works perfectly.
       if (changes.isEmpty()){return null;}
        return changes.remove().toString();

    }

    public static void main(String args[]) {

        new Dorothy().setVisible(true);
    }

}
class Zones extends JPanel {
 
   //a whole bunch of variables.

    public Zones(Dorothy mainGame) {

        myParent = mainGame;
        setLayout(null);
       //setup mouse listeners
      //setup "each zone"

    }
    public void newGame(){
        //Prepare a new game
       gameOver = false;
        killMouse = false;
        gridSize = 4;
        seqSize = 3;
        sequence = "";
        attempt = "";
        clicks = 0;
        highlight = 0;
        newSequence = true;
        repaint();
    }

     private void zonePressed(MouseEvent evt) {
       
        if(killMouse){return;}
        clicks++;
        //zone a
       if(evt.getX()< 180 && evt.getY() < 180)
        {
            highlight = 1;
            attempt += "a";
        }
        //zone b
       if(evt.getX()>180 && evt.getY()<180)
        {
            highlight = 2;
            attempt += "b";
        }
        //zone c
       if(evt.getX()<180 && evt.getY()>180)
        {
            highlight = 3;
            attempt += "c";
        }
        //zone d
       if(evt.getX()>180 && evt.getY()>180)
        {
            highlight = 4;
            attempt += "d";
        }
        myParent.sendMsg(highlight,'h');
        repaint();

        String codeSeg = sequence.substring(0,clicks);

        if(!codeSeg.equals(attempt))
        {
            gameOver = true;//if the user was wrong
           clicks =0;
            highlight = 0;
            attempt = "";
            repaint();
        }
        else
        {
           if(clicks == sequence.length()){
               //if the sequence was remembered correctly
              attempt = "";
               clicks = 0;
               newSequence = true;
           }
        }
     }

     private void zoneRelease(MouseEvent evt) {
        if(killMouse){return;}
        if(!gameOver){
           
            if(newSequence){
                System.out.println("Time for a New Sequence!");
                repaint();
            }
            else{
                System.out.println("Still Guessing!");
                repaint();
            }
        }

     }
//THIS METHOD IS USED TO RESET THE COLOURED ZONE AREAS DURING A repaint() CALL. (ALSO TO UPDATE
//ZONE ATTRIBUTES WHEN THE CONNECTED USER MAKES CHANGES.
    private synchronized void reset(Graphics g){
//everything that happens here is to handle when zone attribute needs to be changed. (colour, shape, etc)
           for (int i = 0; i<4 ;i++){
                g.setColor(zoneGroup[i].getColor());
                switch(zoneGroup[i].getShape()){
                    case 4:g.fill3DRect(178*(i%2)+2, 178*(i/2)+2, zoneGroup[i].getDimension(), zoneGroup[i].getDimension(), true);break;
                    case 5:g.fillPolygon(new int[]{178*(i%2)+2,178*(i%2)+92,178*(i%2)+180},new int[]{178*(i/2)+182,178*(i/2)+2,178*(i/2)+182}, 3); break;
                    case 6:break;
                    case 7:break;
                }
            }
       
     }

    @Override
    public  void paint(Graphics g) {
        super.paint(g);
        reset(g);

        if(killMouse){return;}

        if (gameOver){
            killMouse = true;
            myParent.resetGame();

        }else if (highlight > 0){
            highlight--;
                g.setColor(zoneGroup[highlight].getColor().brighter());
                g.fill3DRect(178*(highlight%2)+2, 178*(highlight/2)+2,
                        zoneGroup[highlight].getDimension(), zoneGroup[highlight].getDimension(), true);
            highlight = 0;
        }else if(newSequence){
            setEnabled(false);
//-------------------------------------------------------------------------------------------------
//THIS IS WHERE THE SEQUENCE IS SUPPOSED TO LIGHT UP BUT ISN'T WORKING
//setEnabled was used to disable key clicks by the user, and has no effect to the problem in question
//as the problem existed before it was put in.
//--------------------------------------------------------------------------------------------------
           makeSequence();

            //iterate through the code
           for(int i = 0; i < sequence.length(); i++)
            {

                //light up the next square

                char letter = sequence.toCharArray()[i];
                if(letter=='a')
                {
                    g.setColor(zoneGroup[0].getColor().brighter());
                    g.fill3DRect(178*(0%2)+2, 178*(0/2)+2, zoneGroup[0].getDimension(), zoneGroup[0].getDimension(), true);
                }
                if(letter=='b')
                {
                    g.setColor(zoneGroup[1].getColor().brighter());
                    g.fill3DRect(178*(1%2)+2, 178*(1/2)+2, zoneGroup[1].getDimension(), zoneGroup[1].getDimension(), true);
                }
                if(letter=='c')
                {
                    g.setColor(zoneGroup[2].getColor().brighter());
                    g.fill3DRect(178*(2%2)+2, 178*(2/2)+2, zoneGroup[2].getDimension(), zoneGroup[2].getDimension(), true);
                }
                if(letter=='d')
                {
                    g.setColor(zoneGroup[3].getColor().brighter());
                    g.fill3DRect(178*(3%2)+2, 178*(3/2)+2, zoneGroup[3].getDimension(), zoneGroup[3].getDimension(), true);
                }
                try
                {
                Thread.sleep(300);
                }
                catch(Exception e){}

                reset(g);
            }
            clicks = 0;
           
            newSequence = false;
            setEnabled(true);
        }
       
    }
    private void makeSequence() {
      //creates the letterized sequence used to display the colours to the user.
     //"abcdcba"
}

}
class zone{
   //custom class that handles each zone and it's currently active attributes

}
3  Game Development / Newbie & Debugging Questions / Re: Embedded JPanel as it's own thread? on: 2009-07-08 17:48:47
The only reason I bring up the idea of threads is because when I initially designed the custom JPanel class, I initially ran it as a JFrame and it was the sole thing on the screen. And it worked exactly as I wanted. But as soon as I made it a JPanel and put it inside a JFrame, that's when everything went to hell.

Now, my first reaction was that JPanels and JFrames are different, and that could be why the difference when running a loop that is supposed to "pause the current thread", that being the one running all the UI. But I could not for the life of me figure it out, and just tried moving onto making a thread that runs seperately from the rest of the ui.

I'm also not 100% sure how I would cast a "repaint()" into it's own thread...seeing as there's only 2 repaint calls that I would want to run seperately from the rest of the UI, this seems like a good idea....

But how to I do it?

new Thread(this.repaint()).run();


ok...so that didn't solve anything...

Can anyone explain why this code works when it's run as the "Top" class, but not when it's run as an embedded component Class?
(works as Frame, but not as a JPanel WITHIN a 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  
//partially pseudo-code

@override
public paint(Graphics g)

     for (//length of sequence))
    {
               Char whichZone = charAt(sequence);

            if(letter == 'a')
                {
                    g.setColor(zoneA.getColor().brighter()); //from custom zone class
                   g.fill3DRect(0,0,180,180,true);
                }

            //3 other similar methods for 3 other zones

                try
                {
                Thread.sleep(300);
                }
                catch(Exception e){}
               
                reset(g); //does the same as above, just reverts back to g.setColor(zoneA.getColor());


                }


4  Game Development / Newbie & Debugging Questions / Embedded JPanel as it's own thread? on: 2009-07-08 15:47:25
So, I've been trying to figure out a way of making my custom JPanel class run as it's own thread, because I believe that it'll solve my problem of "showing the user a sequence of colours" (it's a version of the game Simon) without hanging the rest of the program. (or more correctly, hanging everything until it comes up with the new sequence, instead of actually showing it.)

I just can't figure out how. Should I make the class extend Thread? Should I declare it as a thread in my "outer" class? (pseudo code below)


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  
public class outer extends JFrame

private inner customClass;

constructor{

initialize inner;
add(inner)
}

public main
{
new outer.setVisible(true)
}


class CustomClass extend JPanel

constructor
{
build JPanel
}

@override
private paint(g)
{

The entire game runs in this method.
//disable mouseEvents
in a loop
{
     highlight a sequence
     g.drawSquare(brighter color than already there)
     pause thread
     repeat for the length of the sequence
}
//reenable mouse events

}
5  Game Development / Newbie & Debugging Questions / Re: Need help with objects that "paint" on: 2009-06-19 09:20:14
Edit: Reposted proper code so it's more clear. See Above.
6  Game Development / Newbie & Debugging Questions / Need help with objects that "paint" on: 2009-06-19 00:40:06
EDIT: I've re-posted the proper code, and made my problem more clear. (I hope) At least now people will be looking at the right thing and not guessing at what they think my true problem is.

Zones extends a JPanel, which is added to a JFrame called Dorothy. When done this way, Zones doesn't work as planned. When I declare it as it's own class (public class Zones extends JFrame) and override it's paint function, it works the way I want. Details below.

What it should be doing:

1. Show user the sequence
2. Allow user to click squares
3. if right, Go to 1.
4. if wrong, show reset button that Goes to 1 on click.

What it does when inside another container:

1. Freeze as the splash screen is hidden, and it is set to visible.
2. unfreezes, allowing the player to interact (no sequence shown)
3. Allow user to click squares
4. if right, Freeze (no sequence shown), go to 2.
5. if wrong, show reset button. When clicked, freezes. Go to 2.

The user needs to see what Simon says to play.  Undecided

Apologies for not providing the code in the post, I was forced to provide a file because I reached the "10000" character limit, despite what Office and 2 online character counters said. (Said I had 9655 Tongue)
7  Game Development / Newbie & Debugging Questions / New to Java game programming. Need help with threads? on: 2009-06-08 16:34:44
Hey all,

Just a quick question really, I prefer to attempt to solve the answer on my own, though a shove in the right direction is always appreciated.

I'm currently writing a "Simon Says" type game, but I'm throwing in a Wizard of Oz interface for another user to use to 'mess up' the player.

My big problem is that I'll need almost constant communication between the two programs (two computer over a LAN, not the same box.)

My question really boils down to: Should I be using 1 thread to pick up the incoming information, 1 thread for outgoing information and 1 thread for the actual playing of the game?

I've read most of Sun's tutorial, but I'm still not 100% clear on what situations require, or should be use with, Threads.
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
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!