Eduardo Rod

+ Follow
since Sep 06, 2020
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Eduardo Rod

I never realized I was painting a background at the drawPanel and that it was blocking the BackgroundPanel. This code now works,I can add my drawPanel to the BackgroundPanel as follow:

6 months ago
ok Thank you I am checking it. I was trying to use a a JLayeredPane without success.

and on setImage() I got

but it does not work, the panel does not show. I will test the BackgroundPanel and post anything if works.

edit: The BackgroundPanel does a very nice job however my gif is still on top, each time I press a key the content of the drawPanel shows up but it's like a small flash, what I only see is the gif moving but the other stuff is not on top. This is my modified code which still does not work, any help?

6 months ago
Hello, I am trying to get an animated gif as a background of my JFrame, what I do is:

My frame has a drawPanel where I paint some things, the JPanel has it's own class where I call paintComponent() to draw everything, the constructor of my frame is something like this

The result is the image in front of everything, what I drew on panel is under image and I expect it to be over the image since the gif is a background but it's not behaving like it. Do you know what i am doing wrong? Besides the image is not 400x600, it's a bit smaller so it does not cover full frame, I'd need to scale but that's another thing what really matters is to get it as background and not over my panel.

6 months ago

Piet Souris wrote:Your push method requires an Entry as argument, but you have a double.

And, when you pop, you do not adjust the size variable.

Yes, the method as you defined receives an object of type Entry, and then you are pushing a float instead of an object would this works?:
6 months ago
Hello I am currently working with implementation of Minimax. I got a very small pseudocode and with that I am supposed to implement full game, I have no hints and no additional information. I cannot use any internet examples, all the code must be done on my own. However I found two good references to this topic:

Algorithms Explained – minimax and alpha-beta pruning
Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI – Finding optimal move)

First the pseudocode, do you think this is enough to code minimax function? Does this pseudocode provides enough information? I think it does not however the guy wants us to do it all with only this.

Function minimax(player, state):
    if state is terminal:
         return evaluation of state
    for each subState
         if player == MAX:
              return Maximum of minimax(MIN, subState)
         if not:
              return Minimum of minimax(MAX, subState)

Here the subState is when you expand all possible moves for current state, then you call for each one the function minimax recursively. The evaluation of state gives 1 if computer wins, -1 if human player wins and 0 if it's a tie. Based on this I got the following code, the problem is that CPU player isn't really that smart picking up the best move. I tried to do the pseudocode of the youtube video which I think is very clear. My app goes like this: A JFrame with a few panels, 9 buttons to pick a movement and an ActionListener that is checking for user input. Each time the player clicks a button to mark his/her position I call a method for the CPU movement, here the CPU is the maximizer so every time I call minimax I set isMaximizer to true.

Tic Tac Toe is a string array of size 9, I order elements ascending row by row. Then when I check if the state is terminal I do the following:

Finally when the user does the move it's computer's turn so I call:

I cannot see what I am doing wrong, I also ran the GeeksForGeeks code and I'm kinda understanding the algorithm but still can't see the problem. I hope you can help me out, greetings.
6 months ago
The topic about EDT is interesting but I just noticed the problem in my code is not related to SwingWorker anymore. I do have errors in my algorithm that search and sort, I saw that I was using continue instead of break in my for loop,and I am finding more errors in my code which are definitely not related to what we were discussing. I'm still working on it and I think the solution has nothing to do with threads anymore so I'd say that the code I posted before for the actionPerformed is the solution to the updating GUI problem.

edit: The sorting goes like this: a for each loop will read the linked list contained into the arraylist then a nested for will get each button from the list. it will compare the loaded button with the ones on the array of buttons and change things if needed. For some reason the program sometimes reads the linked list of buttons twice, magically it jumps all the loops and reads the next button of the list. In other words it reads a button from the linked list and then reads the next button skiping the rest of the code. Why do you guys think it happens? Maybe at that point the app thread and the background thread read the same line? The program fails mostly on the last elements, so from the 50 buttons it works fine for 30 or more buttons and then suddenly the error appears. My updated method for sorting is:
8 months ago

Campbell Ritchie wrote:OP has used Void not void; there is a difference. I don't know how far you can get because it is an uninstantiable class. I presume there is some tutorial recommending SwingWorker<Void, Void>. OP: please provide more details; I have forgotten the little I ever knew about SwingWorker.

