java - Swing 绘制点

标签 java swing

我必须用 java 编写一个数学应用程序。此应用程序计算 vector 的 vector 加法和 vector 的标量乘法,我必须使用 vector (x,y) 设置三个起点 A、B 和 C(ABC 是三角形)。该应用程序有一个迭代循环的函数,它将运行 50000 次(该函数计算从起点 A、B、C(随机)到新点 p 的半路线),在这个函数中我想绘制所有点,但现在我用 Java Swing 绘制这些点。现在我已经解决了A、B、C点的绘制问题。点的计算现在也解决了。

这是我的 Java 代码,供其他人如何编写与我的程序类似的东西。

vector 计算

  import java.util.ArrayList;
    import java.util.Random;

    public class Vectorcalculation {

        public static final double[] POINT_A = {40,40};
        public static final double[] POINT_B = {450,450};
        public static final double[] POINT_C = {40,450};

        ArrayList<Point> points = new ArrayList<Point>();

        double p[] = {70,70};

        public double[] addVectors( double[] a, double[] b )
        {

            double[] result = new double[a.length];

            result[0] = a[0] + b[0];
            result[1] = a[1] +b[1];

            return result;
        }

        public double[] multipleScalar (double s, double[]a) {

            double[] result = new double[a.length];

            result[0] = a[0] * s;
            result[1] = a[1] * s;

            return result;

        }

        public double[] randomVector (double[] pointA, double[] pointB, double[] pointC) {

            double[] randomPoint = null;

            //note a single Random object is reused here
            Random randomGenerator = new Random();
            int i= randomGenerator.nextInt(3);
            if (i==0) {
                randomPoint = pointA;
            }
            else if (i==1) {
                randomPoint = pointB;
            }
            else if(i==2) {
                randomPoint = pointC;
            }
            return randomPoint; 
        }

        public void setUpPoints () {

            double pABC[] = {0,0};

            for(int i = 0; i < 50000 ; i++) {

                double[] randvec = randomVector(POINT_A,POINT_B,POINT_C);

                pABC = addVectors(randvec ,multipleScalar(-1.0,p));
                p = addVectors(p,multipleScalar(0.5,pABC));

                int pNewX = (int) p[0];
                int pNewY = (int) p[1];

                points.add(new PointShape(pNewX, pNewY, 1, 1));
            }
        }

        public ArrayList<Point> getList() {
            return points;
        }

       }

import java.awt.Graphics;

public abstract class Point {
    private int x;
    private int y;
    private int width;
    private int height;

    public Point() {
        this(0, 0, 1, 1);
    }

    public Point(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public abstract void draw(Graphics g);

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

点形状

       import java.awt.Graphics;

    public class PointShape extends Point {
        public PointShape(int x, int y, int width, int height) {
            super(x, y, width, height);
        }

        public PointShape() {
            super();
        }

        @Override
        public void draw(Graphics g) {
            g.drawOval(getX(), getY(), getWidth(), getHeight());
        }
    }

绘图点

    import java.awt.Graphics;
    import java.util.ArrayList;


    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;

    public class DrawPoint extends JFrame {

        static Vectorcalculation vc = new Vectorcalculation();

        public ArrayList<Point> points = vc.getList();

        public DrawPoint(String title) {
            super(title);

            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setSize(700, 700);

            this.setLocationRelativeTo(null);
        }

        @Override
        public void paint(Graphics g) {
            for (Point s : points) {
                s.draw(g);
            }
        }

        public static void main(String args[]) {
            vc.setUpPoints();
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    DrawPoint d = new DrawPoint("MLAIT VectorCalculation");
                    d.setVisible(true);
                }
            });
        }
    }

最佳答案

      int px = (int) p[0];
      int py = (int) p[1];
//px and py never changes.

          for(int i = 0; i < 50000 ; i++) {

            double pABC[] = {0,0};
            pABC = vc.addVectors(vc.randomVector(POINT_A,POINT_B,POINT_C),vc.multipleScalar(-1.0,p));
            p = vc.addVectors(p,vc.multipleScalar(0.5,pABC));
            synchronized (p) { //I don't think you need a synchronised call here.                  
                g2d.setColor(new Color(0,0,0));
                //px = (int)p[0] and py = (int)p[1];
                g2d.fillOval(px, py, 6, 6);
            }
      }

您只初始化 px 和 py 一次。所以你实际上画了 50000 次,但同一点一遍又一遍。 在Vectorcalculation上做一个方法来获取最新的点。

关于java - Swing 绘制点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29324152/

相关文章:

java - Android 应用程序在重新启动时崩溃

Java - JOptionPane 错误消息和信息消息

java - TableModelListener 并不总是工作

java - JLabel 最初不显示

java - 通过其 ID 序列化 JAXB 对象?

java - 在 SQLite for Java 中比较包含重音符号的字符串不起作用

java - MagTek——uDynamo (DUKPT)

java - 有没有一种方法可以使用 | 一次比较多个(非 boolean 值)事物?或 & 在 Java 中,类似于如何使用 | 捕获多个异常?

java - GUI 通常如何操作?

java - JDialog装饰true但没有关闭按钮