Last visited

Community Reputation

About Code Sage

Are you guys really that slow? If so I do feel bad for you. If that is the "best" GUI you can make without running the project than I guess your right. What do you win? Nothing, it was for fun... You just did that FOR FUN. People act like when they are coding a GUI they don't constantly keep checking to see if the positioning is correct for the text, buttons, sprites etc. The whole point it to create a "Good" looking GUI without debugging your code than simply post a picture. I don't know why that is so hard for you ignorant people to understand. Someone please just delete this thread, nothing but complete idiots on here.

Because a "challenge" should be easy ok... I don't think it is good practice to shoot a basketball with your eyes closed, but it would be a fun competition among friends to see who could make the most while doing it. And sorry that "for fun" isn't incentive enough for you. If you don't want to do than challenge than don't, but there is not reason to hate on it, otherwise your just as ignorant as you believe I am.
This reveals you are not a developer, because a developer knows no program or asset is ever really truly done. If you're not a developer, you're from sales so you're gonna get downvoted to oblivion.
The only time you may run the project is when you believe you will be satisfied with the results.* Sorry forgot I was addressing literal minded people.

Well when the State of the game changes it has to be done like so: (Assume we are going from the MainMenuState to the GameState by clicking the play button)
case PLAY_GAME:
State gameState = new GameState(game);
State.setState(gameState);
break;
When the GameState is being constructed it calls its supers constructor first, which looks like this:
public State(final Game game) {
if (currentState != null) {
currentState.setInitialized(false);
currentState = null;
}
game.getComponentHandler().getComponents().clear();
}
So before anything is really constructed inside of GameState the currentState is uninitialized and than set to null. This should effectively stop the mouseMoved() and mousePressed() from doing anything while GameState is adding/removing its components to the collection. At least I hope it is working like this.

If you have some free time and feel like doing something, I challenge you to code a GUI without ever debugging or running the project. The only time you may run the project is once you believe it is done, but once it is run you may not go back and edit the code anymore. Also you can't use any extra libraries or tools to create the GUI other than what is included in Java. Once you are done feel free to post a screenshot to show everyone what it looks like.
- Good Luck to those brave enough to give it a go ;)

I believe I have figured it out. Well at least for the past 100+ times I have opened my game it has not occurred.
Let me brake down the basics of what I was doing first. Inside my Game class is where I initialize all my handlers, so for instance my ComponentHandler. After my handlers are initialized I initialize the starting State of the game, in this case the MainMenuState. OK, so inside of the ComponentHandler constructor is where I add my MouseListener and MouseMotionListener to game, therefore the mouseMoved() method starts getting called before the State of the game is initialized. Well the problem doesn't exactly have to do with the order of my initializations, but it did play an important factor. This is what my mouseMoved() method looked like before the fix:
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int componentId = -1;
for (Component c : components) {
if (c == null || c.isDisabled())
continue;
if (c.contains(x, y)) {
componentId = c.getId();
if (!c.getHovered())
c.setHovered(true);
} else
c.setHovered(false);
}
if (componentId != -1) {
switch(componentId) {
// TODO: HANDLE THINGS
}
}
}
So as you can see that it will start to loop through all the components in order to find the componentId of whatever the x, y coordinates of the mouse are contained within.
Ok so now that the ComponentHandler is initialized, its time to initialized my MainMenuState. Well inside my MainMenuState is where the Interface is built for the main menu. This includes adding Components (Buttons) to the interface. Every Button() extends the Component class and upon creation is automatically added to the List of components inside ComponentHandler. So this is where the exception is thrown, while I am already iterating through the list of components inside ComponentHandler, I am also trying to add new buttons to that list through the creation of the main menu interface. Since you can't add or remove items to a list while iterating through it a ConcurrentModificationException was thrown.
How I decided to fix this problem is by stopping the mouseMoved() from iterating through the List without the State of the game first being initialized like so:
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int componentId = -1;
if (State.currentState == null || !State.currentState.isInitialized()) // Fix
return;
for (Component c : components) {
if (c == null || c.isDisabled())
continue;
if (c.contains(x, y)) {
componentId = c.getId();
if (!c.getHovered())
c.setHovered(true);
} else
c.setHovered(false);
}
if (componentId != -1) {
switch(componentId) {
// TODO: HANDLE THINGS
}
}
}
Inside the current States class I just be sure to set the initialized boolean to true at the end of the constructor. Also at the beginning of every States initialization I clear the list of components inside ComponentHandler. And just for the record I did the same thing with the mousePressed() to avoid any issues with that.
I am not 100% sure if this was the cause, however it seems to be fixed for now without the use of any CopyOnWriteArrayLists If you have any questions, comments or concerns let me know.

I already tried this method of doing the iteration and it sadly did not fix the exception. Only time it gets fixed for me is when I CopyOnWriteArrayList :/ And as for your other method I am using custom Buttons. They are not Swing or AWT.

I have been trying to recreate it for a while and I simplified it down as far as possible, but now the exception is never thrown... Honestly as I keep adding more and more trying to throw the exception I will have figured out the problem by the time it happens. I'll make a post if I figure it out, but I don't think i'll post any code unless you wanna take a look at my full source.