It was on another site that someone was asking something and a guy proposed some code which I only took the SwingWorker part. On Java Multithreading: Swing and SwingWorker (Video Tutorial Part 15) the code example says that GUI should be updated on process() method or done() method, and on Intro to SwingWorker in Java it says that when creating a SwingWorker<T, V> we define T - the result type returned by doInBackground() and V the type used in publish() and process() methods.

So with this information I noticed I am processing data and updating GUI on doInBackground(), so I should rewrite things in order to call process() to update buttons. Respect to the SwingWorker arguments, T would still be Void because I am not implementing done() right?, and V should be the button so that publish() sends it to process() so that it can update the GUI there, in this case the button color and number. This is the method I am working on, I just don't know how to split the search and the update into different methods.

The only thing I can think of is moving the SwingWorker code to my main class and the for loops would be inside doInBackground, instead of updating there I would send the three buttons that make the changes to process().

8 months ago

Samuel Marchant wrote:On the button object and the object with the background  to change after all the changes have been called.  use
e.g.   myControlButton1.repaint();

Repaint is a method of java.awt.Component it updates the screen.

Like I said that is not the solution to the problem Samuel. Respect to the actionListener I'm not sure how to do it properly. The issue is that I normally do things on the Netbeans design window and it's easy to add components and set the methods on the events section. However I could not find out how to create an array of buttons this way (design window) so I had to create them manually by coding it then I have some automatic code added by netbeans and what i wrote manually in the same class. Only practice will help me to do it better.

Anyway I followed the Event Dispatch Thread solution and for now it works, the button that starts animation has an actionPerformed and inside of it I have added the SwingWorker. Now it does what I want, changes are done as soon as code is executed. Could you tell me if this is right? I mean this works but I am not sure it is done the right way. do I need to add done() method? what should I put inside of it?

And one last question, by adding a SwingWorker do I have to worry about memory access or something else?. I just realized that when switching buttons (recoloring and renumbering) there are times where it reads the value of a button but it is not what I see on the window. Let's say that the array is filled randomly and that the fifth button is red and has the number 3. When the method desfragmentarUnidad() is running and reads the properties of the fifth button it shows up diferent data like blue color and number 5. Right before the method starts I am printing values for everyone so I can check what values are set for the buttons and I am just getting wrong data during method execution, from the 50 buttons maybe a third of them are wrong and when the method sort them the result is wrong.

Thank you so much for your quick answers, greetings.
8 months ago
Hello, I have been looking for information and I only found topics which say I have to call revalidate() and repaint() methods in order to update the content of the GUI.

I'm working on an app that has a frame with a Jpanel and gridlayout, inside of it there are lots of buttons. Each button represent a cell that has a color and the text will have a number. I am randomly painting and enummerating them. At some point a method will order buttons by color and sort them by the number in text. So there will be yellow buttons together, then red one and so on. Everything works ok except that buttons do not change as soon as button.setBackground() is called, the window and it's elements will change only after the method that process everything finishes. The app is very simple and it switches buttons, what I do is that I change their color and text so the user sees on the screen how they swap until everything is in order.

I am sharing code and I added a "press key to continue" method so that you can see where the problem is. I'm only doing one button change to make it easier, the idea is that when I change the background to green I want to see the change on the GUI and not after desfragmentarUnidad method ends. Think that i will have a for loop to sort and change colors etc etc. I wanna see changes each iteration and not until the job is done

On the easy example i'm just changing the background of first button to green, however the change does not show up. I've tried lots of thing and none of them work, like getting the JPanel and calling revalidate and repaint methods to update. I also tried the setVisible(true) and does not work either.

I think that because of my array of buttons does not have an actionListener it is not working, I really don't know what the problem is. I have the Jframe and the process of data in different classes and I think there is something missing in my main app that does not call properly to the window class.

Here is some code, One button (JButtonReiniciar) will call a method that randomly give some colour to buttons and set a number, the other button would start a simulation like a disc defrag, it would get all the buttons with the same color together and then sort by their number, the user has to see how buttons change one by one and not when it's all done.

and for the window:

The picture I attached shows how the buttons are before the procedure, as work is being done I always see the same thing until everything finish. I will really appreciate your comments, greetings
8 months ago