I'm having some issues on using a conditionally assigned value in more than one class. I want to apply the use of a value in one class if and only if a condition is met, then use it in some-other class to add up to a total score. this is my code!
@SuppressWarnings("serial")
public class Beginner extends JPanel {
static JButton quest;
Random rand = new Random();
int n = 10;
static List <Point> points = new ArrayList<Point> ();
int scores;
int fails;
public Beginner() {
int radius = 200;
Point center = new Point (250, 250);
double angle = Math.toRadians(360 / n);
points.add(center);
for (int i = 0; i < n; i++) {
double theta = i * angle;
int dx = (int) (radius * Math.sin(theta));
int dy = (int) (radius * Math.cos(theta));
Point p = new Point (center.x + dx , center.y + dy);
points.add(p);
}
draw (points);
}
public void draw (List<Point> points) {
JPanel panels = new JPanel();
SpringLayout spring = new SpringLayout();
int count = 1;
for (Point point: points) {
quest = new JButton("Question " + count);
quest.setForeground(Color.BLACK);
Font fonte = new Font("Script MT Bold", Font.PLAIN, 20);
quest.setFont(fonte);
add (quest);
count++;
spring.putConstraint(SpringLayout.WEST, quest, point.x, SpringLayout.WEST, panels );
spring.putConstraint(SpringLayout.NORTH, quest, point.y, SpringLayout.NORTH, panels );
setLayout(spring);
panels.setOpaque(false);
panels.setVisible(true);
panels.setLocation(5,5);
add(panels);
quest.addActionListener(new java.awt.event.ActionListener(){
@Override
public void actionPerformed (ActionEvent p) {
if (point.equals(points.get(0))) {
new Quest1();
JButton source = (JButton) p.getSource();
source.setEnabled(false);
source.setBackground(Color.GREEN);
}
else if (point.equals(points.get(1))) {
new Quest2();
JButton source = (JButton) p.getSource();
source.setEnabled(false);
source.setBackground(Color.GREEN);
}
else if (point.equals(points.get(2))) {
new Quest3();
JButton source = (JButton) p.getSource();
source.setEnabled(false);
source.setBackground(Color.GREEN);
}
else if (point.equals(points.get(3))) {
new Quest4();
JButton source = (JButton) p.getSource();
source.setEnabled(false);
source.setBackground(Color.GREEN);
}
else if (point.equals(points.get(4))) {
new Quest5();
JButton source = (JButton) p.getSource();
source.setEnabled(false);
source.setBackground(Color.GREEN);
}
Now, this is what my first called-up class (named Quest1) looks like
public class Quest1 {
//sound files to be shuffled and played!
String [] word = { "C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/audio.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/bomb.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/baby.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/gym.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/hearing.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/goal.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/manifest.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/mountain.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/market.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/debate.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/election.wav",
"C:/Users/HP/Workspace/spellingApp/src/Level1/Question 1/number.wav",
};
//matching strings for each file
String [] words = { "Audio", "Bomb", "Baby", "Gym", "Hearing", "Goal", "Manifest","Mountain", "Market", "Debate", "Election", "Number" };
Random rand = new Random();
int random = rand.nextInt(word.length);
String temp = word[random];
int scores;
int fails;
public Quest1 () {
//announcing the JComponents to be used
JFrame frame = new JFrame ();
JButton click = new JButton("Play");
JTextField type = new JTextField(15);
JLabel pic = new JLabel(new ImageIcon("C:\\Users\\HP\\Desktop\\Sample pics\\1.png"));
JButton score = new JButton ("Check My Answer");
JPanel cont = new JPanel ();
//set up frame properties
frame.getContentPane().setBackground(Color.WHITE);
frame.setLayout(new FlowLayout());
frame.setUndecorated(true);
frame.setResizable(false);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.add(cont, BorderLayout.CENTER);
frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
frame.setVisible(true);
//setting layout of JPanel
cont.setLayout(new GridBagLayout());
cont.setOpaque(false);
cont.setPreferredSize(new Dimension(500,500));
cont.setLocation(100, 50);
GridBagConstraints g = new GridBagConstraints();
g.anchor = GridBagConstraints.WEST;
g.gridx = 4;
g.gridy = 2;
g.gridwidth = 2;
g.insets = new Insets (70, 2, 2, 2);
cont.add(pic , g);
g.anchor = GridBagConstraints.WEST;
g.gridx = 4;
g.gridy = 5;
g.gridwidth = 2;
g.insets = new Insets (50, 2, 2, 2);
cont.add(click, g);
click.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e) {
//sound file shuffled and played
}
});
g.anchor = GridBagConstraints.EAST;
g.gridx = 10;
g.gridy = 4;
g.gridwidth = 5;
g.insets = new Insets(30, 2, 2, 10);
Font fonty = new Font("Lucida", Font.PLAIN, 15);
type.setFont(fonty);
cont.add(type, g);
g.anchor = GridBagConstraints.SOUTH;
g.gridx = 9;
g.gridy = 8;
g.gridwidth = 2;
g.insets = new Insets(2, 2, 2, 50);
cont.add(score, g);
score.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent tx) {
if (type.getText().equals(words[random])) {
//sound file played
scores+= 3;
String a = "Correct!..... The answer is " + words[random];
JOptionPane.showMessageDialog(null, a);
frame.dispose();
return;
}
else {
//sound file played again
fails+=0;
// This is where my problem is: The other 10 classes are like
//this, I want to get this value and that of scores (depending on the
//condition that is met), and add them up in the class beginner! )
String a = "Sorry, The Answer is " + words[random];
JOptionPane.showMessageDialog(null, a);
frame.dispose();
}
}
});
//sound file played
}
}