import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.experimental.chart.plot.dial.DialBackground;
import org.jfree.experimental.chart.plot.dial.DialCap;
import org.jfree.experimental.chart.plot.dial.DialPlot;
import org.jfree.experimental.chart.plot.dial.DialTextAnnotation;
import org.jfree.experimental.chart.plot.dial.DialValueIndicator;
import org.jfree.experimental.chart.plot.dial.SimpleDialFrame;
import org.jfree.experimental.chart.plot.dial.StandardDialRange;
import org.jfree.experimental.chart.plot.dial.StandardDialScale;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.StandardGradientPaintTransformer;
public class DialApplet extends JApplet implements ChangeListener {
/**
*
*/
private static final long serialVersionUID = 6436654984397289399L;
private ChartPanel chartPanel = null;
private JFreeChart jfreechart = null;
private JPanel jpanel;
private JSlider slider;
private DefaultValueDataset dataset;
public void init() {
double startVal = Double.parseDouble(this.getParameter("start_value"));
double endVal = Double.parseDouble(this.getParameter("end_value"));
Container c = getContentPane();
jpanel = new JPanel();
c.setLayout(new BorderLayout());
//指针值
dataset = new DefaultValueDataset(Double.parseDouble(this.getParameter("point_value")));
DialPlot dialplot = new DialPlot();
dialplot.setView(0.0D, 0.0D, 1.0D, 1.0D);
dialplot.setDataset(dataset);
SimpleDialFrame. simpledialframe. = new SimpleDialFrame();
simpledialframe.setForegroundPaint(getColor(this.getParameter("line_f_color"),Color.darkGray));
simpledialframe.setBackgroundPaint(getColor(this.getParameter("line_bg_color"),Color.lightGray));
dialplot.setDialFrame(simpledialframe);
GradientPaint gradientpaint = new GradientPaint(new Point(),
getColor(this.getParameter("f_color"),new Color(255, 255, 255)),
new Point(),
getColor(this.getParameter("bg_color"),new Color(170,170,220)));
DialBackground dialbackground = new DialBackground(gradientpaint);
dialbackground.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
dialplot.setBackground(dialbackground);
//设置显示在表盘值标题
String valueTitle = this.getParameter("val_title");
DialTextAnnotation dialtextannotation = new DialTextAnnotation(valueTitle);
dialtextannotation.setFont(new Font("Dialog", 1, 14));
dialtextannotation.setRadius(0.69999999999999996D);
dialplot.addLayer(dialtextannotation);
DialValueIndicator dialvalueindicator = new DialValueIndicator(0, "c");
dialplot.addLayer(dialvalueindicator);
StandardDialScale standarddialscale = new StandardDialScale(startVal, endVal, -120D, -300D);
standarddialscale.setMajorTickIncrement(Integer.parseInt(this.getParameter("step_length")));
standarddialscale.setTickRadius(0.88D);
standarddialscale.setTickLabelOffset(0.14999999999999999D);
standarddialscale.setTickLabelFont(new Font("Dialog", 0, 14));
dialplot.addScale(0, standarddialscale);
//以下是默认分为三段类型值
double valueLine = (endVal - startVal) / 3.0;
StandardDialRange standarddialrange = new StandardDialRange(startVal + valueLine + valueLine, endVal, Color.red);
standarddialrange.setInnerRadius(0.52000000000000002D);
standarddialrange.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange);
StandardDialRange standarddialrange1 = new StandardDialRange(startVal + valueLine, startVal + valueLine + valueLine, Color.orange);
standarddialrange1.setInnerRadius(0.52000000000000002D);
standarddialrange1.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange1);
StandardDialRange standarddialrange2 = new StandardDialRange(startVal, startVal + valueLine, Color.green);
standarddialrange2.setInnerRadius(0.52000000000000002D);
standarddialrange2.setOuterRadius(0.55000000000000004D);
dialplot.addLayer(standarddialrange2);
org.jfree.experimental.chart.plot.dial.DialPointer.Pointer pointer = new org.jfree.experimental.chart.plot.dial.DialPointer.Pointer();
dialplot.addLayer(pointer);
DialCap dialcap = new DialCap();
dialcap.setRadius(0.10000000000000001D);
dialplot.setCap(dialcap);
//通过参数设置仪表盘标题
String title = this.getParameter("chart_title");
jfreechart = new JFreeChart(dialplot);
//new Color(218,242,243)
//仪表盘方框的颜色
jfreechart.setBackgroundPaint(new Color(218,242,243));
jfreechart.setTitle(title);
chartPanel = new ChartPanel(jfreechart);
// chartPanel.setBackground(Color.blue);
chartPanel.setPreferredSize(new Dimension(130, 130));
slider = new JSlider((int)startVal,(int)endVal);
slider.setMajorTickSpacing(Integer.parseInt(this.getParameter("slider_step_length")));
slider.setPaintLabels(true);
slider.addChangeListener(this);
add(chartPanel);
add(slider, "South");
}
public void paint(Graphics g ) {
chartPanel.repaint();
jpanel.repaint();
slider.repaint();
}
public void stateChanged(ChangeEvent arg0) {
dataset.setValue(new Integer(slider.getValue()));
}
public double getVal(){
return dataset.getValue().doubleValue();
}
@Override
public void repaint() {
chartPanel.repaint();
jpanel.repaint();
slider.repaint();
}
private Color getColor(String colorStr,Color defaultColor){
Color color = defaultColor;
try {
colorStr = colorStr.trim();
if(colorStr.length() > 0){
colorStr = colorStr.substring(1);
}
color = new Color(Integer.parseInt(colorStr,16));
} catch (NumberFormatException e) {
e.printStackTrace();
}
return color;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
chartPanel = null;
jfreechart = null;
jpanel = null;
slider = null;
dataset = null;
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/14701951/viewspace-711614/
,如需转载,请注明出处,否则将追究法律责任。