in this program i have to use listners, and radio buttons and buttons....right now i am doing the first part of the program where when the user selects the radio button for circle two circles appear on the screen.....

import javax.swing.JFrame;

public class shapes
{
	public static void main(String[] args)
	{
		JFrame frame= new JFrame("Shapes and Colors");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.getContentPane().add(new ShapesPanel());
		
		frame.pack();
		frame.setVisible(true);
	}
}
//********************************************************************
//  LeftRightPanel.java       Authors: Lewis/Loftus
//
//  Demonstrates the use of one listener for multiple buttons.
//********************************************************************

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ShapesPanel extends JPanel
{
   private JRadioButton circle, square;
   private JLabel label;
   private JPanel buttonPanel;
	private Circle circle1, circle2;
	private Square square1, square2;

   //-----------------------------------------------------------------
   //  Constructor: Sets up the GUI.
   //-----------------------------------------------------------------
   public ShapesPanel()
   {
      circle = new JRadioButton ("Circle");
      square = new JRadioButton ("Square");
		
		circle1 = new Circle (30, Color.white, 70, 35);
		circle2 = new Circle (50, Color.white, 30, 20);
		square1 = new Square (50, 50, 40, 40)

      ButtonListener listener = new ButtonListener();
      circle.addActionListener (listener);
      square.addActionListener (listener);

      label = new JLabel ("Shapes&Colors");

      buttonPanel = new JPanel();
      buttonPanel.setPreferredSize (new Dimension(200, 40));
      buttonPanel.setBackground (Color.white);
      buttonPanel.add (circle);
      buttonPanel.add (square);

      setPreferredSize (new Dimension(200, 80));
      setBackground (Color.cyan);
      add (label);
      add (buttonPanel);
   }

   //*****************************************************************
   //  Represents a listener for both buttons.
   //*****************************************************************
   private class ButtonListener implements ActionListener
   {
      //--------------------------------------------------------------
      //  Determines which button was pressed and sets the label
      //  text accordingly.
      //--------------------------------------------------------------
      public void actionPerformed (ActionEvent event)
      { 	
		  		if (event.getSource() == circle)
        	 	{
			   	circle1.draw();
					circle2.draw();
				}
				
         	else
				{
					square1.draw();
					square2.draw();
				}  
      }
   }
}
import java.awt.*;

public class Circle
{
	private int diameter, x, y;
	private Color color;
	
	public Circle(int size, Color shade, int upperX, int upperY)
	{
		diameter = size;
		color = shade;
		x = upperX;
		y = upperY;
	}
	
	public void draw(Graphics page)
	{
		page.setColor (color);
		page.fillOval(x, y, diameter, diameter);
	}
	
	public void setDiameter (int size)
	{
		diameter = size;
	}
	
	public void setColor (Color shade)
	{
		color = shade;
	}
	
	public void setX (int upperX)
	{
		x = upperX;
	}
	
	public void setY (int upperY)
	{
		y = upperY;
	}
	
	public int getDiameter()
	{
		return diameter;
	}
	
	public Color getColor()
	{
		return color;
	}
	
	public int getX()
	{
		return x;
	}
	
	public int getY()
	{
		return y;
	}
}

error is:

ShapesPanel.java:60: draw(java.awt.Graphics) in Circle cannot be applied to ()
			   	circle1.draw();
			   	       ^
ShapesPanel.java:61: draw(java.awt.Graphics) in Circle cannot be applied to ()
					circle2.draw();
					       ^
ShapesPanel.java:66: cannot find symbol
symbol  : variable square1
location: class ShapesPanel.ButtonListener
					square1.draw();
					^
ShapesPanel.java:67: cannot find symbol
symbol  : variable square2
location: class ShapesPanel.ButtonListener
					square2.draw();
					^
4 errors

i know the reason for the square error because i havent created the class for it yet....

Look how you have defined the draw method in your Circle class, and how you are calling it.

About the "square1" error you have declared it correctly at the ShapesPanel class but you are using inside the private ButtonListener class.
Maybe you should do this:

