[英]Graph plotting in Java Swing only draws points
我目前正在開發一個程序,其中某些隨時間演變的數值變量在每次迭代時都會顯示其值。 這很好用,但現在我想繪制一張圖表,顯示它們隨時間的演變。 所以,我查看了一個用於在Swing中繪制圖形的代碼示例。 我的最終代碼如下所示:
public class Populus3 extends JPanel
{
public static void main(String[] args) throws IOException {
final Populus3 pop = new Populus3();
JFrame f = new JFrame(); //where I want to plot the graph
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new GraphingData());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
frame = new JFrame("Animation Frame"); //where I'm running animation for another element of the program
frame.add(pop, BorderLayout.CENTER);
frame.setSize(graphSize, graphSize);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//insert all sort of things
}
public void paint(Graphics g)
{
super.paint(g);
paintCell(g, 1);
Toolkit.getDefaultToolkit().sync(); // necessary for linux users to draw and animate image correctly
g.dispose();
}
public void actionPerformed(ActionEvent e) {
repaint();
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i = 0; i < particleType.length; i++)
paintCell(g, i); //a method that draws a small circle for the animation panel
}
public static class GraphingData extends JPanel {
int[] data = {
21, 14, 18, 03, 86, 88, 74, 87, 54, 77,
61, 55, 48, 60, 49, 36, 38, 27, 20, 18
};
final int PAD = 20;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// Draw ordinate.
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
// Draw abcissa.
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
double xInc = (double)(w - 2*PAD)/(data.length-1);
double scale = (double)(h - 2*PAD)/getMax();
// Mark data points.
g2.setPaint(Color.red);
for(int i = 0; i < data.length; i++) {
double x = PAD + i*xInc;
double y = h - PAD - scale*data[i];
g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
}
}
private int getMax() {
int max = -Integer.MAX_VALUE;
for(int i = 0; i < data.length; i++) {
if(data[i] > max)
max = data[i];
}
return max;
}
}
}
現在,動畫面板工作得很好。 另一方面,圖形面板......當我運行程序時,它會顯示一堆紅點,沒有連接它們的線條。 我究竟做錯了什么?
除了@ Hovercraft的有用建議,還要考慮以下其他方法:
GeneralPath
可以呈現為需要的, 例如 。 drawLine()
使用合適的坐標系,概括這里 。 JFreeChart
。 你的代碼讓我困惑:
paintCell(...)
。 Toolkit.getDefaultToolkit().sync();
用於Swing應用程序。 你有這個需求的參考嗎? 考慮更多地隔離你的問題並發布一個sscce ,一個我們可以編譯,運行,修改和糾正的最小測試程序,它向我們展示你的問題,但是沒有與問題無關或演示所需的額外代碼。
以下代碼演示了使用XChart的實時Java圖表,其中隨着數據隨時間的推移而更新行。 創建實時圖表就像通過XYChart
實例調用一個或多個系列對象的updateXYSeries
並觸發包含圖表的JPanel
的重繪一樣簡單。 這適用於所有的圖表類型,包括XYChart
, CategoryChart
, BubbleChart
和PieChart
,為此示例源代碼可以在這里找到: https://github.com/timmolter/XChart/tree/develop/xchart-demo/src/main/java / org / knowm / xchart / demo / charts / realtime 。 示例演示了SwingWrapper
將SwingWrapper
與repaintChart()
方法一起使用,以及將XChartPanel
與revalidate()
和repaint()
。 免責聲明,我是XChart庫的主要開發人員。
public class SimpleRealTime {
public static void main(String[] args) throws Exception {
double phase = 0;
double[][] initdata = getSineData(phase);
// Create Chart
final XYChart chart = QuickChart.getChart("Simple XChart Real-time Demo", "Radians", "Sine", "sine", initdata[0], initdata[1]);
// Show it
final SwingWrapper<XYChart> sw = new SwingWrapper<XYChart>(chart);
sw.displayChart();
while (true) {
phase += 2 * Math.PI * 2 / 20.0;
Thread.sleep(100);
final double[][] data = getSineData(phase);
chart.updateXYSeries("sine", data[0], data[1], null);
sw.repaintChart();
}
}
private static double[][] getSineData(double phase) {
double[] xData = new double[100];
double[] yData = new double[100];
for (int i = 0; i < xData.length; i++) {
double radians = phase + (2 * Math.PI / xData.length * i);
xData[i] = radians;
yData[i] = Math.sin(radians);
}
return new double[][] { xData, yData };
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.