package com.jidesoft.chart.support.ortelius;

import com.jidesoft.gauge.*;
import com.jidesoft.icons.JideIconsFactory;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.LinearGradientPaint;
import java.awt.Paint;

public class GaugeWithColorGrading2 extends JPanel {

    private Dial dial;

    public GaugeWithColorGrading2() {
        super(new BorderLayout());
        dial = new Dial();
        DialFrame frame = new DialFrame();
        frame.setFill(Color.darkGray);
        frame.setFrameWidth(0.02f);
        frame.setMidChordRadius(0.15f);
        frame.setArcEndAngle(9f);
        dial.setFrame(frame);
        DialAxis axis = new DialAxis(0, 100, 10, 5);
        axis.setLabelRadius(0.8);
        axis.setInnerRadius(0.96);
        axis.setOuterRadius(1.0);
        axis.setTickLabelFont(UIManager.getFont("Label.font").deriveFont(24f));
        GaugeTickStyle tickStyle = new GaugeTickStyle();
        axis.setMajorTickStyle(tickStyle);
        axis.setStartAngle(180);
        axis.setEndAngle(0);
        dial.setAxis(axis);
        dial.setStartAngle(180);
        dial.setEndAngle(0);
        dial.setBorder(new EmptyBorder(30,30,30,30));
        Color red = new Color(250, 0, 0);
        Color yellow = new Color(255, 255, 0);
        Color green = new Color(0, 250, 0);
        addMarker(-50, 50, red, red, red, 10);
        addMarker(50, 80, red, yellow, yellow, 10);
        addMarker(80, 150, yellow, green, green, 10);
        // Add a pivot
        Pivot pivot = new Pivot(dial, 0.14f, Color.black);
        dial.addDrawable(pivot);
        Paint pivotPaint = new DialRadialPaint(dial, new float[] {0f, 0.09f}, new Color[] {Color.white, Color.darkGray});
        Pivot innerPivot = new Pivot(dial, 0.09f, pivotPaint);
        innerPivot.setZOrder(101);
        dial.addDrawable(innerPivot);
        // Now add a needle
        NeedleStyle needleStyle = new NeedleStyle();
        needleStyle.setHeadLength(0.95);
        needleStyle.setBaseWidth(0.15f);
        needleStyle.setFillPaint(Color.black);
        dial.addNeedle("value", needleStyle);
        dial.setValue("value", 92);
        add(dial, BorderLayout.CENTER);
        // Set the background paint
        dial.setDialBackground(createPaint());
    }

    private void addMarker(double from, double to, Color fromColor, Color midColor, Color toColor, double transitionSize) {
        double fromAngle = dial.getAngle(from);
        double midColorStart = dial.getAngle(from+transitionSize);
        double midColorEnd = dial.getAngle(to-transitionSize);
        double toAngle = dial.getAngle(to);
        Paint paint = new DialConicalPaint(dial,
                new float[] {(float)fromAngle, (float) midColorStart, (float) midColorEnd, (float)toAngle},
                new Color[] {fromColor, midColor, midColor, toColor});
        DialIntervalMarker marker = new DialIntervalMarker(dial, from, to, paint);
        marker.setInnerRadius(0);
        dial.addDrawable(marker);
    }

    private Paint createPaint() {
        return new LinearGradientPaint(0f, 0f, 0f, 300f, new float[] {0f, 1f}, new Color[] {Color.white, Color.gray});
    }

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("GaugeWithColorGrading");
                frame.setIconImage(JideIconsFactory.getImageIcon(JideIconsFactory.JIDE32).getImage());
                frame.setContentPane(new GaugeWithColorGrading2());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(150, 150, 400, 300);
                frame.setVisible(true);
            }
        });
    }
}