Java Swing中的图形绘制仅绘制点

use*_*014 3 java swing graph

我目前正在开发一个程序,其中某些随时间演变的数值变量在每次迭代时都会显示其值.这很好用,但现在我想绘制一张图表,显示它们随时间的演变.所以,我查看了一个用于在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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,动画面板工作得很好.另一方面,图形面板......当我运行程序时,它会显示一堆红点,没有连接它们的线条.我究竟做错了什么?

tra*_*god 6

除了@ Hovercraft的有用建议,还要考虑以下其他方法:

  • 积聚在一个点GeneralPath根据需要可以被呈现为例如.

lissajou图片

  • 用千呼万唤点连接到drawLine()使用合适的坐标系,概括这里.

正弦图像


Hov*_*els 5

你的代码让我困惑:

  • 你为Populus3 JPanel覆盖了paint和paintComponent - 为什么?您应该只覆盖paintComponent,除非您必须让绘图影响组件的子项和边框.
  • 你处理掉传递给paint的Graphics对象 - 这是一件非常危险的事情.您永远不应该处理JVM提供给您的Graphics对象,只能处理您自己创建的Graphics对象.
  • 你为我们反复调用一个这里没有定义的方法paintCell(...).
  • 我从未听说过Toolkit.getDefaultToolkit().sync();对Swing应用程序的需求.你有这个需求的参考吗?
  • 你提到"动画",但我看不到动画代码.
  • 在你的GraphingData类的paintComponent方法中,你在for循环中填充省略号,但是你没有将它们与行连接起来,所以你只看到图中的点而没有行就不足为奇了.

考虑更多地隔离你的问题并发布一个sscce,一个我们可以编译,运行,修改和纠正的最小测试程序,它向我们展示你的问题,但没有额外的代码与问题无关或演示所需.