Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 568
  • Last Modified:

JSlider option... how do I modify this code?

Dear Experts,

I have to modify my current code and add a JSlider option.  Basically what I need to do at the end is to have the user be able to alter the radius with a JSlider.  The radii range must be between 100 and 200.  But when the JSlider is moved, the diameter, circumference, and area information is to be updated and displayed on the JFrame.  The initial radius should be set to 150.  So far I am able to have a random circle and its information displayed but I am not sure how or where I should implement the JSlider.  I am a beginner in java programming for my company I am working for and any help would be greatly appreciated.  Thank you.  This is what I have gotten once to show up (as far as the values are concerned) --- http://www.asiamarie.com/circle1.gif

Here is the code:

// Circle1.java
// Program draws a circle of a random
// diameter and displays the area, diameter,
// and circumference.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Circle1 extends JFrame {
   private CircleCanvas theCanvas;
   private JTextArea display;

   public Circle1()
   {
      super( "Circle1" );
      theCanvas = new CircleCanvas();
      display = new JTextArea( 5, 30 );

      display.setText( "The Radius is: " + theCanvas.getRadius() +
                       "\nThe Diameter is: " + theCanvas.getDiameter()
                       + "\nThe Area is: " + theCanvas.getArea() +
                       "\nThe Circumference is: " +
                       theCanvas.getCircumference() );

      //setLayout( new BorderLayout() );
      getContentPane().add( theCanvas, BorderLayout.CENTER );
      getContentPane().add( display, BorderLayout.SOUTH );
      setSize( 200, 200 );
      show();
   }

   public static void main( String args[] )
   {
      Circle1 app = new Circle1();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );
   }
}

class CircleCanvas extends JPanel {
   private int radius;

   public CircleCanvas()
   {
      radius = ( int )( 1 + Math.random() * 100 );
      setSize( 100, 100 );
   }

   public void paintComponent( Graphics g )
   {  g.drawOval( 0, 0, radius, radius );  }

   public int getDiameter()  {  return ( 2 * radius );  }

   public int getCircumference()
   {  return ( int )( 2 * Math.PI * radius );  }

   public int getArea()
   {  return ( int )( radius * radius * Math.PI );  }

   public int getRadius()  {  return radius;  }
}

 

0
adidas7fire
Asked:
adidas7fire
  • 4
  • 3
  • 2
  • +1
1 Solution
 
suprapto45Commented:
I will help you. Wait for my codes.
0
 
suprapto45Commented:
Since I cannot give you the whole codes directly.

Please read how to get the value of the JSlider.

private JSlider lagSlider;

lagSlider = new JSlider(JSlider.HORIZONTAL,1,100,1);
lagSlider.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
lagSlider.setPaintTicks(true);
lagSlider.addChangeListener(new SliderListener());    

class SliderListener implements ChangeListener {
      public void stateChanged(ChangeEvent e) {
          int lag=lagSlider.getValue();
          System.out.println(lag);
      }
    }

Remember, you must import some packages too. You have to find out :).
0
 
adidas7fireAuthor Commented:
well, that will get me started... thanks a bunch!  i'll see if i can't implement that somehow. ;-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
expertmbCommented:
modified source
ususally slider represents the percentage. hmmmm i am using it to get for radius.
// Circle1.java
// Program draws a circle of a random
// diameter and displays the area, diameter,
// and circumference.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Circle1 extends JFrame {
   private CircleCanvas theCanvas;
   private JTextArea display;
private JSlider lagSlider;
private JPanel panel = new JPanel();
private JPanel panel1 = new JPanel();
static Circle1 app ;
   public Circle1()
   {
      super( "Circle1" );
      theCanvas = new CircleCanvas();
      display = new JTextArea( 5, 30 );

      display.setText( "The Radius is: " + theCanvas.getRadius() +
                       "\nThe Diameter is: " + theCanvas.getDiameter()
                       + "\nThe Area is: " + theCanvas.getArea() +
                       "\nThe Circumference is: " +
                       theCanvas.getCircumference() );

            lagSlider = new JSlider(JSlider.HORIZONTAL,1,100,1);
            lagSlider.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
            lagSlider.setPaintTicks(true);
            lagSlider.addChangeListener(new SliderListener());


            //panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
            panel.setLayout(new GridLayout(3, 1));

      panel.add(theCanvas);
      panel.add(display);
      panel.add(lagSlider);

      getContentPane().add( panel, BorderLayout.CENTER );
      setSize( 200, 200 );
      show();
   }

   public static void main( String args[] )
   {
      app = new Circle1();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit( 0 );
            }
         }
      );
   }


   class SliderListener implements ChangeListener {
        public void stateChanged(ChangeEvent e) {
            int lag= lagSlider.getValue();
            System.out.println(lag);

            theCanvas.setRadius(lag);
            theCanvas.repaint();
                  display.setText( "The Radius is: " + theCanvas.getRadius() +
                       "\nThe Diameter is: " + theCanvas.getDiameter()
                       + "\nThe Area is: " + theCanvas.getArea() +
                       "\nThe Circumference is: " +
                       theCanvas.getCircumference() );
            app.repaint();
        }
    }
}

