gpt4 book ai didi

java - jFreechart点点击时消失

转载 作者:行者123 更新时间:2023-11-30 07:25:41 24 4
gpt4 key购买 nike

我有一个 Java 6/Swing 应用程序(是的,我知道,但这是要求),我需要显示一些极坐标图。到目前为止,一切都运行良好,但是当我单击绘图时,绘制的点就会消失,即使我移动周围的窗口也可能会消失。我使用 netbeans,这是一个导致相同问题的虚拟代码

使用的库是

  • jfreechart-1.0.19.jar
  • jcommon-1.0.23.jar

NewJFrame.java

import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;


public class NewJFrame extends javax.swing.JFrame {


private PlotHandler plotHandler;

public NewJFrame() {
initComponents();
plotHandler = new PlotHandler(jPanel1, "blub", 1);
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 504, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 409, Short.MAX_VALUE)
);

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(36, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(217, 217, 217))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1)
.addContainerGap())
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
XYSeriesCollection data;
XYSeries series;//("Bearing"+getBearingIndex(i));
data = new XYSeriesCollection();
series = new XYSeries("Bearings");
series.add(Math.random()*180/Math.PI,
Math.random());
data.addSeries(series);
plotHandler.plotData(data);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}

PlotHandler.java

import java.awt.BorderLayout;
import javax.swing.JPanel;
import org.jfree.data.xy.XYSeriesCollection;

public class PlotHandler {
private final JPanel jPanel;
private final String plane;
private double range;


public PlotHandler(JPanel jPanel, String plane, double range) {
this.jPanel = jPanel;
this.plane = plane;
this.range = range;

// initialize plot
this.jPanel.setLayout(new java.awt.BorderLayout());
this.jPanel.add(PolarPlotHandler.emptyPolarChart(range), BorderLayout.CENTER);
this.jPanel.validate();
}

public void plotData(XYSeriesCollection data) {
jPanel.setLayout(new java.awt.BorderLayout());

jPanel.add(PolarPlotHandler.createPolarChart(data, range), BorderLayout.CENTER);
jPanel.validate();
}
}

PolarPlotHandler.java

import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.PolarChartPanel;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PolarPlot;
import org.jfree.chart.renderer.DefaultPolarItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeriesCollection;

public final class PolarPlotHandler {

private PolarPlotHandler() { }

public static ChartPanel createPolarChart(XYSeriesCollection data, double range) {
final XYDataset dataset = data;
range = 200;
range = Math.ceil(range/10)*10;
range = Math.max(1, range);
final JFreeChart chart = createChart(dataset, range/1000000);
if(data.getSeriesCount()<=1 && data.getSeries(0).getItemCount()>0)
chart.removeLegend();
else {

}
ChartPanel chartPanel = new PolarChartPanel(chart);
chartPanel.setDomainZoomable(false);
chartPanel.setRangeZoomable(false);

return chartPanel;
}
private static JFreeChart createChart(final XYDataset dataset, double range) {
final JFreeChart chart = ChartFactory.createPolarChart(
"", dataset, true, true, false
);
final PolarPlot plot = (PolarPlot) chart.getPlot();
// get Axis
plot.setAngleOffset(0);
plot.setCounterClockwise(true);
plot.getAxis().setTickLabelsVisible(false);
plot.setAngleTickUnit(new NumberTickUnit(45));
((NumberAxis)plot.getAxis()).setTickUnit(new NumberTickUnit(range));
// find the maximum range
plot.getAxis().setRange(0, 2.0);
plot.getAxis().setVisible(true);
plot.setBackgroundAlpha(0.0f);
plot.setAngleGridlinePaint(Color.black);
plot.setRadiusGridlinePaint(Color.black);
final DefaultPolarItemRenderer renderer = (DefaultPolarItemRenderer) plot.getRenderer();
renderer.setSeriesFilled(2, false);
return chart;
}
public static ChartPanel emptyPolarChart() {
return emptyPolarChart(200.0);
}

public static ChartPanel emptyPolarChart(double range) {
final JFreeChart chart = createChart(new XYSeriesCollection(), range/1000000);
chart.removeLegend();
ChartPanel chartPanel = new PolarChartPanel(chart);
return chartPanel;
}
}

最佳答案

您似乎正在尝试通过替换整个封闭的 ChartPanel 来更新 PolarPlot。相反,更新数据集的系列,收听图表面板将自行更新作为响应。在下面的示例中,按钮的处理程序会清除数据集的 XYSeries 并在每次单击按钮时添加一个新的随机点。

@Override
public void actionPerformed(ActionEvent e) {
series.clear();
series.add(r.nextDouble() * 360, r.nextDouble());
}

image

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PolarPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/** @see http://stackoverflow.com/a/36846122/230513 */
public class Test {

private final XYSeries series = new XYSeries("Series");
private final XYDataset dataset = new XYSeriesCollection(series);

public Test() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFreeChart chart = ChartFactory.createPolarChart(
"Title", dataset, true, true, false);
final PolarPlot plot = (PolarPlot) chart.getPlot();
plot.setBackgroundPaint(Color.white);
plot.getAxis().setRange(0, 1);
plot.getAxis().setTickLabelsVisible(false);
plot.setAngleGridlinePaint(Color.black);
plot.setRadiusGridlinePaint(Color.black);
plot.setRadiusMinorGridlinesVisible(false);
ChartPanel chartPanel = new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
};

JButton button = new JButton("Button");
button.addActionListener(new ActionListener() {
Random r = new Random();

@Override
public void actionPerformed(ActionEvent e) {
series.clear();
series.add(r.nextDouble() * 360, r.nextDouble());
}
});
JPanel control = new JPanel(new FlowLayout(FlowLayout.RIGHT));
control.add(button);

f.add(chartPanel, BorderLayout.CENTER);
f.add(control, BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
Test t = new Test();
}
});
}
}

关于java - jFreechart点点击时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36838545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com