テキスト領域の更新または「再描画」に問題があります。for ループのサイクルごとにテキスト領域を更新したいと考えています。paint();
andrepaint();
関数の呼び出しや and の使用など、いくつか試してみましたが、text.append();
うまくtext.setText();
いかないようです。また、繰り返しごとにテキストを追加するように for ループを設定しています。これが私のコードです。どんな助けも大歓迎です。
ニック。
package ********_****_rps;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AutomatedRoshamboRPS extends javax.swing.JFrame {
int num = 0;
int compnum = 0;
int earnings = 0;
int wins = 0;
int losses = 0;
int ties = 0;
int roshamboCount = 0;
int gamesPlayed = 0;
int i;
int games = 0;
String a = "", b;
void updateStats() {
String statsToString = stats.getText();
if (num - compnum == 1 || num - compnum == -2) //won
{
wins++;
gamesPlayed++;
} else if (num - compnum == 0) //tied
{
ties++;
gamesPlayed++;
} else { //lost
losses++;
gamesPlayed++;
}
earnings = +(wins * 2) - (losses * 2) + (roshamboCount * 10) - (roshamboCount * 2);
stats.setText("STATISTICS:"
+ "\nGames Played:" + gamesPlayed
+ "\nEarnings:" + earnings
+ "\nWins:" + wins
+ "\nTies:" + ties
+ "\nLosses:" + losses
+ "\nRoshambo:" + roshamboCount);
}
void game() {
String userChoice;
compnum = (int) Math.floor((Math.random() * 3) + 1);
switch (num) {
case 1:
userChoice = "ROCK";
break;
case 2:
userChoice = "PAPER";
break;
case 3:
userChoice = "SCISSORS";
break;
default:
userChoice = "ROCK";
}
if (num - compnum == 1 || num - compnum == -2) {
text.append("\nYou won!");
a += userChoice;
} else if (num - compnum == 0) {
text.append("\nYou tied!");
} else {
text.append("\nYou lost!");
a = "";
}
if (a.equals("ROCKROCKROCK") || a.equals("PAPERPAPERPAPER") || a.equals("SCISSORSSCISSORSSCISSORS")) {
roshamboCount++;
a = "";
if (text.getLineCount() > 10000) {
text.setText("");
}
}
}
public AutomatedRoshamboRPS() {
initComponents();
text.setText("ROCK PAPER SICSSORS"
+ " \nSelect your throw by clicking on one \nof the options to the left.\n"
+ "---------------------------------------------------");
rock.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
b = numGames.getText();
games = Integer.parseInt(b);
for (int l = 0; l < games; l++) {
num = 1;
game();
updateStats();
}
}
});
paper.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
b = numGames.getText();
games = Integer.parseInt(b);
for (int l = 0; l < games; l++) {
num = 2;
game();
updateStats();
}
}
});
scissors.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
b = numGames.getText();
games = Integer.parseInt(b);
for (int l = 0; l < games; l++) {
num = 3;
game();
updateStats();
}
}
});
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
buttonGroup2 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
text = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
stats = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
numGames = new javax.swing.JTextField();
rock = new javax.swing.JToggleButton();
paper = new javax.swing.JToggleButton();
scissors = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
jLabel1.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
jLabel1.setText("Automated Roshambo");
text.setColumns(20);
text.setEditable(false);
text.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
text.setLineWrap(true);
text.setRows(5);
jScrollPane1.setViewportView(text);
stats.setColumns(20);
stats.setEditable(false);
stats.setRows(5);
jScrollPane2.setViewportView(stats);
jLabel5.setText("Number of games");
jLabel6.setText("to simulate:");
rock.setIcon(new javax.swing.ImageIcon(getClass().getResource("/haughton_nick_rps/Rock.png"))); // NOI18N
rock.setContentAreaFilled(false);
paper.setIcon(new javax.swing.ImageIcon(getClass().getResource("/haughton_nick_rps/Paper.png"))); // NOI18N
paper.setContentAreaFilled(false);
scissors.setIcon(new javax.swing.ImageIcon(getClass().getResource("/haughton_nick_rps/Scissors.png"))); // NOI18N
scissors.setContentAreaFilled(false);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jLabel1)
.add(80, 80, 80))
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(36, 36, 36)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(6, 6, 6)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
.add(rock, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(scissors, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(paper))
.add(35, 35, 35)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel3)
.add(jLabel2)
.add(jLabel4)))
.add(jLabel5)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, numGames)
.add(org.jdesktop.layout.GroupLayout.LEADING, jLabel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.add(18, 18, 18)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 244, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(32, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(18, 18, 18)
.add(jLabel1)
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPane1)
.add(layout.createSequentialGroup()
.add(jLabel5)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel6)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(numGames, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(0, 0, Short.MAX_VALUE)
.add(jLabel2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel4)
.add(208, 208, 208))
.add(layout.createSequentialGroup()
.add(rock, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(paper, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 60, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(scissors, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 61, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(0, 0, Short.MAX_VALUE)))))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 125, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new AutomatedRoshamboRPS().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.ButtonGroup buttonGroup2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextField numGames;
private javax.swing.JToggleButton paper;
private javax.swing.JToggleButton rock;
private javax.swing.JToggleButton scissors;
private javax.swing.JTextArea stats;
private javax.swing.JTextArea text;
// End of variables declaration
}