摆动画点

时间:2015-03-29 00:12:22

标签: java swing

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

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

VectorCalculation

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

PointShape

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

DrawPoint

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

2 个答案:

答案 0 :(得分:0)

您永远不会在veccal中初始化MainApplication 您也永远不会在Vectorcalculation中初始化abcg

答案 1 :(得分:0)

      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上创建一个方法来获取最新的点。