• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 511
  • Last Modified:

Drawing up a screen using gridlayout.

Well, I decided to tinker around a bit, and what I'm trying to is draw up a board thats 10x10 of buttons.  What I want to end up doing is moving images around on the board but for the love of sake, I can not figure out this array.

Just looking for somebody who can guide me in the right direction.  If im doing something extreamly out of the ordinary, or it could be done a different way, let me know.  If it would help to talk directly about this in real time you can find me on aim >> aim marine.

Here is what I have to showy::  http://www.pastebin.com/279814


----------------------------------------
Another thing I was interested in doing was learning how to add sound to an intro splash screen.  But from what I have come across, it seems to be a HUGE process.  I just wanted to drop by to see if that was 'totally' true.   Is there a method that I should be looking at in the API?  All I wish to play is a *.wav. (background music, button clicks)


----------------------------------------
Currently I know about 'GridLayout'
    ---------------------------------
   |                                       |
   |                                       |
   |                                       |
   |                                       |
   |                                       |
   |                                       |
   |                                       |
   |                                       |
    --------------------------------

What if I would like to split that into two different panes?  Would it be done like a webpage, where I would call to a specific pane?

           pane1              pane2
  -----------------------------------
   |                        |               |
   |     Game           |    Score  |
   |                        |               |
   |                        |               |
   |                        |               |
   |                        |               |
 ------------------------------------

-----------------------------------------------------------------------

Thanks a lot in advance for any assistance given.  I appreciate it.  This stuff's coming on slowly but steady and I thank you all for being a quote/unquote mentors of a sort.

Regards,
Dave
0
ildave1
Asked:
ildave1
  • 5
  • 5
  • 4
2 Solutions
 
suprapto45Commented:
Hi,

Do you mean that you want to move all the 10X10 images around? One concept that you can know is that to do that, you need to re-arrange all your GUI again but with the random location :). So you should clear all your current GUI and rearrange the GUI again but each image with the random location. Do you get what I mean? My concept is not the best so please look over at all other experts' suggestions.

For sound, I do not think that it is hard.
http://www.javaalmanac.com/egs/javax.sound.sampled/Load.html

You can also put those codes in your button ActionListener, correct?

Please also read
http://www.developer.com/java/other/article.php/1565671

I hope that helps. If I find the new materials, I will let you know.

Regards
Dave
0
 
suprapto45Commented:
Hi Dave,

The reason you say
"But from what I have come across, it seems to be a HUGE process"

it might be the older version of jdk. With the newer version of jdk, it should be simpler :).

Regards
Dave
0
 
suprapto45Commented:
Hi,

To create a good GUI, I totally suggest you to learn GridBagLayout. It is powerful and richer than GridLayout.

Regards
Dave
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ildave1Author Commented:
Well, what im wanting to do is for example:

 ----------------------------
|  |  |  |  |  |  |  |  |  |  |
 ------------------------ ----
|  |  |  |  |  |  |  |  |  |  |
 ----------------------------
|  |  |  |  |  |  |  |  |  |  |
 ----------------------------
|  |  |  |  |  |  |  |  |  |  |
 ----------------------------
|  |  |  |  |  |  |  |  |  |  |
 ----------------------------
|  |  |  |  |  |  |  |  |  |  |                     Moving the X around the board, but the images that the X runs across, will change.
 ---------------------------                      Hopefully Ill be able to add something to 'chase' that X to make it a little more challenging  :)
|  |  |  |  |  |  |  |  |  |  |
 ----------------------------
|  |  |  |  |  |  |  |  |  |  |
 ---------------------------
|  |  |  |  |  |  |  |  |  |  |
 ---------------------------
|X|  |  |  |  |  |  |  |  |  |
0
 
objectsCommented:
for playing simple audio see this article:

http://www.javaworld.com/javaworld/javatips/jw-javatip24.html
0
 
suprapto45Commented:
Hi Dave,

What you can do is that to have a kind of timer (probably Thread) to always change the position of the X.

For example,
Now, your X is on (1,1) - (row, col). Then (1,2), (2,3) and etc is blank images. What you can do is that to have random number to specify the new random row and col for your X and refresh the GUI.

