Okay so, I'm trying to make a tic tac toe that has a network option. I've wrote this code but I'm having a little problem.
public void actionPerformed(ActionEvent e)
{
JButton temp=(JButton)e.getSource();
//temp.setText("X");
if(turn%2==0)
{
temp.setText("X");
temp.setIcon(gui.imgX);
temp.setDisabledIcon(gui.imgX);
gui.repaint();
gui.revalidate();
turn++;
try {
for(int i=0;i<gui.buttons.length;i++)
if(temp==gui.buttons[i])
out.write(i);
} catch (IOException e1) {
e1.printStackTrace();
}
}
if(turn%2==1)
{
try {
System.out.println(turn);
gui.buttons[in.read()].setText("O");
gui.revalidate();
System.out.println(turn);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
turn++;
}
}
Okay so, whenever I click on a button, it runs inside the next if condition(turn%2==0), but doesn't set the button icon or text. However, it actually sets it somehow in the background, so I icon and text come visible ONLY when it finishes the whole actionPerformed and reads an int from the other pc. So basically, it displays "X" and "O" together after finishing the whole actionPerformed method. How can I fix that so it shows "X" when a button gets pressed without waiting to read from the other pc. Note that it successfully write the int in the other pc and the button text/icon get changed there without waiting the other if condition.
At present you are mixing the UI things with the logic/ controller. I think it would be helpful, if you split it. I.e. while processing the ActionEvent you are waiting for the response from the "remote player", blocking all the other UI things.
I would try to move the control (whos turn it is and reading from remote station) into a background thread and then update the UI only if my turn was executed or I received the information from the remote player.
Related
I have a problem that probably has a simple fix but I can't seem to get it to work.
I need my program to pause or wait until the user selects either the skip button, or the positive/negative feedback button, before moving on.
I assume this is going to require basic threading, but I'm not sure how to implement it. Any help will be appreacited.
The gui is displayed in a separate class(GUI) and the rest is another class.
The code is sort of messy as it was coded for a Hackfest in 12 hours.
EDIT: Solved it on my own by removing button listeneers and making the variables static.
public void onStatus(Status status) {
//this is a listener from the Twitter4J class. Every time new Tweet comes in it updates.
for (int i = 0; i <= posWords.length; i++) {
if (status.getText().toLowerCase().contains(gui.sCrit.getText())
&& (status.getText().toLowerCase().contains(posWords[i].toLowerCase()))) {
//If the tweet matches this criteria do the following:
String tweet;
Status tempStoreP;
System.out.println("Flagged positive because of " +posWords[i].toLowerCase()+" " + i);
tempStoreP = status;
tweet = tempStoreP.getUser().getName() + ":" + tempStoreP.getText() + " | Flagged as positive \n\r";
gui.cTweet.setText(tweet);
//Write to log
try {
wPLog.append("~" + tweet);
} catch (IOException e) {
}
//Add action listeneer to gTweet.
//here is my problem. I want it to wait until a user has clicked one of these buttons before moving on and getting the next Tweet. It has to pause the thread until a button is clicked then it can only move on to getting another Tweet.
//the problem is that the button listener uses local variables to work.
gui.gTweet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (gui.pTweet.getText().equalsIgnoreCase("")) {
gui.pTweet.setText("Please type a response");
} else if (gui.pTweet.getText().equalsIgnoreCase("Please type a response")) {
gui.pTweet.setText("Please type a response");
} else {
try {
Status sendPTweet = twitter
.updateStatus("#" + tempStoreP.getUser().getScreenName() + " "
+ gui.pTweet.getText());
} catch (TwitterException e1) {
}
}
gui.gTweet.removeActionListener(this);
}
});
//add Aaction listert to sTweet
gui.sTweet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
wPLog.append("Skipped \n\r");
} catch (IOException e1) {
}
gui.sTweet.removeActionListener(this);
}
});
}
Thank you for any help. On a side note, if anyone can tell me why when the button is clicked, it loops and spams people with the same message, it will be helpful. Thank you.
I thought about the multithreading thing and i think it isn't easy enough.
If i were you i would disable all controls except the button that is to click.
Example:
JButton button = new JButton( "Test );
button.setEnabled( false );
The user won't be able to click the button until you use button.setEnabled( true );, if you just disable all controls but the button that should be fine.
In my understand of your problem you can use Multithreading like this:
try{
while(!isSkipClicked){
//your multithreading code
}
//code to move...
}
or
you can use dispose method if you have 2 JFrame.
or
you can use multiple JPanel like while skip button clicked hide pnl1 and show pnl2 using method
pnl1.setvisible(false);
pbl2.setVisible(true);
I assume this is going to require basic threading,
Not true at all.
You say you want your program to "wait." That word doesn't actually mean much to a Swing application. A Swing application is event driven. That is to say, your program mostly consists of event handlers that are called by Swing's top-level loop (a.k.a., the Event Dispatch Thread or EDT) in response to various things happening such as mouse clicks, keyboard events, timers, ...
In an event driven program, "Wait until X" mostly means to disable something, and then re-enable it in the X handler.
Of course, Swing programs sometimes create other threads to handle input from sources that the GUI framework does not know about and, to perform "background" calculations. In that case, "Wait until X" might mean to signal some thread to pause, and then signal it to continue its work in the X handler.
I am having issues setting up a computer vs computer loop in my java game applet. I have been trying for 3 days now to effectively add a one second delay between the two computer-player turns, while also repainting the board. I have tried try/catch/thread.sleep and wait and a few other tricks, however none have been successful. In the program's current state, when a computer vs computer game is initiated, the program freezes for the duration of the game (with one second delays between moves) and then displays the final board when the game has finished. How can I make it so the program repaints/delays after each move? From all of the reading I have done, I am aware that the following implementation will not work but my issue is I cannot figure out how to do it any other way.
Thanks in advance!
The following code is inside my actionPerformed listener method
if (event.getSource() == startAIvAI)
{
drawing.clear();
while (drawing.hasWon() == -1 && !drawing.isFull())
{
go1();
repaint();
try {
Thread.sleep(1000);
} catch (Exception e) {}
go2();
repaint();
try {
Thread.sleep(1000);
} catch (Exception e) {}
}
}
When you invoke Thread.sleep(1000); which Thread do you think you're pausing? Probable the one handling event given your code start with if (event.getSource() == startAIvAI).
You should read this to understand what to do: Sleep method locks my GUI
I wrote a complex java program, that has a GUI.
In the GUI I have three buttons, "play", "play all" and "reset", and some table.
When I click play, the program does its logic and then prints the output to the table i mentioned. this is working fine, as intended. after a certain amount of "play" operations, we printed all the data to the table and so we are done and can no longer click it. at the point, i disable the play button.
When I click play all, I want it to be as if I clicked "play" until it is no longer possible. So I wrote this code:
public synchronized void actionPerformed(ActionEvent event)
{
if(event.getSource() == playAllButton)
{
while(playButton.isEnabled())
{
playButton.doClick();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if(event.getSource() == playButton)
{
Command command = listOfCommands.get(commandNumber++);
if(command.getCommandType() == "PF")
{
PFLabel.setText("PF Count:" + (++PFCount));
}
if(command.getCommandType() == "PR")
{
PRLabel.setText("PR Count:" + (++PRCount));
}
if(command.getCommandType() == "GP")
{
if(checkIfPF())
{
addPageToRam(((GPCommand)command).getPage());
}
else
{
if(checkIfPR(((GPCommand)command).getPage().getPageId()))
{
replacePage(((GPCommand)command).getPage(),getWithWhoToReplace(((GPCommand)command).getPage().getPageId()));
}
}
}
}
if(commandNumber == listOfCommands.size()) // we are done!
{
playButton.setEnabled(false);
}
}
As I mentioned, when I click the play button, it immediately prints the result as intended.
But when I click play all, it runs for a very long time without displaying any output, and then at the end prints just the final result. This isn't what I want. What I want is for it to print the current result, wait a bit so I can look at it, and then print the next result.
Even when I remove the Thread.sleep it is very slow (and still only prints the last result).
Is there a way to make it run faster? And more importantly - how do I make it wait before clicking the button again? I want to view the current result for a while before it clicks again.
In the comments section I figured out, that the issue could be solved by using SwingWorkers and moving the long-time-performing code to an asynchronous task. Mr. Coobird has posted a way, how to do this here.
so Im doing a project of a tic tac toe using artificial intelligence, and I wanted to make an option so you could replay the game to see what happend in the last round, My intentions were to make the replay as its actually playing right now, which means after each move will be shown there wil lbe a delay and then the next one will occur.And instead of working as expected the delay jsut goes all at a time without anything happening until suddenyl the whole game just shows up without beinge displayed as a game but as a finishing screen' anyway here is the code:
replayMode=true;
setStatus("Replay Playing...");
ifButtonsEnabled(true);
initBoard();
int turn=0;
Move current;
for(int i=0;i<replayer.getSizeOfVect();i++)
{
current=replayer.getMoveAt(i);
if(i%2==0)
{
buttons[current.getI()][current.getJ()].setText("X");
}
else
{
buttons[current.getI()][current.getJ()].setText("O");
}
try {
Thread.sleep(650);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
setStatus("Replay ended.");
replayMode=false;
Usually when you see this kind of effect in a GUI program, it's because you need to yield control back to the GUI thread's event loop for any drawing to happen. Rather than Thread.sleep I recommend using a timer with a 650ms interval and doing one "step" of the animation with each tick of the timer.
Here is my loop code (This is the only code relating to my loop):
while(true)
{
try {
Thread.sleep(20);
System.out.println("1");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I launch the applet, it white screens and I cannot close it unless I press the "Terminate" button in eclipse.
You're blocking the UI Thread with an infinite while loop. You don't say whether you're using an AWT or Swing applet, either way the result will be the same. If you're using a Swing applet, use a Swing Timer. If you're using the old heavyweight AWT, convert it to Swing and follow the previous advice.
As said you made an infinity loop with:
while(true){
//something
}
There is no break; So why or what should stop the loop except of a thrown exception?
To see when a InterruptedException is thrown you should read the JavaDoc:
http://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.html
You are hogging the applets EDT. You need to run your loop in another thread. Try adding Thread gameThread; as a variable and use
gameThread = new Thread() {
public void run() {
while (condition) {
//code here
}
}
}
and then gameThread.start() both in your applet start method and gameThread.join() in your applet stop method.