Jeetesh Nataraj
Jeetesh Nataraj

Reputation: 608

Circular JSlider?

A JSlider by default takes a straight line shape. Can we make JSlider a circle resembling like speed-o-meter or circle with reading?

If anyone has done something similar to this task... Please share the code...

Thank You

Upvotes: 2

Views: 1821

Answers (4)

By coincidence I was just needing this for my own App, I passed through your question which didn't really help me and then I got to the point I saw people use Graphics2D.

I really always look at Graphics g as a 100-headed monster that wants to murder my whole family, but I decided to face my fears, got a code that made a triangle, and managed to make a circle out of it.

It is so much more easier than I ever thought:

 public class CustomSlider extends JSlider{

 public CustomSlider(int begin, int end) {
     super(begin, end);
     setFont(new CustomFont(10));
     setPaintTrack(true);
    setUI(new BasicSliderUI(this) { 
        /*Sets the size of the Handler, should be the same as the OVAL size*/
        @Override
        protected Dimension getThumbSize() {
            return new Dimension(25,25);
        }

        /*Painting the THUMB as a OVAL in the x and y positions of thumbRect (The Handle)*/
        @Override
        public void paintThumb(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            Rectangle t = thumbRect;
            g2d.setColor(Color.BLUE);
            g2d.fillOval(t.x, t.y, 25, 25);
        }
    });
    /* Set JLabels for specific Values*/
    Hashtable<Integer, JLabel> position = new Hashtable<Integer, JLabel>();
    position.put(0, new JLabel("HELP"));
    position.put(1, new JLabel("NORMAL"));
    position.put(2, new JLabel("CHAOS"));
    setLabelTable(position);
    setPaintLabels(true);
}

Result: https://i.sstatic.net/0OiKL.png (Can't add images, lack of reputation :( ) If you prefer the look of a MetalSliderUI, use that one instead of the BasicSliderUI.

Upvotes: 1

Greg S.
Greg S.

Reputation: 48

There's no built in function to do this, as far as I know. However, you can use a class such as this JKnob and add the actual sliding value yourself. (Actually, it has a getValue() method, so just call that and divide by pi to get a value from -1.0 to 1.0)

Upvotes: 0

Catalina Island
Catalina Island

Reputation: 7126

JFreeChart has a DialPlot that might give you some ideas on how to do the rendering.

Upvotes: 2

jzd
jzd

Reputation: 23629

There is not a default way to do this. However if you are creating a custom component, it sounds like you want some sort of dial that could be grabbed and adjusted rather than a slider that does in a circle.

This would be a custom component that would be a medium but fun task to create. The hard part will be making it appear to the user as something that is adjustable rather than just displaying a value. In real life a speedometer is read-only.

Upvotes: 2

Related Questions