You can also set the certain amount of time for the application to generate the random number so that it is not running across very fast. One idea....you can add "level" to make your application more challenging too. And the higher the level is, the faster the X moves around :).

And wait for other experts' suggestion :).

Regards
Dave
0
 
objectsCommented:
Looking at you layout code it looks like it should be more like the following:


       public void window(){
                // Creating a Grid Layout
                JPanel window = new JPanel();
                window.setLayout(new GridLayout(10,10));
 
                // Attempting to place buttons... 0_0
                for(int j = 0; j < 10; ++j)
                {
                        for(int i = 0; i < 10; ++i)
                        {
                                square[i][j] = new Square ((50 * i), (50 * j), false, Images[1]);
                        }
                }
               
                square[0][9].setPic(Images[0]);
 
                for(j = 0; j < 10; ++j)
                {
                        for (i = 0; i < 10; ++j)
                        {
                                window.add(square[j][i].getButton());
                        }
                }
               
                // Creating a Windows User Interface
                Image icon = Toolkit.getDefaultToolkit().getImage("orange_E.gif");
               
                JFrame frame = new JFrame();
                frame.getContentPane().add(window);
                frame.setLocationRelativeTo(null);                  // Centering JFrame
                frame.setIconImage(icon);
                frame.setTitle("Chomper V.01 [Build 00042]");
                frame.setSize(450, 500);
                frame.setLocation(500, 250);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);  
        }
0
 
ildave1Author Commented:
-------------------
What you can do is that to have a kind of timer (probably Thread) to always change the position of the X.
>> I want the user to change X, only 1 square (like a rook in chess, any direction).
>> However, A timer will be usefull when I put Y into the screen (hopefully I can manage to make Y chase X by inself, not sure how hard that'll be)  I still dont know
>> a whole lot about timers yet.

You can also set the certain amount of time for the application to generate the random number so that it is not running across very fast. One idea....you can add "level" to make your application more challenging too. And the higher the level is, the faster the X moves around :).
>> GridBagLayout looks real interesting, I tested out their example on that page.  I may be a lot easier to use if I decide to implement 'levels' so to speak within the >> gameboard.
--------------------

As for:
        for(j = 0; j < 10; ++j)
        {
                for (i = 0; i < 10; ++j)
                {
                        window.add(square[j][i].getButton());
                }
        }

For window, I keep getting an out of bounds exeption. `,=0|
>> Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
>>       at GUI.window(GUI.java:51)
>>       at Main.main(Main.java:18)
0
 
objectsCommented:
sorry should be:

        for(j = 0; j < 10; j++)
        {
                for (i = 0; i < 10; i++)
                {
                        window.add(square[j][i].getButton());
                }
        }


Your existing code would have been getting the same error by the looks.
0
 
ildave1Author Commented:
Hmm...
      public ImageIcon[] Images(){
             // Storing Images into an Array (Lots to come....)
             Images[0] = new ImageIcon("Smile_N.GIF");
             Images[1] = new ImageIcon("Smile_E.GIF");
             Images[2] = new ImageIcon("Smile_S.GIF");
             Images[3] = new ImageIcon("Smile_W.GIF");
             Images[4] = new ImageIcon("Berry.GIF");
             Images[5] = new ImageIcon("Goblin.GIF");
             
             return Images;
      }

Would something such as:::
        square[0][9].setPic(Images[0]);      work to set an image on that block?

+----------------------------------------+  square class,
    public void setPic(ImageIcon a)
    {
        button.setIcon(a);
    }
+----------------------------------------+


Thanks for all the help all! Greatly appreciated.  I'm sure there'll be more threads by me :)

Regards,
Dave
0
 
ildave1Author Commented:
btw, I didnt realize putting ++j  or j++ would make it not compile, i thought it was the same thing?
0
 
objectsCommented:
> square[0][9].setPic(Images[0]);      work to set an image on that block?

yes

>  I didnt realize putting ++j  or j++ would make it not compile

you were also incrementing the wrong index in the inner loop

for (i = 0; i < 10; ++j)

0
 
ildave1Author Commented:
hmm, i guess its not actually reading the file right then

thanks!

regards,
dave
0
 
suprapto45Commented:
Hi,

Glad I could help. Sorry, I just get back from my lunch and meeting.

Regards
Dave
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now