Search code examples
javauser-interfacejbuttonpoint

Converting Point to Button


Is it possible to convert from point to JButton in java, just as you convert from int to string .. Any help is acceptable, please I need help on it. Thanks!

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;

    import javax.swing.*;

    import java.awt.*;


    @SuppressWarnings("serial")
    public class Beginner extends JPanel {

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

        int n = 10;

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


        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) {

                             JButton source = (JButton) p.getSource();
                             //where the problem lies
                             if (source.equals(points.get(0))) {

                                //some action....

                             }

                         }
                     });

                      }
                       }
          public static void main(String [] args){
                 JFrame frame = new JFrame();
                 //set JFrame properties
                 JButton start = new JButton ("CLick here to start");

          start.addActionListener(new ActionListener(){
                  public void actionPerformed (ActionEvent et) {
                  Beginner novice = new Beginner();
                  //set Beginner properties (remember it extends JPanel);
           }
              });

                 } }

Solution

  • Probbaly you will need to add the missing lines into your code just like this, I've just tested it and it works.

    • Import the Point class from awt package.
    • Use the main method to execute your code by creating a new instance of the class.
    • Also you need to extend the JFrame class to be able to call the setLayout and add methods.

      package javaapplication6;
      
      import java.awt.Color;
      import java.awt.Font;
      import java.awt.Point;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.util.ArrayList;
      import java.util.List;
      import java.util.Random;
      import javax.swing.JButton;
      import javax.swing.JPanel;
      import javax.swing.SpringLayout;
      import javax.swing.JFrame;
      
      public class Beginner extends JFrame {
      
          Random rand = new Random();
      
          int n = 10;
      
          static List<Point> points = new ArrayList<Point>();
      
          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) {
      
                  JButton quest = new JButton("Question " + count);
      
                  quest.setForeground(Color.BLACK);
                  Font fonte = new Font("Script MT Bold", Font.PLAIN, 20);
                  quest.setFont(fonte);
                  quest.addActionListener(new ActionListener() {
                      @Override
                      public void actionPerformed(ActionEvent et) {
                          System.out.println(quest.getText());
                          //do something else
                      }
      
                  });
                  add(quest);
      
                  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);
      
                  count++;
              }
              setSize(700, 700);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
          }
      
          public static void main(String[] args) {
              new Beginner();
          }
      }