class CircleCanvas extends JPanel {
   private int radius;

   public CircleCanvas()
   {
      radius = ( int )( 1 + Math.random() * 100 );
      //setSize( 100, 100 );
   }

   public void paintComponent( Graphics g )
   {  g.drawOval( 0, 0, radius, radius );  }

   public int getDiameter()  {  return ( 2 * radius );  }

   public int getCircumference()
   {  return ( int )( 2 * Math.PI * radius );  }

   public int getArea()
   {  return ( int )( radius * radius * Math.PI );  }

   public int getRadius()  {  return radius;  }

   public void setRadius(int radius){this.radius = radius;}
}
0
 
NaeemgCommented:
I've tried to solve ur problem. try this code now.

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

public class Circle
    extends JFrame
{
  BorderLayout borderLayout1 = new BorderLayout();
  JSlider jSliderRadius = new JSlider();
  JTextArea display = new JTextArea(5, 30);
  private CircleCanvas theCanvas;

  public Circle()
  {
    try
    {
      jbInit();
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception
  {
    this.getContentPane().setLayout(borderLayout1);
    this.setTitle("Circle");
    theCanvas = new CircleCanvas();
    display.setText( "The Radius is: " + theCanvas.getRadius() +
                       "\nThe Diameter is: " + theCanvas.getDiameter()
                       + "\nThe Area is: " + theCanvas.getArea() +
                       "\nThe Circumference is: " +
                       theCanvas.getCircumference() );

    jSliderRadius.setMaximum(200);
    jSliderRadius.setMinimum(0);
    jSliderRadius.setValue(150);
    jSliderRadius.setOrientation(JSlider.HORIZONTAL);
    jSliderRadius.setMajorTickSpacing(10);
    jSliderRadius.setMinorTickSpacing(1);
    jSliderRadius.setPaintLabels(true);
    jSliderRadius.setPaintTicks(true);
    jSliderRadius.setPaintTrack(true);
    jSliderRadius.setToolTipText("Move slider to change the radius.");
    jSliderRadius.addChangeListener(new javax.swing.event.ChangeListener()
    {
      public void stateChanged(ChangeEvent e)
      {
        jSliderRadius_stateChanged(e);
      }
    });

    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.getContentPane().add( theCanvas, BorderLayout.CENTER );
    this.getContentPane().add(jSliderRadius, BorderLayout.NORTH);
    this.getContentPane().add(display, BorderLayout.SOUTH);
    this.setBounds(100,100,500,500);
  }

  void jSliderRadius_stateChanged(ChangeEvent e)
  {
    theCanvas.setRadius(jSliderRadius.getValue());
    theCanvas.updateUI();
    display.setText( "The Radius is: " + theCanvas.getRadius() +
                   "\nThe Diameter is: " + theCanvas.getDiameter()
                   + "\nThe Area is: " + theCanvas.getArea() +
                   "\nThe Circumference is: " +
                   theCanvas.getCircumference() );
  }
 
  //Main method
  public static void main(String[] args)
  {
    try
    {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    Circle thisCircle = new Circle();
    thisCircle.setVisible(true);
  }

}

class CircleCanvas
    extends JPanel
{
  private int radius;

  public CircleCanvas()
  {
    setRadius(50);
  }

  public void setRadius(int radiusSize)
  {
    radius = (int) (radiusSize + Math.random() * 100);
    setSize(5000, 100);
  }

  public void paintComponent(Graphics g)
  {
    g.drawOval(0, 0, radius, radius);
  }

  public int getDiameter()
  {
    return (2 * radius);
  }

  public int getCircumference()
  {
    return (int) (2 * Math.PI * radius);
  }

  public int getArea()
  {
    return (int) (radius * radius * Math.PI);
  }

  public int getRadius()
  {
    return radius;
  }
}
0
 
adidas7fireAuthor Commented:
Ok, after doing slight modifications to this code, I've gotten just about everything to work EXCEPT 1. when I run the application, the NORTH border cuts off the circle and the CENTER border has too much space for the text; 2. on the JSlider, the slider starts on 150 however it is not reflected on the radius in the text (i.e., the slider is defaulted to the value of 150 but if you run the application, it will choose a random radius value in the text.)  Here is the code and if you can offer any help or show me what I am doing wrong, please don't hesitate!  Thank you in advance.

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

public class Circle
    extends JFrame
{
  BorderLayout borderLayout1 = new BorderLayout();
  JSlider jSliderRadius = new JSlider();
  JTextArea display = new JTextArea(5, 30);
  private CircleCanvas theCanvas;

  public Circle()
  {
    try
    {
      jbInit();
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception
  {
    this.getContentPane().setLayout(borderLayout1);
    this.setTitle("Circle");
    theCanvas = new CircleCanvas();
    display.setText( "The Radius is: " + theCanvas.getRadius() +
                       "\nThe Diameter is: " + theCanvas.getDiameter()
                       + "\nThe Area is: " + theCanvas.getArea() +
                       "\nThe Circumference is: " +
                       theCanvas.getCircumference() );

    jSliderRadius.setMaximum(200);
    jSliderRadius.setMinimum(0);
    jSliderRadius.setValue(150);
    jSliderRadius.setOrientation(JSlider.HORIZONTAL);
    jSliderRadius.setMajorTickSpacing(20);
    jSliderRadius.setMinorTickSpacing(10);
    jSliderRadius.setPaintLabels(true);
    jSliderRadius.setPaintTicks(true);
    jSliderRadius.setPaintTrack(true);
    jSliderRadius.setToolTipText("Move slider to change the radius.");
    jSliderRadius.addChangeListener(new javax.swing.event.ChangeListener()
    {
      public void stateChanged(ChangeEvent e)
      {
        jSliderRadius_stateChanged(e);
      }
    });

    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.getContentPane().add( theCanvas, BorderLayout.NORTH );
    this.getContentPane().add(jSliderRadius, BorderLayout.SOUTH);
    this.getContentPane().add(display, BorderLayout.CENTER);
    this.setBounds(100,100,500,300);
  }

  void jSliderRadius_stateChanged(ChangeEvent e)
  {
    theCanvas.setRadius(jSliderRadius.getValue());
    theCanvas.updateUI();
    display.setText( "The Radius is: " + theCanvas.getRadius() +
                   "\nThe Diameter is: " + theCanvas.getDiameter()
                   + "\nThe Area is: " + theCanvas.getArea() +
                   "\nThe Circumference is: " +
                   theCanvas.getCircumference() );
  }
 
  //Main method
  public static void main(String[] args)
  {
    try
    {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    Circle thisCircle = new Circle();
    thisCircle.setVisible(true);
  }

}

class CircleCanvas
    extends JPanel
{
  private int radius;

  public CircleCanvas()
  {
    setRadius(50);
  }

  public void setRadius(int radiusSize)
  {
    radius = (int) (radiusSize + Math.random() * 100);
    setSize(5000, 100);
  }

  public void paintComponent(Graphics g)
  {
    g.drawOval(0, 0, radius, radius);
  }

  public int getDiameter()
  {
    return (2 * radius);
  }

  public int getCircumference()
  {
    return (int) (2 * Math.PI * radius);
  }

  public int getArea()
  {
    return (int) (radius * radius * Math.PI);
  }

  public int getRadius()
  {
    return radius;
  }
}
0
 
expertmbCommented:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class Circle3
    extends JFrame
{
  BorderLayout borderLayout1 = new BorderLayout();
  JSlider jSliderRadius = new JSlider();
  JTextArea display = new JTextArea(5, 30);
  private CircleCanvas theCanvas;
      //this panel ADDED
  private JPanel panel = new JPanel();
  public Circle3()
  {
    try
    {
      jbInit();
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception
  {
    this.getContentPane().setLayout(borderLayout1);
    this.setTitle("Circle");
    theCanvas = new CircleCanvas();
    display.setText( "The Radius is: " + theCanvas.getRadius() +
                       "\nThe Diameter is: " + theCanvas.getDiameter()
                       + "\nThe Area is: " + theCanvas.getArea() +
                       "\nThe Circumference is: " +
                       theCanvas.getCircumference() );

    jSliderRadius.setMaximum(200);
    jSliderRadius.setMinimum(0);
    jSliderRadius.setValue(150);
    jSliderRadius.setOrientation(JSlider.HORIZONTAL);
    jSliderRadius.setMajorTickSpacing(20);
    jSliderRadius.setMinorTickSpacing(10);
    jSliderRadius.setPaintLabels(true);
    jSliderRadius.setPaintTicks(true);
    jSliderRadius.setPaintTrack(true);
    jSliderRadius.setToolTipText("Move slider to change the radius.");
    jSliderRadius.addChangeListener(new javax.swing.event.ChangeListener()
    {
      public void stateChanged(ChangeEvent e)
      {
        jSliderRadius_stateChanged(e);
      }
    });

    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    //this.getContentPane().add( theCanvas, BorderLayout.NORTH );
    //this.getContentPane().add(jSliderRadius, BorderLayout.SOUTH);
    //this.getContentPane().add(display, BorderLayout.CENTER);
    panel.setLayout(new GridLayout(3, 1));

      panel.add(theCanvas);
      panel.add(display);
    panel.add(jSliderRadius);
this.getContentPane().add(panel, BorderLayout.CENTER);
    this.setBounds(100,100,500,300);
  }

  void jSliderRadius_stateChanged(ChangeEvent e)
  {
    theCanvas.setRadius(jSliderRadius.getValue());
    theCanvas.updateUI();
    display.setText( "The Radius is: " + theCanvas.getRadius() +
                   "\nThe Diameter is: " + theCanvas.getDiameter()
                   + "\nThe Area is: " + theCanvas.getArea() +
                   "\nThe Circumference is: " +
                   theCanvas.getCircumference() );
  }

  //Main method
  public static void main(String[] args)
  {
    try
    {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    Circle3 thisCircle = new Circle3();
    thisCircle.setVisible(true);
  }

}

class CircleCanvas
    extends JPanel
{
  private int radius;

  public CircleCanvas()
  {
    setRadius(50);
  }

  public void setRadius(int radiusSize)
  {
    radius = (int) (radiusSize + Math.random() * 100);
    setSize(5000, 100);
  }

  public void paintComponent(Graphics g)
  {
    g.drawOval(0, 0, radius, radius);
  }

  public int getDiameter()
  {
    return (2 * radius);
  }

  public int getCircumference()
  {
    return (int) (2 * Math.PI * radius);
  }

  public int getArea()
  {
    return (int) (radius * radius * Math.PI);
  }

  public int getRadius()
  {
    return radius;
  }
}
0
 
adidas7fireAuthor Commented:
Expertmb,

Thanks for looking the code over.  I was comparing the previous code and the new code and yes, more room was added to the NORTH layout and less in the CENTER layout, but if I use the JSlider to enlarge the circle, it gets cut off.  I can't figure out why the JSlider may be set to 150 however it is not reflected on the text radius as being 150 as well... it's weird how I will move the slider to say, 170, but the result in the text area is something totally different.  Eventually, I plan to have it look like this picture I found on the net... http://www.asiamarie.com/circle1.gif
Thank you again for all your time and assistance on this.
0
 
expertmbCommented:
put below statements in jbinit after the

jSliderRadius.setToolTipText("Move slider to change the radius."); this statement.

theCanvas.setRadius(jSliderRadius.getValue());
  theCanvas.updateUI();
    display.setText( "The Radius is: " + theCanvas.getRadius() +
                       "\nThe Diameter is: " + theCanvas.getDiameter()
                       + "\nThe Area is: " + theCanvas.getArea() +
                       "\nThe Circumference is: " +
                       theCanvas.getCircumference() );



  public void setRadius(int radiusSize)
  {
    radius = radiusSize;//(int) (radiusSize + Math.random() * 100);
    setSize(5000, 100);
  }
0
 
expertmbCommented:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class Circle3
    extends JFrame
{
  BorderLayout borderLayout1 = new BorderLayout();
  JSlider jSliderRadius = new JSlider();
  JTextArea display = new JTextArea(5, 30);
  private CircleCanvas theCanvas;
      //this panel ADDED
  private JPanel panel = new JPanel();
  public Circle3()
  {
    try
    {
      jbInit();
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception
  {
    this.getContentPane().setLayout(borderLayout1);
    this.setTitle("Circle");
    theCanvas = new CircleCanvas();

    jSliderRadius.setMaximum(100);
    jSliderRadius.setMinimum(0);
    jSliderRadius.setValue(75);
    jSliderRadius.setOrientation(JSlider.HORIZONTAL);
    jSliderRadius.setMajorTickSpacing(10);
    jSliderRadius.setMinorTickSpacing(1);
    jSliderRadius.setPaintLabels(true);
    jSliderRadius.setPaintTicks(true);
    jSliderRadius.setPaintTrack(true);
    jSliderRadius.setToolTipText("Move slider to change the radius.");


    theCanvas.setRadius(jSliderRadius.getValue());
    theCanvas.updateUI();
    display.setText( "The Radius is: " + theCanvas.getRadius() +
                       "\nThe Diameter is: " + theCanvas.getDiameter()
                       + "\nThe Area is: " + theCanvas.getArea() +
                       "\nThe Circumference is: " +
                       theCanvas.getCircumference() );


    jSliderRadius.addChangeListener(new javax.swing.event.ChangeListener()
    {
      public void stateChanged(ChangeEvent e)
      {
        jSliderRadius_stateChanged(e);
      }
    });

    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    //this.getContentPane().add( theCanvas, BorderLayout.NORTH );
    //this.getContentPane().add(jSliderRadius, BorderLayout.SOUTH);
    //this.getContentPane().add(display, BorderLayout.CENTER);

//    panel.setLayout(new GridLayout(3, 1));

      GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();

      panel.setLayout(gridbag);
      c.fill = GridBagConstraints.BOTH;
      c.weightx = 1.0;               //reset to the default
      c.gridwidth = 1;                  //reset to the default
      c.gridheight = 2;
      c.weighty = 1.0;
      c.gridwidth = GridBagConstraints.REMAINDER;

      gridbag.setConstraints(theCanvas, c);
      panel.add(theCanvas);

      c = new GridBagConstraints();
      c.fill = GridBagConstraints.BOTH;
      c.weightx = 0.0;               //reset to the default
      c.gridwidth = 1;                  //reset to the default
      c.gridheight = 1;
      c.gridwidth = GridBagConstraints.REMAINDER;
      gridbag.setConstraints(display, c);
      panel.add(display);

      c = new GridBagConstraints();
      c.fill = GridBagConstraints.BOTH;
      c.weightx = 0.0;               //reset to the default
      c.gridwidth = 1;                  //reset to the default
      c.gridheight = 1;
      c.gridwidth = GridBagConstraints.REMAINDER;
    gridbag.setConstraints(jSliderRadius, c);
    panel.add(jSliderRadius);
    this.getContentPane().add(panel, BorderLayout.CENTER);
    this.setBounds(100,100,400,500);
  }

  void jSliderRadius_stateChanged(ChangeEvent e)
  {

    theCanvas.setRadius(jSliderRadius.getValue());
    theCanvas.updateUI();
    display.setText( "The Radius is: " + theCanvas.getRadius() +
                   "\nThe Diameter is: " + theCanvas.getDiameter()
                   + "\nThe Area is: " + theCanvas.getArea() +
                   "\nThe Circumference is: " +
                   theCanvas.getCircumference() );
  }

  //Main method
  public static void main(String[] args)
  {
    try
    {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    Circle3 thisCircle = new Circle3();
    thisCircle.setVisible(true);
  }

}

class CircleCanvas
    extends JPanel
{
  private int radius = 0;

  public CircleCanvas()
  {
    //setRadius(50);
  }

  public void setRadius(int radiusSize)
  {
    radius = ((getWidth()-50)*radiusSize)/100;//(int) (radiusSize + Math.random() * 100);
    setSize(getWidth(), getHeight());
  }

  public void paintComponent(Graphics g)
  {

    g.drawOval(0, 0, radius, radius);
  }

  public int getDiameter()
  {
    return (2 * radius);
  }

  public int getCircumference()
  {
    return (int) (2 * Math.PI * radius);
  }

  public int getArea()
  {
    return (int) (radius * radius * Math.PI);
  }

  public int getRadius()
  {
    return radius;
  }
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now