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....