Basically, I am building my own pokemon game, however whenever I go into a new "region" the tiles should reset and refresh, however, instead of clearing the old ones out, they just add onto the existing ones, thus causing a lot of issues.
I know that once I can successfully clear the old tiles, the game will refresh nice and neat for me.
I thought all i had to do was
buttonPanels = new JButton[row][col]
to make a new (clean) instance of the button array?
The whole board is a just a JButton array.
Two images below, one of before and one of after I go into a new region. You can see how how in the after photo all the new tiles just add onto the existing ones, which causes a lot of problems.
http://i421.photobucket.com/albums/pp296/rskom/before.png
http://i421.photobucket.com/albums/pp296/rskom/after.png
THANK YOU!! :)
First time at trying a rpg type thing, so don't be too critical of it so far.
Without seeing more of your code it is hard to answer your problem, but I think I know what the problem is. You show code where you do create a new array of JButton references. However, this does not destroy or remove the buttons you had in the previous array. If the buttons were visible somewhere (usually the case for having buttons) then they should first be removed from that container. This is probably the sequence you want if won want to replace all buttons.
For each button that is referenced from your array, remove it from the container where it is visible. Then you can replace your array, fill it with new buttons and finally add the buttons to the container where they should be visible again.
But then the real question is if you really do need to create a new array with new buttons. Can you not just reuse your existing one?
Related
I've read through a dozen or so actionlistener/loop related questions here, but I'm not sure I've found my answer. I started on my first large Java project, a text RPG that's spiraled into around 5K lines of logic and game features which was functioning as intended using just the console - when I decided I'd try to build a Java swing GUI for it instead. Here's my problem:
I use a Room object which handles the description of where the player is at and also has an array of options for the player to choose next which it creates dynamically based on what cell the room's id is in on a csv file and what is beside it. I stopped outputting this to the console and instead started creating JButtons based on the options array like so:
public void showNarrate(){
add(dd,gridConstraints);
optionCopy.clear();
int i = 0;
for(JButton j : optionButtons){
//adding and formatting buttons to gridBagConstraint I also set actionCommand for each button to the triggerValue (ID of the next room which the button should take the player to)
}
//I tried using a copy of my JButton array here so I could have something to iterate over in actionListener after clearing out the original
//(Since it needs to be cleared so the next Room's buttons can be built after the player chooses an option)
for(JButton j : optionButtons){
optionCopy.add(j);
}
optionButtons.clear();
//dd is a seperate drawingComponent I used for outputting room descriptions which may be totally unnecessary at this point :/
dd.repaint();
setVisible(true);
}
Over in actionlistener (Same class) this is how I tried to swing it:
for(JButton j : optionCopy){
if(e.getActionCommand().equals(j.getActionCommand())){
Main.saveRoom = Main.currentRoom;
Main.currentRoom = j.getActionCommand();
System.out.println(Main.currentRoom);
}
}}
Then in my main class I call:
narrator.narrate(currentRoom, saveRoom); which takes care of some other logic concerning locked doors, encounters, etc.
Also in Main loop are some other methods related to autosave and tracking which rooms the player has visited. I know from other q/a i'v read on here that this is all pretty bad design, and I'm sttarting to understand that now, but my issue is this:
The first room of the game loads up fine, when I click a button it outputs to console(Just for testing) the correct trigger value of the room the button should be calling, so I'm getting that far, but how can I call the same method over again now?
-If I call narrate from actionListener it will wind up calling itself again and complain about ConcurrentModification.
-If I try to keep a loop going in my Main class it will keep looping and won't allow for the player to actually choose a button.
I've never used threads before, which I wonder might be the answer,and the closest thing to a related answer I've found is this:
Java: Method wait for ActionListener in another class
but I don't think moving actionListener to Main class would resolve my problem which is actionListener winding up calling itself recursively. And as for the observer-observable pattern... I just can't understand it :(
I appreciate any and all help, I've learned a LOT trying to make this thing work without seeking help as much as possible but this has stumped me.
Your loop in actionPerformed only checks whether a JButton exists in your optionList with the given actionCommand. However this can be done before actually doing something:
boolean contained = false;
for (JButton j : optionButtons)
if (j.getActionCommand().equals(e.getActionCommand()))
contained = true;
if (contained) {
// change room
}
now you can call narrate because you have finished iterating over the collection beforehand and will not get a ConcurrentModificationException
I'm newish to programming so there might be something obvious I don't know of that's a solution to this.
I'm currently writing a program that tests the user on words that they've entered before. When the user is entering words, the screen shows a jtextfield and jtextarea among other things for adding a word and it's definition and a variable is set to the constant NOT_TESTING to remember that that's the screen being displayed. My question is, when I want to switch screens to show the testing screen which has a jlabel displaying the word and a different jtextfield to write the definition of a word, what's the best way to not show the jtextfield and jtextarea from the screen where the user submits a word? (I'm not sure screen is the proper term, but I'm using it anyway.) If I don't make the components instnace variables, then I can't remove them when making a different screen, but it seems ridiculous to make them instance variables and then remove them whenever I switch screens, create new objects when going back to the old screen, and have so many instance variables.
Sorry for the long explanation, basically my question is: what's the best way to not show objects from previous screens when drawing a new screen?
Take a look at CardLayout, which allows multiple JPanels to be shown without the other one showing (basically, it makes the entire window that panel and you can switch between them).
I have been working on a memory game i asked a question about yesterday, I got some brilliant suggestions but none of them seem to be helping my problem i can get the buttons to shuffle around the grid layout fine, but i cannot figure out how to keep the set values i tried using "setActionCommand" but i don't think this is the way i can do it for matching the values of two buttons within my memory game.
gameBtn[0].setActionCommand("1");
gameBtn[1].setActionCommand("2");
gameBtn[2].setActionCommand("3");
gameBtn[3].setActionCommand("4");
gameBtn[4].setActionCommand("5");
gameBtn[5].setActionCommand("6");
gameBtn[6].setActionCommand("7");
gameBtn[7].setActionCommand("8");
gameBtn[8].setActionCommand("1");
gameBtn[9].setActionCommand("2");
gameBtn[10].setActionCommand("3");
gameBtn[11].setActionCommand("4");
gameBtn[12].setActionCommand("5");
gameBtn[13].setActionCommand("6");
gameBtn[14].setActionCommand("7");
gameBtn[15].setActionCommand("8");
above is how i used the setActionCommand but i cant find out how you would compare them to each other to find a match for example matching club1 to diamond1 and club2 to diamond2. would this not mean i would have to do something like this for every single match i wanted?.
if (gameBtn[0].getActionCommand().equals(gameBtn[8].getActionCommand())){
sameValues();
}
if (gameBtn[1].getActionCommand().equals(gameBtn[9].getActionCommand())){
sameValues();
}
although the above might seem to work as it does not throw any errors im left thinking there must be a more efficient better way to do it i accomplished this at first before i added the shuffle to move the buttons on the grid i was using another list of values, but know its randomized the card on the button no longer actually matches the buttons set value.
have a look at the link at the bottom of the question if you need more information from my last question, Any help would be much appreciated as this has been bugging me for a while know thank you.
link: Java concentrate is there a way to shuffle jbuttons
First Java project - solving a Sudoku puzzle. Used SDK to create GUI first (bad idea?). GUI has 9x9 grid of square JButtons. Added handlers (81!) such that 'click' cycles values '_','1',...'9'. Named buttons in grid to reflect position (also painful). To solve puzzle implemented 9x9 array of Tile objects. Tiles to mirror state of respective JButtons (text value, etc). Want to use array of Tiles to solve puzzle. To use get/setText methods to r/w from array to/from JButtons. Hit wall!! Can't find way to reference JButtons. Hoped to create String representing JButton variable name (easy enough) then magically convert (type casting?) to reference JButton object. Not possible? A String is a String is a...? I have an object in memory (JButton) I can't reference dynamically? Approaches seen (tic-tac-toe, etc.) create an array of JButtons to access by index. Don't want to do this as it takes away from simplicity of using swing. The SDK generated source code is ~2000 lines already! Use hash to map objects to objects? Ideally each Tile object will map to respective JButton when created. Hopefully enough detail here to explain what I am 'trying' to do. Is this case where pointers would be nice? Is this a downfall of using SDK for GUI? Shortcoming in Java? Can anyone recommend approach to this (tricks or tips)?? Thanks!!
Array of Objects extending JButton is way to go. Learned much here and many thanks to all who contributed!! For newbie to Java to think an extensive swing UI is an easy approach is foolish. Took me a few weeks to learn basics of JComponents. I still use 'Window Builder' but only for the most basic interface. Want to challenge a new student? Let him/her try to tackle Sudoku as first project! Now that I have conquered this UI I feel on top of the world and no problem is beyond my reach! THANKS AGAIN!!
I'm trying to create this game
in Java for my school A-level course. I'm currently trying to use an individual button for each of the squares and cannot in any way remove the buttons's background. I've tried setBackground(false) which hides the background, but it is still there so still clickable, which you can imagine completely screws up the board.
Ultimately what I'm asking is how to make a button from a picture the same size as the picture, without any extra backgrounds at all, not just hidden.
Here is a picture of what I mean:
Any help/suggestions would really be appreciated, I'm desperate!
There was a question on here for creating custom buttons with java:
Creating a custom button in Java
The answer to this question might solve your problem.