public class ShapesPanel extends JPanel implements ActionListener {
  public ShapesPanel() {
      ....
      circle.addActionListener (this);
      square.addActionListener (this);
  }  


public void actionPerformed (ActionEvent event)
      { 	
		  		if (event.getSource() == circle)
        	 	{
			   	circle1.draw();
					circle2.draw();
				}
				
         	else
				{
					square1.draw();
					square2.draw();
				}  
      }
}

This is my program now still wont work

import javax.swing.JFrame;

public class shapes
{
	public static void main(String[] args)
	{
		JFrame frame= new JFrame("Shapes and Colors");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.getContentPane().add(new ShapesPanel());
		
		frame.pack();
		frame.setVisible(true);
	}
}
//********************************************************************
//  LeftRightPanel.java       Authors: Lewis/Loftus
//
//  Demonstrates the use of one listener for multiple buttons.
//********************************************************************

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ShapesPanel extends JPanel implements ActionListener
{
   private JRadioButton circle, square;
   private JLabel label;
   private JPanel buttonPanel;
	private Circle circle1, circle2;
	private Square square1, square2;

   //-----------------------------------------------------------------
   //  Constructor: Sets up the GUI.
   //-----------------------------------------------------------------
   public ShapesPanel()
   {
      circle = new JRadioButton ("Circle");
      square = new JRadioButton ("Square");
		
		circle1 = new Circle (30, Color.white, 70, 35);
		circle2 = new Circle (50, Color.white, 30, 20);
		square1 = new Square (50, 50, 50, 50);

      ButtonListener listener = new ButtonListener();
      circle.addActionListener (this);
      square.addActionListener (this);

      label = new JLabel ("Shapes&Colors");

      buttonPanel = new JPanel();
      buttonPanel.setPreferredSize (new Dimension(200, 40));
      buttonPanel.setBackground (Color.white);
      buttonPanel.add (circle);
      buttonPanel.add (square);

      setPreferredSize (new Dimension(200, 80));
      setBackground (Color.cyan);
      add (label);
      add (buttonPanel);
   }

   //*****************************************************************
   //  Represents a listener for both buttons.
   //*****************************************************************
   private class ButtonListener implements ActionListener
   {
      //--------------------------------------------------------------
      //  Determines which button was pressed and sets the label
      //  text accordingly.
      //--------------------------------------------------------------
      public void actionPerformed (ActionEvent event)
      { 	
		  		if (event.getSource() == circle)
        	 	{
			   	circle1.draw(page);
					circle2.draw(page);
				}
				
         	else
				{
					square1.draw(page);
					square2.draw(page);
				}  
      }
   }
}
import java.awt.*;

public class Circle
{
	private int diameter, x, y;
	private Color color;
	
	public Circle(int size, Color shade, int upperX, int upperY)
	{
		diameter = size;
		color = shade;
		x = upperX;
		y = upperY;
	}
	
	public void draw(Graphics page)
	{
		page.setColor (color);
		page.fillOval(x, y, diameter, diameter);
	}
	
	public void setDiameter (int size)
	{
		diameter = size;
	}
	
	public void setColor (Color shade)
	{
		color = shade;
	}
	
	public void setX (int upperX)
	{
		x = upperX;
	}
	
	public void setY (int upperY)
	{
		y = upperY;
	}
	
	public int getDiameter()
	{
		return diameter;
	}
	
	public Color getColor()
	{
		return color;
	}
	
	public int getX()
	{
		return x;
	}
	
	public int getY()
	{
		return y;
	}
}
import java.awt.*;

public class Square
{
        private double side;
   
        public Square(double squareOne)
        {
                side = squareOne;
        }
        
        private void setSide(double squareOne)
        {
                side = squareOne;
        }
        
        public double getSide()
        {
                return side;
        }
        
        public double getPerimeter()
        {
                return 4 * side;
        }

        public double getArea()
        {
                return side * side;
        }
}

This is my program now still wont work

import javax.swing.JFrame;

