junxi
junxi

Reputation: 174

Writing an ActionListener to draw shapes by pressing button

I need to write a GUI that draws circles/lines every time I press the button Draw Circle/Draw Line.

The frame works just fine but I have trouble implementing the ActionListener for this program. I don't know how to connect the buttons with "boolean circZ". Everything I tried gives me errors.

Maybe you can help.

public class fram extends JFrame {
   fram(){
      JPanel panel = new JPanel();
      add(panel);
      JButton btn1 = new JButton("Draw Circle");
      JButton btn2 = new JButton("Draw Lines");
      panel.add(btn1);
      panel.add(btn2);
      MyPanel obj = new MyPanel();
      panel.add(obj);
}

public class MyPanel extends JPanel{
   public boolean circZ = true;

   public void paintComponent(Graphics g){**
      super.paintComponent(g);
         if(cicZ == true){
            g.setColor(Color.BLACK);
            g.drawOval(150, 50, 50, 50);
         }else if(circZ==false){
                 ...
          }
   }
}

public class CRListener implements ActionListener(){

           !!!  I DO NEED HELP HERE  !!!
}

public static void main(String[]args){
   fram f = new fram();
   f.paint(null);
}

Upvotes: 1

Views: 946

Answers (3)

blurfus
blurfus

Reputation: 14031

You need to implement the method:

class CRListener implements ActionListener {

       public void actionPerformed(ActionEvent e) {    
           System.out.println("Hello action listener"); // nothing happens    
       }
}

and then attach it to the button:

btn1.addActionListener(new CRListener ());

Repeat for every button (and make it work) then you can look to optimize it by passing a parameter to the constructor - I'll let you figure out that part :)

UPDATED based on comments

To connect the action listeners above to the panel, you could do the following:

public class fram extends JFrame {

   MyPanel myPanel;

   fram(){
      JPanel panel = new JPanel();
      add(panel);
      JButton btn1 = new JButton("Draw Circle");
      JButton btn2 = new JButton("Draw Lines");

      // add event listeners
      btn1.addActionListener( new CRListener() );
      btn2.addActionListener( new CRListener() );

      panel.add(btn1);
      panel.add(btn2);
      myPanel = new MyPanel();
      panel.add(myPanel);
}

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35096

You can accomplish what you are trying to do by adding anonymous ActionListeners to your buttons (see below). You need to either make your MyPanel final, or a member in the containing fram so that it can be referenced later.

NOTE: In this design, it would be cleaner to make MyPanel an inner class of fram and circZ a member of fram

   MyPanel obj;
   fram(){
      JPanel panel = new JPanel();
      add(panel);
      JButton btn1 = new JButton("Draw Circle");
      JButton btn2 = new JButton("Draw Lines");
      btn1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
           obj.circZ =...;
        }
      });
      btn2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
           obj.circZ =...;
        }
      });
      panel.add(btn1);
      panel.add(btn2);
      obj = new MyPanel();
      panel.add(obj);
  }

Upvotes: 2

Jacob
Jacob

Reputation: 91

What you need to do is add an actionListener to each of the JButtons. Add one to bt1 and one to btn2. In each one, you'll know which one was pressed because the actionListener for that one will fire. In each, handle generating either a circle or line. The way I would do it is to create a CopyOnWriteArrayList for holding the geometry the panel is drawing - you could run into ConcurrentModification otherwise. Also, use either Optional or AtomicRefence for holding the newly generated shapes. In your repaint, you'll need to see if either of the Optionals/AtomicReferences has a new value. If it does, add it to your array list and then remove it from the atomic reference. Below that, loop through the array list and draw all graphics objects it contains.

Upvotes: 0

Related Questions