简体   繁体   English

使用x,y坐标点列表绘制曲线峰线

[英]Drawing curve line peaks using list of x,y coordinates points

I am trying to draw a curve line peaks using a list of x,y coordinates points. 我正在尝试使用x,y坐标点列表绘制一条曲线峰线。 Will be useful for a mouse movement algorithm that I coded and would like a graph/analysis for it. 对于我编码的鼠标移动算法很有用,并且需要对其进行图形/分析。

What I have tried: 我尝试过的

import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class curve extends JFrame {

    public static void main(String[] args) {
        final JFrame f = new JFrame("My curve");
        f.setSize(600, 400);
        f.setVisible(true);

        f.add(new JComponent() {
            public void paintComponent(Graphics2D g) {

                Graphics2D graphics = g;// your graphics object
                double[] x = { 50, 100, 300, 500 }; // x coordinates of polyline
                double[] y = { 70, 120, 50, 280 }; // y coordinates of polyline
                Path2D polyline = new Path2D.Double();
                polyline.moveTo(x[0], y[0]);
                for (int i = 1; i < x.length; i++) {
                    polyline.lineTo(x[i], y[i]);
                }
                graphics.draw(polyline);
            }
        });

    }
}

Results I get: Empty JFrame 我得到的结果:空的JFrame

Expected results: Something like that Example 预期结果:类似示例

I have been trying to make it draw anything on the JFrame since the morning but with no luck. 从早上开始,我一直在尝试使其在JFrame上绘制任何内容,但没有运气。 It will be really great if you could show me how to do it. 如果您能告诉我该怎么做,那真是太好了。 Thanks! 谢谢!

Call setVisible after you call add . 调用add之后,调用setVisible Swing's layouts are lazy, unless you tell them, they generally won't update themselves. Swing的布局是惰性的,除非您告诉他们,否则它们通常不会自我更新。

Next, add @Override to methods you "think" you're overriding... 接下来,将@Override添加到您“认为”您要覆盖的方法中...

        @Override
        public void paintComponent(Graphics2D g) {

This will provide you with compiler errors when you do something wrong (and spend a bunch of time scratching your head wondering why nothing works) 当您做错了事情时,这将为您提供编译器错误(并花了很多时间挠头思考为什么什么都不起作用)

With this addition, you should then get an error message similar to this... 使用此添加项,您应该然后收到类似于此的错误消息...

/.../Curve.java:20: error: method does not override or implement a method from a supertype
            @Override
1 error

A quick jump over to the JavaDocs would highlight the specific issue. 快速跳转到JavaDocs将突出显示特定问题。

The expected method signature is paintComponent(Graphics) not paintComponent(Graphics2D) 预期的方法签名是paintComponent(Graphics)而不是paintComponent(Graphics2D)

And some minor tweaks later... 然后稍作调整...

文档帮助

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Curve extends JFrame {

    public static void main(String[] args) {
        final JFrame f = new JFrame("My curve");
        f.setSize(600, 400);

        f.add(new JComponent() {
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D graphics = (Graphics2D) g.create();// your graphics object
                double[] x = {50, 100, 300, 500}; // x coordinates of polyline
                double[] y = {70, 120, 50, 280}; // y coordinates of polyline
                Path2D polyline = new Path2D.Double();
                polyline.moveTo(x[0], y[0]);
                for (int i = 1; i < x.length; i++) {
                    polyline.lineTo(x[i], y[i]);
                }
                graphics.draw(polyline);
                graphics.dispose();
            }
        });
        f.setVisible(true);

    }
}

These are all very easy (and, regrettably, common) mistakes to make. 这些都是很容易犯的错误(很遗憾,是常见错误)。 The language provides tools for you to use to help identify and reduce the likely hood of these issues (ie @Override ). 该语言为您提供工具,以帮助您识别和减少这些问题的可能性(即@Override )。

When it comes to things like painting, the most common issues are; 说到绘画,最常见的问题是绘画。

  • Not adding the component to a visible container (yes, this happens to everyone, more then we'd like to admit) 不将组件添加到可见容器中(是的,这种情况发生在每个人身上,更多的是我们想承认的)
  • Not actually overriding the method we thing we're overriding. 实际上并没有覆盖我们正在覆盖的方法。

This when the documentation and available tutorials/examples are invaluable to helping solve the problems - Yes, I've been caught out many times with "But I typed it exactly" only to find I've made an assumption about a parameter, or worse, an import . 当文档和可用的教程/示例对于帮助解决问题非常宝贵时-是的,我被“但是我准确键入”了很多次, 发现我对参数做了假设,或更糟糕的是import

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

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