Page 3 of 3 FirstFirst 123
Results 41 to 45 of 45
  1. #41
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,581
    Rep Power
    23

    Default Re: create moving points with respect to time

    What do you mean by "one by one"?

    Is it this:
    draw shape 1
    wait some
    draw shape 2
    wait some
    etc

    Your code does NOT follow the design I recommended earlier:
    1) Do the drawing in the paintComponent method using the Graphics object passed to the method
    2) compute the positions in the Time listener method and call repaint()

  2. #42
    life4menu is offline Member
    Join Date
    Mar 2012
    Posts
    22
    Rep Power
    0

    Default Re: create moving points with respect to time

    Thanks for ur reply. Yes I want to draw a shape, wait, draw another and wait and so on.

    Actually I got different kind of output from my experiment today, where I don't have to calculate the locations. I just have to enter my result in x and y positions. So, I didnot write any method to calculate locations.

    Now everything worked. but the only thing is to draw shape, wait, draw next shape, wait and so on... and after drawing the last shape, I want to erase all the shapes drawn before.

    Please help. Thanks.

  3. #43
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,581
    Rep Power
    23

    Default Re: create moving points with respect to time

    You need to post your code and explain what the problem is with it.
    The last code you posted was wrong. It did not draw in the paintComponent method.

  4. #44
    life4menu is offline Member
    Join Date
    Mar 2012
    Posts
    22
    Rep Power
    0

    Default Re: create moving points with respect to time

    Hi..

    I have created instance of a class LadderContruction in my code. That class has the paintcomponent method. That paint component method draws a ladder in my JFrame. that is fine.

    The code I post here draws just the shapes on this ladder that I created in previous paint component method. Everything is working fine.
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.*;
     
    import javax.swing.*;
    public class TimerRun extends JFrame implements ActionListener {
    /**
         * 
         */
    private static final long serialVersionUID = -6910867316879215818L;
    private JPanel container;
    LadderConstruction lc= new LadderConstruction();
    RungCoordinates rc = new RungCoordinates();
     
    private Timer timer1 = new Timer(2000, this);
    //private Timer timer2 = new Timer(1000, this);
     
    int x = rc.r5x;
    int y= rc.rY;
    int yl = rc.lY;
    int x1 = rc.l7x;
    int x2 = rc.l8x;
    int x3 = rc.r10x;
    int x4 = rc.l13x;
    int x5 = rc.r15x;
    int x6 = rc.l18x;
    int x7 = rc.r21x;
     
    public TimerRun() {
    //set initial delay to 1000 milliseconds
    timer1.setInitialDelay(1000);
    timer1.getDelay();
    //timer2.setInitialDelay(2500);
     
     
    //initialize window
    container = new JPanel();
    this.add(container);
    this.add(lc);
    //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(1200,1200);
    this.setVisible(true);
     
     
     
    //start timer
    timer1.start();
    //timer2.start();
     
    }
     
    /**
    * when timer begins this method draws rectangles from left to right
    */
     
    public void actionPerformed(ActionEvent e) {
     
     
     
    Graphics g = lc.getGraphics();
     
    g.setColor(Color.CYAN);
    g.drawRect(x, y, 10, 5);
    g.fillRect(x, y, 10, 5); // R5 rung
     
    g.drawRect(x1, yl, 10, 5);
    g.fillRect(x1, yl, 10, 5); // L7 rung
     
    g.setColor(Color.MAGENTA);
    g.drawRect(x, y, 10, 5);
    g.fillRect(x, y, 10, 5); // R5 2nd time
     
    g.drawRect(x2, yl, 10, 5);
    g.fillRect(x2, yl, 10, 5); // L8
     
    g.drawRect(x3, y, 10, 5);
    g.fillRect(x3, y, 10, 5); // R10
     
    g.setColor(Color.RED);
    g.drawRect(x, y, 10, 5);
    g.fillRect(x, y, 10, 5); // R5 3rd time
     
    g.setColor(Color.MAGENTA);
    g.drawRect(x3, y, 10, 5);
    g.fillRect(x3, y, 10, 5);  // R10 2nd time
     
    g.setColor(Color.MAGENTA);
    g.drawRect(x2, yl, 10, 5);
    g.fillRect(x2, yl, 10, 5); // L8 2nd time
     
    g.setColor(Color.CYAN);
    g.drawRect(x4, yl, 10, 5);
    g.fillRect(x4, yl, 10, 5); // L13
     
    g.setColor(Color.RED);
    g.drawRect(x3, y, 10, 5);
    g.fillRect(x3, y, 10, 5);  // R10 3nd time
     
    g.setColor(Color.MAGENTA);
    g.drawRect(x4, yl, 10, 5);
    g.fillRect(x4, yl, 10, 5); // L13 2nd time
     
    g.setColor(Color.RED);
    g.drawRect(x4, yl, 10, 5);
    g.fillRect(x4, yl, 10, 5); // L13 3rd time
     
    g.setColor(Color.CYAN);
    g.drawRect(x5, y, 10, 5);
    g.fillRect(x5, y, 10, 5); // R15 rung
     
    g.setColor(Color.YELLOW);
    g.drawRect(x4, yl, 10, 5);
    g.fillRect(x4, yl, 10, 5); // L13 4th time
     
    g.setColor(Color.MAGENTA);
    g.drawRect(x5, y, 10, 5);
    g.fillRect(x5, y, 10, 5); // R15 2nd time
     
    g.setColor(Color.CYAN);
    g.drawRect(x6, yl, 10, 5);
    g.fillRect(x6, yl, 10, 5); // L18 rung
     
    g.setColor(Color.RED);
    g.drawRect(x5, y, 10, 5);
    g.fillRect(x5, y, 10, 5); // R15 3rd time
     
    g.setColor(Color.MAGENTA);
    g.drawRect(x6, yl, 10, 5);
    g.fillRect(x6, yl, 10, 5); // L18 2nd time
     
    g.setColor(Color.CYAN);
    g.drawRect(x7, y, 10, 5);
    g.fillRect(x7, y, 10, 5); // R21 rung
     
     
    timer1.stop();
     
     
    }
     
    public static void main(String[] args){
    TimerRun display = new TimerRun();
     
     
    }
     
    }
    and I added thread.sleep(1000)
    after drawing each shape. So they are drawn one by one. But after finishing the drawing, I want to disappear all the shapes. For this, I need some syntax related help. Thank you.

  5. #45
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,581
    Rep Power
    23

    Default Re: create moving points with respect to time

    Let me repeat this, you seem to have missed it all the other times I said it:
    You should do all the drawing in the paintComponent method.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Replies: 1
    Last Post: 08-11-2011, 10:46 AM
  2. code for moving objects at same time
    By Muhammad Assad in forum New To Java
    Replies: 13
    Last Post: 06-20-2010, 05:11 PM
  3. Replies: 3
    Last Post: 06-16-2010, 10:27 PM
  4. Replies: 2
    Last Post: 08-24-2008, 12:30 AM
  5. Urgent-Imp-Displaying message with respect to system time
    By garinapavan in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 02:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •