salocin
salocin

Reputation: 13

Swing draw points

I have to program a math application in java. This application calculates vector addition and the scalar multiplication of a vector I have to set up Three start Point A,B and C (ABC is a Triangle) with a Vector (x,y). The application has a function to iterate through a loop which it will run 50000 times(the function calculates the half route from a start point A,B,C (randomized) to a new point p) and in this function I want to draw all points but I now to draw the points with Java Swing. Now i have solved the drawing the points A,B,C. Calculation of the points is now solved too.

Here is my Java Code for others how have to program something similar to my program.

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

       }

Point

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

Upvotes: 1

Views: 1523

Answers (2)

Gent Ahmeti
Gent Ahmeti

Reputation: 1599

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

You're only initializing px and py once. So you're actually drawing 50000 times, but the same point over and over again. Make a method on Vectorcalculation to get the latest points.

Upvotes: 1

Quicksilver002
Quicksilver002

Reputation: 735

You're never initializing veccal in MainApplication You're also never initializing a, b, c, and g in Vectorcalculation.

Upvotes: 0

Related Questions