0

Actually, I am working on an already written code but I have to add additional features. It is a grid system where you can draw shapes by mouse clicks and apply transformations like translation, rotation and so on. If i get the idea of how to do for one transformation, I think I will be able to manage for the other transformations. For e.g the existing code now just outputs the final transformation on a shape but I have to demonstrate that by an animation to the final output. How do I do that? bRotate is a Jbutton for Rotation

    bRotate.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e){
                     Panel.Translate=false;
                     Panel.Rotate=true;
                     Panel.Scale=false;
                     Panel.Shear=false;
                     Panel.Reflect=false;
                     Panel.Fill=false;
                     Panel.draw=false;

                    double angle = Integer.parseInt(tAngle.getText())*-1;
                    int ptsNum =  Panel.poly.X.length;

                    if( Panel.R==null){
                         Panel.R = new double[ptsNum][2];
                        for(int i=0;i<ptsNum;i++){
                             Panel.R[i][0]= Panel.poly.X[i];
                             Panel.R[i][1]= Panel.poly.Y[i];
                        }
                    }

                    ActionListener listener = new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                            Panel.R = Transformation.rotate( Panel.R, angle);

                            double [] xpts = new double[ptsNum];
                            double [] ypts = new double[ptsNum];
                            for(int i=0;i<ptsNum;i++){
                                xpts[i]=(int) Panel.R[i][0];
                                ypts[i]=(int) Panel.R[i][1];    
                            }

                             Panel.poly= new Polygon(xpts,ypts,xpts.length);        
                             Panel.undoPoly.add(Panel.poly);

                             Panel.currentPolygon=null;
                             Panel.enable=true;
                            Main.enabled();                     
                            repaint();
                        }
                    };

                    Timer timer = new Timer(10, listener);

                    timer.start();





                }

            }

    );
  • 1) Variable names should NOT start with an upper case character. The forum highlights the variable like a class name, making the code harder to read 2) The point of using a Timer is to replace looping code. 3. So the ActionListener for the button will simply start the Timer. 4) When the Timer fires the "state" of you code will be updated to go to the next step., whatever that is. In the case of rotation you would update the rotation by one degree and then paint the transformation. The next time the Timer firest you update it by another degree. When the rotation is finished you stop the timer/ – camickr Mar 24 '20 at 14:38
  • @camickr I've added the timer but it is just rotating without stopping. I want it to make a slow transformation and stop. How do I do that? –  Mar 24 '20 at 17:32
  • The variable names have not been fixed. Why are you still posting code that is hard to read? *I want it to make a slow transformation* As I said in my answer you need to change the "state" of your code to do one step at a time. I don't know how your code works so you need to restructure it to allow it to change state and then freeze. Then next time the Timer fires you change the "state" by one step. When all the steps are finished you stop the Timer. – camickr Mar 24 '20 at 19:47
  • @camickr what do you mean by state? –  Mar 25 '20 at 07:13
  • A Swing components have a "state" that can change dynamically. For example a JTextArea displays text. There is a method `append(…)` that allows you to add 1 or more character to the text area. This changes is state so it needs to be repainted to paint all the characters. Your question says *just outputs the final transformation* - so you have multiple steps to do the translation. each step would be a change of state. So you need to restructure the code so you can perform on step at a time and freeze the state and paint it. Then do the next step. – camickr Mar 25 '20 at 14:56
  • Check out: https://stackoverflow.com/a/33907282/131872 for a simple example. Each time the Timer fires the "state" is changed. That is the text is moved one character to the left – camickr Mar 25 '20 at 14:58
  • @camickr Sorry for late respond, but my rotation happens in a single step. I take the angle of rotation and points and do a matrix multiplication for the final shape. How do I get around this? –  Apr 10 '20 at 12:14
  • Rewrite your code so it does multiple small steps. Instead of rotating 45 degrees all at once you rotate 1 degree at a time. – camickr Apr 10 '20 at 15:16

0 Answers0