0

This is my DialPlot class:

public class Demo {

    private  DefaultValueDataset dataset = new DefaultValueDataset(0);

    private ChartPanel buildDialPlot(int minimumValue=0,int maximumValue,int majorTickGap) {

        DialPlot plot = new DialPlot(dataset);
        plot.setDialFrame(new StandardDialFrame());
        plot.addLayer(new DialValueIndicator(0));
        plot.addLayer(new DialPointer.Pointer());
        int redLine =maximumValue / 5 + 10;
        plot.addLayer(new StandardDialRange(maximumValue, redLine,            Color.blue));
        plot.addLayer(new StandardDialRange(redLine, minimumValue, Color.red));

        StandardDialScale scale = new StandardDialScale(minimumValue,
        maximumValue, -120, -300, majorTickGap, majorTickGap - 1);
        scale.setTickRadius(0.88);
        scale.setTickLabelOffset(0.20);
        scale.setTickLabelsVisible(false);
        plot.addScale(0, scale);

        return new ChartPanel(new JFreeChart(plot)) {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
          }
       };
    }
}

This is my JFrame GUI class

public class NewJFrame extends javax.swing.JFrame {
    Demo d=new Demo();
    int minimumValue=0;
    int maximumValue=100;
    int majorTickGap=1;

    public NewJFrame() {
    initComponents();
    d.buildDialPlot(minimumValue,maximumValue,majorTickGap);
    this.pack();
    this.setLocationRelativeTo(null);
    }

I also added it in JFrame GUI class like this:

this.add(d.buildDialPlot(minimumValue,maximumValue,majorTickGap));

It shows the same result empty JFrame. Anyone knows what's my mistake?

The image below shows what my example code currently produces

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Hamza Shahid
  • 19
  • 1
  • 6

1 Answers1

2

Art a minimum, you need to add() the ChartPanel returned by buildDialPlot() to the JFrame. A complete example is shown here.

public NewJFrame() {
    initComponents();
    ChartPanel cp = d.buildDialPlot(minimumValue, maximumValue, majorTickGap);
    this.add(cp);
    this.pack();
    this.setLocationRelativeTo(null);
}

Addendum: Using the approach suggested here, the example below adds your Demo to a locally declared JFrame.

image

As tested:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.DialValueIndicator;
import org.jfree.chart.plot.dial.StandardDialFrame;
import org.jfree.chart.plot.dial.StandardDialRange;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.data.general.DefaultValueDataset;

/**
 * @see https://stackoverflow.com/a/38906326/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Demo d = new Demo();
        ChartPanel cp = d.buildDialPlot(0, 100, 10);
        f.add(cp);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static class Demo {

        private DefaultValueDataset dataset = new DefaultValueDataset(0);

        private ChartPanel buildDialPlot(int minimumValue, int maximumValue, int majorTickGap) {

            DialPlot plot = new DialPlot(dataset);
            plot.setDialFrame(new StandardDialFrame());
            plot.addLayer(new DialValueIndicator(0));
            plot.addLayer(new DialPointer.Pointer());
            int redLine = maximumValue / 5 + 10;
            plot.addLayer(new StandardDialRange(maximumValue, redLine, Color.blue));
            plot.addLayer(new StandardDialRange(redLine, minimumValue, Color.red));

            StandardDialScale scale = new StandardDialScale(minimumValue,
                maximumValue, -120, -300, majorTickGap, majorTickGap - 1);
            scale.setTickRadius(0.88);
            scale.setTickLabelOffset(0.20);
            scale.setTickLabelsVisible(false);
            plot.addScale(0, scale);

            return new ChartPanel(new JFreeChart(plot)) {

                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(300, 300);
                }
            };
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • can you please guide me how can i do that? – Hamza Shahid Aug 11 '16 at 21:21
  • For more specific guidance, please edit your question to include a [mcve] that shows your revised approach. – trashgod Aug 11 '16 at 21:25
  • it display in local frame but still according to your method not in gui frame. i have to add some more components in but when i appply following code on frame Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); f.setSize(d); the size of dial automatical increase according to frame size that what i dont know – Hamza Shahid Aug 11 '16 at 22:34
  • I suspect the GUI editor is getting in your way; I'd recommend the approach [cited](http://stackoverflow.com/a/2561540/230513) as more scalable. – trashgod Aug 11 '16 at 22:47
  • Thank you so much i just have to set layout of the frame or Panel Thanks again for your time – Hamza Shahid Aug 11 '16 at 23:23