This is a terrible exception name. Not only can you modify something in two different threads, but just iterating over the collection and trying to change something can cause this. It may not have anything to do with multiple threads.
So is my fix sufficient? Or is the whole thing a mistake in the first place..
If it was me I would undo the fix and see if I could find the problem. Without seeing the code I can't tell you if this has fixed the problem or just hidden it.
I could send you my code if you wouldn't mind looking over it for me. Just promise I won't see it in the coding horrors section later haha.

This is a terrible exception name. Not only can you modify something in two different threads, but just iterating over the collection and trying to change something can cause this. It may not have anything to do with multiple threads.
So is my fix sufficient? Or is the whole thing a mistake in the first place..

Components get added and removed whenever there is state change. At initialization of every state I clear the list and than it gets filled with whatever components are necessary for that state of the game. For the time being I ended up making the list CopyOnWriteArrayList and it has been working perfectly fine. It may not be a legit fix but, if it works it works I guess haha. If someone has a better solution though I am more than happy to hear it.

I am attempting to create a ComponentHandler, which extends MouseAdapter, that essentially handles all the buttons in my game. Every time a new Component is created it is put in to an ArrayList inside ComponentHandler. This ArrayList is iterated through inside mouseMoved() and mousePressed() in order to identify the component and handle what happens to it when it is clicked/hovered over. Randomly when I start up my game I get a ConcurrentModificationException inside mouseMoved(). Sometimes it can be the first time starting it other times it could be the 50th. I can't for the life of me figure out the problem, so I am considering just using a CopyOnWriteArrayList and hoping that solves the issue. Do you think that would be the optimal fix for this? Or am I completely derping out and missing something totally obvious? I also have methods in ComponentHandler that getComponent, addComponent, removeComponent, getComponentList. Currently I am only using getComponent inside and outside of ComponentHandler. If you need to know anything just let me know and I will be glad to tell you. Thanks for your time and help.

The reason this seems like the right solution is because that's how Java handles mouse/keyboard input for a Swing or JavaFX GUI code. If you are using a JPanel for each player then this is an option. If you were coding in DirectX or OpenGL, there would only ever be one mouse. You don't have the concept of a mouse listener.
Whenever I see someone ask "What's the best way to [insert thing here]" alarm bells go off. How is your game structured? Are you using GUI components and callbacks, or are you doing a game loop with active rendering?
You are correct I guess I don't have a proper concept on what a mouse listener really easy. But I have come up with something that I want to try an do and I would love your opinion on it. I am currently not using GUI components and I am using a game loop and active rendering.
Basically I am going to have one class called ComponentHandler and it's job is to handle the input of all GUI Components.
package com.sage.rpg.gui;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import com.sage.rpg.Game;
public class ComponentHandler extends MouseAdapter {
public static List<Component> components;
public ComponentHandler(final Game game) {
game.addMouseListener(this);
game.addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int componentId = -1;
for (Component c : components) {
if (c.contains(x, y))
componentId = c.getId();
}
if (componentId != -1) {
switch(componentId) {
case 0:
// handle input
break;
}
}
}
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int componentId = -1;
for (Component c : components) {
if (c.contains(x, y))
componentId = c.getId();
}
if (componentId != -1) {
switch(componentId) {
case 0:
// handle input
break;
}
}
}
}

Are you talking about a multiplayer game ? About a server side implementation ?
You have always two ways of handling events, either pushing (send event to listener) or pulling (ask if a new event occurs or a certain state changed). The listener concept (or a BUS system) are pushing systems. If you have very large amount of potentially listeners, which don't need to listen to all events all the time, it could be more effeciently to use a pulling system (only the component which needs the data gets it).
A possible setup would be
// pushing system
interface InputListener {
void newEvent(...);
}
class InputManager
{
void addListener( InputListener);
void removeListener(InputListener);
}
// pulling system
class InputManager
{
MouseCoord getCurrentMousePosition();
}
For input handling both systems seems valid and using a listener system in java isn't unheart of. Therefor I can't see a reason to not using a listener based input handler, which would only use one class and one interface, thougth multiple listener instances. But you should not have a lot input listener at all, even in a multiplayer game (where you would have network listeners or similar interfaces).
I guess what I am asking is... If I were ever going to display more than one interface on the players screen at one time. I would have to add a new MouseListener to handle every interface being displayed.That is if I wanted to have the code for each interface in its own class. Or I could handle every button of every interface in one class and only add one MouseListener. So what would be the best way to keep the code for each interface separated and not have to deal with multiple MouseListeners. I hope that makes sense, it's sort of hard for me to explain.

I have been looking at a lot of peoples code lately. Seeing how they handle their player input and things of the sort. One thing that I keep coming across and dislike is when people use one or two classes to handle all the input for the game. Doesn't it seem better if the input for the player is handled in the Player class? And if you have several different interfaces, I wouldn't want to handle all of the mouse input in one single class for every other interface. It just makes more sense, to me anyway, if the input is handled in it's own separate "interface" class or something of the sort. However, something that starts to become an issue with this approach, is that for every interface that is displayed and for every player on the screen there has to be another KeyListener or MouseMotionListener added to the game. Does this become inefficient? Is this the reason people use one or two classes to handle all the keyboard and mouse input? I am just curious as to what would be the "best" way of doing it. I know that prettier isn't always better, but it is just something that has been bothering me for quite a while. Thanks for anyone who could shed some light on the subject.