samedi 1 octobre 2016

Detecting if a all points have been checked in a JPanel

I created an array of about 11 buttons in a JPanel using springlayout, then set actionListener on them. However, I want to make the program able to detect when all buttons have been points have been checked, after which some action takes place. Here is my piece of code!

public class Beginner extends JPanel {

    static JButton quest;
    Random rand = new Random();

    int n = 10;

    static List <Point> points = new ArrayList<Point> ();

    Quest1 q1;
    Quest2 q2;
    Quest3 q3;
    Quest4 q4;
    Quest5 q5;
    Quest6 q6;
    Quest7 q7;
    Quest8 q8;
    Quest9 q9;
    Quest10 q10;
    Quest11 q11;

    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 ActionListener() {
                           public void actionPerformed (ActionEvent p) {


                                if (point.equals(points.get(0))) {


                                    q1 = new Quest1();


                                }   
                               else if (point.equals(points.get(1))) {

                                    q2 = new Quest2();

                                                        }
                               else if (point.equals(points.get(2))) {

                                       q3 = new Quest3();

                                                       }
                             else if (point.equals(points.get(3))) {

                                q4 =  new Quest4();
                        }

            else if (point.equals(points.get(4))) {

                q5 = new Quest5();
                                                    }

            else if (point.equals(points.get(5))) {

                q6 = new Quest6();

                                    }  
            else if (point.equals(points.get(6))) {

                q7 = new Quest7();

                                }  

 else if (point.equals(points.get(7))) {

    q8 = new Quest8();


                    }  

 else if (point.equals(points.get(8))) {

    q9 = new Quest9 () ;


        }  
 else if (point.equals(points.get(9))) {

    q10 = new Quest10() ;


                }  
 else if (point.equals(points.get(10))) {

    q11 =  new Quest11 ();

   // I would love to detect if all points have been checked, then some action takes place. This is where my problem is!



        }           
                } 
                       });
                   }

                        }
}

Aucun commentaire:

Enregistrer un commentaire