简体   繁体   English

组合框选择

[英]ComboBox selection

I have a graph showing the lines in 3 colours (red, green, blue), like screenshot. 我有一个以3种颜色(红色,绿色,蓝色)显示线条的图形,如屏幕截图。

When I press one of the colours, ie red, I want to show graph with only red colour. 当我按一种颜色(即红色)时,我只想显示红色的图形。 And if I press green then only green. 如果我按绿色,则只有绿色。 But if I press nothing then it should show all. 但是,如果我什么都不按下,则应该全部显示。

Below my code for creating and drawing the lines. 在我的代码下方,用于创建和绘制线条。

import java.awt.Color;
import java.io.FileInputStream;

import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.SwingConstants;

import java.io.*; 

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

class LineGraph extends ApplicationFrame {



       private JFreeChart createChart(final XYDataset dataset) {

            // Graph wird erstellt...

       final JFreeChart chart = ChartFactory.createXYLineChart("","X","Y",dataset,PlotOrientation.VERTICAL,true,false,false);


            final XYPlot plot = chart.getXYPlot();

            plot.setBackgroundPaint(Color.lightGray);
            plot.setDomainGridlinePaint(Color.white);
            plot.setRangeGridlinePaint(Color.white);

            NumberAxis xAxis = (NumberAxis) plot.getRangeAxis();
            xAxis.setRange (0, 7500);

            NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
            yAxis.setRange(0, 270);


            return chart;


          }   





    public LineGraph(final String title) {

        super(title);
    }

   public ChartPanel createPanel(final String title){

       final XYDataset dataset = createDataset();
       final JFreeChart chart = createChart(dataset);
       ChartPanel chartPanel = new ChartPanel(chart);
       chartPanel.setPreferredSize(new java.awt.Dimension(950, 570));
       //chartPanel.setSize(50, 50);
       //setContentPane(chartPanel);

       return chartPanel;
   }

   private XYSeriesCollection createDataset() {

       try {

       final int pictureWidth = 7500;
       final int pictureHeight = 5;
       int pictureArray[] = new int[pictureWidth * pictureHeight];
       int pixArray[] = new int[pictureWidth];
       int R, G, B, U;
       int averageX, averageY;

      FileInputStream inp = new FileInputStream("d:\\DO_18-02-2014_19-59-05-756_00010.img");

       int k = 0;

       XYSeries RED = new XYSeries("R");   
       XYSeries GREEN = new XYSeries("G");  
       XYSeries BLUE = new XYSeries("B");
       XYSeries  UNDEFINED = new XYSeries("U");


       for (int j = 0; j < pictureHeight; j++) 
        {for (int i = 0; i < pictureWidth; i++) 
           {   R = inp.read();
               G = inp.read();
               B = inp.read();
               U = inp.read();
               Color c = new Color(R, G, B, U);
               pictureArray[k++] = c.getRGB();

               System.out.println((new Integer(i)).toString()+" "+(new Integer(j)).toString());

               RED.add(i, R);
               GREEN.add(i, B);
               BLUE.add(i, G);
               UNDEFINED.add(i, U);

           }
       }
       inp.close();

       final XYSeriesCollection dataset = new XYSeriesCollection();
       dataset.addSeries(RED);
       dataset.addSeries(GREEN);
       dataset.addSeries(BLUE);
       dataset.addSeries(UNDEFINED);

       return dataset;
       }
       catch (Exception e) {
           JOptionPane.showMessageDialog(null, e, "Exception Raised",
                   JOptionPane.INFORMATION_MESSAGE);
           return null;
       }

   }   



    }

And below the code for the combobox. 和下面的组合框的代码。

![//Create ComboBox for RGB Choice
String ChannelStrings\[\] = { "All", "Red", "Green", "Blue"};
JComboBox ChannelChoice = new JComboBox(ChannelStrings);
ChannelChoice.setFont((new Font ("Arial", Font.BOLD, 13)));
panelButton.add(ChannelChoice);][1]

As shown here , you can control the visibility of individual series using the renderer's setSeriesVisible() method. 如图所示在这里 ,你可以控制使用渲染器的各个系列的知名度setSeriesVisible()方法。 The example cited uses checkboxes for control, but your combo's action listener can invoke the same method. 引用的示例使用复选框进行控制,但是组合的动作侦听器可以调用相同的方法。 ChartFactory.createXYLineChart() uses an instance of XYLineAndShapeRenderer ; ChartFactory.createXYLineChart()使用XYLineAndShapeRenderer的实例; you can get a reference from the chart's plot, as shown here . 你可以从图表的绘图参考,如图所示这里

图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM