lundi 31 août 2015

What can I do so that the program is not starting to draw from the last point added?

I am trying to create a program such that there is a square going inside another square (slightly rotated), and another square going inside and so on. I've managed to create a program that does that. But I want the program to make several boxes like that. Take a look at the illustration to get a better idea:

My problem is that after drawing the first box, and then trying to draw the second, the "cursor" drawing the lines is kind of stuck at the last point, so it's drawing the line from the last point of my first square to the first point in the second square, like this:

So as you can see inside pointPanel-constructor I've tried clearing the list, and resetting the counter and i-variable, but no luck. Is there any modifications I can do such that I don't have this problem? I've tried placing the clear()-instruction other places. But maybe I'm missing something.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;




public class Art {
    ArrayList<Point> points = new ArrayList<>();
    int i =0;
    public static void main(String[] args) {
        new Art();
    }

    public Art() {
        JFrame frame = new JFrame("Art");
        frame.add(new pointPanel());

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);


    }

    public class pointPanel extends JPanel{
        //Constructor
        public pointPanel(){
            points(0, 100);
            //points.clear();
            points(500, 200);

        }

        @Override
        //Dimension
        public Dimension getPreferredSize() {
            return new Dimension(800, 600);
        }



        public void points(double x, double y){
            double t=0.1;   //constant
            final int side = 100;
            int counter=0;

            while (counter<20){
                 //Init the first points-->
                Point p  = new Point((int)(x),      (int) (y)           );
                Point p1 = new Point((int)(x+side), (int)(y)            );
                Point p2 = new Point((int)(x+side), (int) (y-side)      );
                Point p3 = new Point((int)(x),      (int)(y-side)       );
                Point p4 = new Point((int)(x),      (int)(y)            );
                //-->and adding them to the list
                if (counter == 0) {
                    points.add(p);
                    points.add(p1);
                    points.add(p2);
                    points.add(p3);
                    points.add(p4);
                }


                //Dynamic part: 
                //If the method has been run earlier - place points making it possible to draw lines
                if (counter>0){
                    x=(1-t)*points.get(i).x + t * points.get(i+1).x;
                    y=(1-t)*points.get(i).y + t * points.get(i+1).y;
                    points.add(  new Point( (int) x, (int) y)  );
                    i++;    
                }

                if (counter>0){
                    x=(1-t)*points.get(i).x + t * points.get(i+1).x;
                    y=(1-t)*points.get(i).y + t * points.get(i+1).y;
                    points.add(  new Point( (int) x, (int) y)  );
                    i++;
                }

                if (counter>0){
                    x=(1-t)*points.get(i).x + t * points.get(i+1).x;
                    y=(1-t)*points.get(i).y + t * points.get(i+1).y;
                    points.add(  new Point( (int) x, (int) y)  );
                    i++;
                }

                if (counter>0){
                    x=(1-t)*points.get(i).x + t * points.get(i+1).x;
                    y=(1-t)*points.get(i).y + t * points.get(i+1).y;
                    points.add(  new Point( (int) x, (int) y)  );
                    i++;
                }

                if (counter>0){
                    x=(1-t)*points.get(i).x + t * points.get(i-3).x;
                    y=(1-t)*points.get(i).y + t * points.get(i-3).y;    
                    points.add(  new Point( (int) x, (int) y)  );       
                    i++;
                }



                counter++;

            }//while
        //counter=0;
        //i=0;
        x=0;
        y=0;

        }//metode

        //Paint-method
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;

            g2d.setColor(Color.RED);
            for (int i = 0; i < points.size()-1; i++) {
                g2d.drawLine(points.get(i).x, points.get(i).y, points.get(i+1).x, points.get(i+1).y);
            }

           g2d.dispose();
        }   
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire