Two Questions

+1 Developer John · October 7, 2014
1. I can't really come up with a way in order to do this in Java, but how can I create a window that has about 16 tiles on it (4x4) and allows me to move them around?
2. How could I set one of these tiles to a custom color?

Post a Reply


Oldest  Newest  Rating
0 Alex Sweps · October 7, 2014
You would probably need to use an Multi Dimensional array. Bucky has a tutorial on creating a multi array table which is pretty much the same thing your looking for. One array for columns and the other for the rows.

As for moving them around and adding colors im not sure. Create your table first and play around with it and try figure what youd need to do adjust only certain squares and then work on adding a single color to a single square and go from there.
0 Developer John · October 7, 2014
I don't understand though. How would I get the tiles on the window?
0 Alex Sweps · October 8, 2014
The truth is I don't know how you would do it. Every new program I write I run into things I dont know how to do but the trick is to break it down and just do one thing at a time. Thats pretty much the methodology you need to use on anything you ever write. Asking for your entire program code to be posted would mean id be doing all the thinking and writing the program for you...

Before you start and while you are writing it, imagine yourself opening the finished program and write down the order you do things. First thing would be opening it and seeing a window. Write that and only then focus on creating the tiles. Start with one tile. You want them to clicked on to move so learn about action listeners and add one to the tile. Play around and use the tutorials on this website. 

Start with this:

Hope this helps and if you get stuck on something post the code you have done so far and we can help. 
0 Developer John · October 8, 2014
Actually, I should have asked for an example of an import I should use. I didn't really say give me the code and I'll just study it. My fault. Anyways, I don't use tutorial #50, I focus more on #51 since it is lots easier to make and I can remember it. I have already started creating GUIs, in fact, I've created a GUI that allows me to do several pointless operations. I'm just thinking too far ahead on what I want to code as practice for my future project. Thanks for advice though on breaking programs down to the basics. I believe if I might a slight mistake though, the whole program will be in trouble. By the way. After watching the tutorials on GUIs, I don't understand what these are:
-Action Listeners
-Action Event
-Item Listeners
-Item Event
-Event Handlers
+1 Alex Sweps · October 8, 2014
Action listeners are what you apply to a button so it can listen and know when the mouse is over the button. When you hover your mouse over something and it displays a hint, that is because it has implemented an action listener on it.

Action Event is what happens when you actually click the button. If you wanted the button to play a sound when you click it you put your code in the action event method.
public void actionPerformed(ActionEvent e) { 
    ...//code that reacts to the action... 

Item listeners and Item events are the exact same thing but for basic on/off components. Things like radio buttons and check boxes. 

Event handlers are the catagory they all go into. So an action listener is a type of event handler because it handles events that happen such as listening for your mouse. 
Any time you want your program to do something when a user clicks or selects something in your program you will want to use these event handlers to make it happen.
0 Developer John · October 8, 2014
What do you mean an Action Listener gives you a hint? Also, what if I wanted to make a GUI pop up when I click a button, would that be inside the Action Event method, or could I use a variable for it?
+1 Alex Sweps · October 9, 2014

That "New tab"  hint box that pops up when you hover the mouse over the new tab button is because of an action listener. 

If you wanted to have a GUI open from a button click my recommendation would be to create a method that opens the new window and then call if from within in the action handler. Something like this:

public void actionPerformed(ActionEvent e) {

private void openGui(){
         code to open gui...
0 Developer John · October 10, 2014
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator