Java Swing中的图形绘制仅绘制点 [英] Graph plotting in Java Swing only draws points

查看:1547
本文介绍了Java Swing中的图形绘制仅绘制点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究一个程序,其中某些数值变量会随着时间而变化,并在每次迭代中显示其值。这很好,但现在我想绘制一张图表,展示它们随着时间的演变。
所以,我看了一个在Swing中绘制图表的代码示例。我的最终代码如下所示:

  public class Populus3 extends JPanel 
{
public static void main String [] args)抛出IOException {

final Populus3 pop = new Populus3();

JFrame f = new JFrame(); //我要绘制图形
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); //我正在为程序的另一个元素运行动画
frame.add(pop,BorderLayout.CENTER);
frame.setSize(graphSize,graphSize);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//插入各种东西



$ b $ public void paint(Graphics g)
{
super.paint(g);
paintCell(g,1);
Toolkit.getDefaultToolkit()。sync(); //需要linux用户正确绘制和动画图像
g.dispose();
}


public void actionPerformed(ActionEvent e){
repaint();



$ b @Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i = 0; i< particleType.length; i ++)
paintCell(g,i); //为动画面板绘制一个小圆圈的方法
}



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;

保护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();
//绘制坐标。
g2.draw(新的Line2D.Double(PAD,PAD,PAD,h-PAD));

//绘制abcissa。
g2.draw(新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();
//标记数据点。
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; (int i = 0; i if(data [i]> max)
max = data [i];
}
return max;
}
}
}

现在,动画面板的作品正好。另一方面,图形面板......当我运行该程序时,它显示了一串红点,没有线连接它们。我做错了什么?

解决方案

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




  • GeneralPath 中累积可根据需要呈现的点,。






I'm currently working on a program where certain numerical variables, which evolve over time, have their value displayed on each iteration. That works well enough, but now I want to plot a graph that shows their evolution over time. So, I looked into an example of code for plotting graphs in Swing. My final code looks like this:

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;
        }
    }
}

Now, the animation panel works just fine. The graph panel, on the other hand...when I run the program, it displays a bunch of red dots, without lines to connect them. What am I doing wrong?

解决方案

In addition to @Hovercraft's helpful suggestions, also consider these other approaches:

  • Accumulate the points in a GeneralPath that may be rendered as required, for example.

  • Connect the points using repeated calls to drawLine() using a suitable coordinate system, outlined here.

这篇关于Java Swing中的图形绘制仅绘制点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