public class shapes
{
	public static void main(String[] args)
	{
		JFrame frame= new JFrame("Shapes and Colors");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.getContentPane().add(new ShapesPanel());
		
		frame.pack();
		frame.setVisible(true);
	}
}
//********************************************************************
//  LeftRightPanel.java       Authors: Lewis/Loftus
//
//  Demonstrates the use of one listener for multiple buttons.
//********************************************************************

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ShapesPanel extends JPanel implements ActionListener
{
   private JRadioButton circle, square;
   private JLabel label;
   private JPanel buttonPanel;
	private Circle circle1, circle2;
	private Square square1, square2;

   //-----------------------------------------------------------------
   //  Constructor: Sets up the GUI.
   //-----------------------------------------------------------------
   public ShapesPanel()
   {
      circle = new JRadioButton ("Circle");
      square = new JRadioButton ("Square");
		
		circle1 = new Circle (30, Color.white, 70, 35);
		circle2 = new Circle (50, Color.white, 30, 20);
		square1 = new Square (50, 50, 50, 50);

      ButtonListener listener = new ButtonListener();
      circle.addActionListener (this);
      square.addActionListener (this);

      label = new JLabel ("Shapes&Colors");

      buttonPanel = new JPanel();
      buttonPanel.setPreferredSize (new Dimension(200, 40));
      buttonPanel.setBackground (Color.white);
      buttonPanel.add (circle);
      buttonPanel.add (square);

      setPreferredSize (new Dimension(200, 80));
      setBackground (Color.cyan);
      add (label);
      add (buttonPanel);
   }

   //*****************************************************************
   //  Represents a listener for both buttons.
   //*****************************************************************
   private class ButtonListener implements ActionListener
   {
      //--------------------------------------------------------------
      //  Determines which button was pressed and sets the label
      //  text accordingly.
      //--------------------------------------------------------------
      public void actionPerformed (ActionEvent event)
      { 	
		  		if (event.getSource() == circle)
        	 	{
			   	circle1.draw(page);
					circle2.draw(page);
				}
				
         	else
				{
					square1.draw(page);
					square2.draw(page);
				}  
      }
   }
}
import java.awt.*;

public class Circle
{
	private int diameter, x, y;
	private Color color;
	
	public Circle(int size, Color shade, int upperX, int upperY)
	{
		diameter = size;
		color = shade;
		x = upperX;
		y = upperY;
	}
	
	public void draw(Graphics page)
	{
		page.setColor (color);
		page.fillOval(x, y, diameter, diameter);
	}
	
	public void setDiameter (int size)
	{
		diameter = size;
	}
	
	public void setColor (Color shade)
	{
		color = shade;
	}
	
	public void setX (int upperX)
	{
		x = upperX;
	}
	
	public void setY (int upperY)
	{
		y = upperY;
	}
	
	public int getDiameter()
	{
		return diameter;
	}
	
	public Color getColor()
	{
		return color;
	}
	
	public int getX()
	{
		return x;
	}
	
	public int getY()
	{
		return y;
	}
}
import java.awt.*;

public class Square
{
        private double side;
   
        public Square(double squareOne)
        {
                side = squareOne;
        }
        
        private void setSide(double squareOne)
        {
                side = squareOne;
        }
        
        public double getSide()
        {
                return side;
        }
        
        public double getPerimeter()
        {
                return 4 * side;
        }

        public double getArea()
        {
                return side * side;
        }
}

lol ignore the comments

The program doesn't work because you haven't followed ny instructions:

public class ShapesPanel extends JPanel implements ActionListener {
  public ShapesPanel() {
      ....
      circle.addActionListener (this);
      square.addActionListener (this);
  }  


public void actionPerformed (ActionEvent event)
      { 	
		  		if (event.getSource() == circle)
        	 	{
..
				}
				
         	else
				{
				..
				}  
      }
}

ShapesPanel is the one that implements the ActionListener and that is the one that needs to implement the actionPerformed method and that class ( this ) would be the input to the addActionListener.

Edited 6 Years Ago by javaAddict: n/a

This question has already been answered. Start a new discussion instead.