Problem With Sliders and Area of a circle-
807597Nov 16 2005 — edited Nov 16 2005Hey everyone--
New to Java and trying to work on an application where an oval is being manipulated by a slider and i have to caluculate and display the semi major(basically the diameter) and semi minor axes and caluculate the area of the ellipse. The center of the ellipse. should be on the center of the panel. A JSlider should be provided to alter the length of the major axis of the array. Note: the major axis may be along the x-axis (horizontal) or y-axis (vertical). The slider should be set up to represent percentage of the major axis. When the slider is at 0%, the major axis length would be equal to zero. When the slider is at 100%, the major axis length should be equal to the length of the JPanel.
The major part I am having trouble with is how to get the oval to start at the center of the JFrame and how to get the circle to stop at the ends of the width of the JFrame and the ends of the Height of the JFrame.
I have two different classes here they are below: the ovalPanel just draws a circle on it self and then the slider demo is an extension of that and is being used to calculate the area and semi major etc...
import java.awt.*;
import javax.swing.*;
public class ovalPanel extends JPanel
{ int diameter = 10;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillOval(246,237,diameter,diameter);
}
public void setDiameter(int newDiameter)
{
diameter = (newDiameter >= 0 ? newDiameter : 10);
repaint();
}
public Dimension getPreferredSize()
{
return new Dimension(500,500);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class sliderDemo extends JFrame
{ private JSlider diameterSlider;
private ovalPanel myPanel;
private JPanel textPanel;
private JTextField textField1, textField2, textField3;
private JLabel label1, label2, label3;
public sliderDemo()
{
super("Slider & Ellipse Demo");
myPanel = new ovalPanel();
myPanel.setBackground(Color.YELLOW);
diameterSlider= new JSlider(SwingConstants.HORIZONTAL, 0, 400, 10);
diameterSlider.setMajorTickSpacing(10);
diameterSlider.setPaintTicks(true);
diameterSlider.addChangeListener(
new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
myPanel.setDiameter(diameterSlider.getValue());
}
}
);
textField1= new JTextField(5);
textField2= new JTextField(5);
textField3= new JTextField(5);
label1=new JLabel("semiMajor: ");
label2=new JLabel("semiMinor: ");
label3=new JLabel(" Area: ");
textPanel = new JPanel();
textPanel.add(label1);
textPanel.setLayout(new GridLayout (1,3));
textPanel.add(textField1);
textPanel.add(label2);
textPanel.add(textField2);
textPanel.add(label3);
textPanel.add(textField3);
Container container = getContentPane();
container.add(diameterSlider, BorderLayout.SOUTH);
container.add(myPanel, BorderLayout.CENTER);
container.add(textPanel, BorderLayout.NORTH);
setSize(500, 500);
setVisible(true);
}
public static void main(String args [])
{
sliderDemo application = new sliderDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Anyone help out with this--would be greatly appreciated
rich_crush