Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Jslider

  • 12-03-2007 10:23pm
    #1
    Registered Users, Registered Users 2 Posts: 206 ✭✭


    Hi,

    I am trying to create a project where i have a resizable cirsle using JSlider and where the color of the circle can be changed using three other JSliders, one for each colour.

    I can create the resizable circle using an OvalPanel and add the other three sliders but they don't do anything!

    Does anyone have any suggestions on how I could get this program to work.

    Thanks,

    Here is the code:

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

    public class SliderDemo extends JFrame{
    private JSlider diameterSlider;
    private JSlider redSlider;
    private JSlider blueSlider;
    private JSlider greenSlider;
    private OvalPanel myPanel;

    ColorPanel canvas = new ColorPanel();

    //set up GUI
    public SliderDemo()
    {
    super("Slider Demo");


    //set up OvalPanel
    myPanel=new OvalPanel();
    myPanel.setBackground(Color.WHITE);

    //set up JSlider to control diameter value
    diameterSlider =
    new JSlider(SwingConstants.VERTICAL, 0,200,10);
    diameterSlider.setMajorTickSpacing(10);
    diameterSlider.setPaintTicks(true);

    //set up sliders for colours
    //red slider
    redSlider =
    new JSlider(SwingConstants.VERTICAL, 0,200,10);
    diameterSlider.setMajorTickSpacing(10);
    diameterSlider.setPaintTicks(true);

    //blue
    blueSlider =
    new JSlider(SwingConstants.VERTICAL, 0,200,10);
    diameterSlider.setMajorTickSpacing(10);
    diameterSlider.setPaintTicks(true);

    //green
    greenSlider =
    new JSlider(SwingConstants.VERTICAL, 0,200,10);
    diameterSlider.setMajorTickSpacing(10);
    diameterSlider.setPaintTicks(true);



    //register JSlider event listeners
    diameterSlider.addChangeListener(

    new ChangeListener() { //anonymous inner class

    //handle change in slider value
    public void stateChanged(ChangeEvent e)
    {
    myPanel.setDiameter(diameterSlider.getValue() );
    }

    }//end anonymous inner class

    );//end call to change listener
    diameterSlider.addChangeListener(

    new ChangeListener() {
    public void stateChanged(ChangeEvent evt) {
    JSlider source = (JSlider)evt.getSource();
    if (source.getValueIsAdjusting() != true) {
    Color current = new Color(red.getValue(), green.getValue(),
    blue.getValue());
    myPanel.changeColor(current);
    myPanel.repaint();
    }
    }

    //attach components to content pane
    // Container container = getContentPane();
    // container.add(diameterSlider, BorderLayout.WEST);
    //OvalPanel.add(redSlider, BorderLayout.WEST);
    //OvalPanel.add(blueSlider, BorderLayout.WEST);
    //OvalPanel.add(greenSlider, BorderLayout.WEST);
    //OvalPanel.add(myPanel,BorderLayout.EAST);


    // setSize(650,450);
    // setVisible(true);
    }//end constructor SliderDemo

    public static void main (String args[])
    {
    SliderDemo application = new SliderDemo();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    }//end class SliderDemo


Advertisement