Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 45
  1. #21
    life4menu is offline Member
    Join Date
    Mar 2012
    Posts
    22
    Rep Power
    0

    Default Re: create moving points with respect to time

    I am sorry... I am new to this forums... And I dont know how to add code tags... I will do it from next time...

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: create moving points with respect to time

    What is the code in the actionListener supposed to do?
    Is the actionListener method being called by the timer?

  3. #23
    life4menu is offline Member
    Join Date
    Mar 2012
    Posts
    22
    Rep Power
    0

    Default Re: create moving points with respect to time

    the code in the actionlistener is supposed to paint ovals each at a particular point of time. And the actionlistener should be called by the timer.

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: create moving points with respect to time

    Can you explain what each part of the code that you posted does to achieve that task?
    For example what is this for:
    if (!worker.isDone()) {

    Add comments on each line of the listener code that describes what the purpose of the code is and repost the code.

    The listener code does NOT do any painting. It computes the location of where the paintComponent method should draw.

    Is the listener code called?

  5. #25
    life4menu is offline Member
    Join Date
    Mar 2012
    Posts
    22
    Rep Power
    0

    Default Re: create moving points with respect to time

    Ohh thats not my code... I was just showing you the part of code from the examples in How to use swing timers. I was trying to understand it if it could be of any help to my task. But my aim of creating timer is completely different.

    I want to create just 4 ovals in a JFrame that can be displayed just for some milliseconds of time. these time periods are not uniform. they are random. For eg:
    19:01:22.074 (x,y)
    19:01:22.093 (x,y)
    19:01:22.189 (x,y)
    19:01:22.232 (x,y)
    19:01:22.310 (x,y)
    19:01:22.317 (x,y)
    19:01:22.429 (x,y)
    19:01:22.497 (x,y)

    I created the JFrame. And my next step is to do the above task.

    I even don't know if creating a Timer to do this task is the correct way of doing it... If there are any other ways to solve this, please let me know. Thanks.

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: create moving points with respect to time

    thats not my code..
    Why post some one else's code that is not related to your problem???

    If you want to draw ovals at different locations over time, then a Timer will be useful.
    Do the project one step at a time.
    Can you draw an oval at a x,y position? You will need to be able to do this.

    When you have the code to do the above:
    Then Create a Timer that calls a listener method.
    Have the listener method change the x,y location of the oval and call the repaint method.

    When you can do the above, then worry about fitting the movements to the formulas and equations you have posted.

  7. #27
    life4menu is offline Member
    Join Date
    Mar 2012
    Posts
    22
    Rep Power
    0

    Default Re: create moving points with respect to time

    Thanks for the suggestions. have written a code according to ur instructions. It is below:

    Java Code:
    import java.util.Date;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class TimerRun implements ActionListener {
    	JPanel lrs = new JPanel();
    	LadderConstruction lc = new LadderConstruction();
    	
        Timer t = new Timer(1000, this);
    
      public TimerRun( ) {
       
        t.start( );
      }
      public void actionPerformed(ActionEvent ae) {
    	  
    	  int x = 270;
    		int y= 350;
    		int y1 = 450;
    		while(x<945){
    			x = x+25;
    		}
    	lc.repaint();
    	t.stop();
      }
    
      public void paint(Graphics g){
    		Graphics2D g2 = (Graphics2D) g;
    		
    		
    		  int x = 270;
    			int y= 350;
    			int y1 = 450;
    			while(x<945){
    				x = x+25;
    			}
    		g2.setColor(Color.CYAN);
    		g2.drawOval(x,y,10,5);
    		g2.drawOval(x,y1,10,5);
    		g2.fillOval(x, y, 10, 5);
    		g2.fillOval(x, y1, 10, 5);
    
    		}
    }
    Hope the code tags are added correctly.

    When I run this program, it says,
    "java.lang.NoSuchMethodError: main
    Exception in thread "main" ".

    on the console. Please help me.

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: create moving points with respect to time

    You have not written a complete program that will execute by itself. Look at your post#7 and line 140
    You need a JFrame and a main method for a program to show GUI and to be executable.

  9. #29
    life4menu is offline Member
    Join Date
    Mar 2012
    Posts
    22
    Rep Power
    0

    Default Re: create moving points with respect to time

    I did it now, and it is working... Thanks a lot for the help.

    Now I can create 2 rows of 27 ovals each.

    But I want to create just 4 ovals appearing for a certain time first and then the next four ovals at different time and so on. So that it could give an impression of 4 ovals moving from right to left on x-axis. the ovals should appear only for a specific milliseconds of time and ten disappear.

    It would be great if you could help me in doing this.

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: create moving points with respect to time

    Change the code to show 4 ovals, each one with its own x,y location.

    Create a Timer that calls a listener method. Have the listener method change the x,y locations for the 4 ovals and call repaint.
    The system will then call the paintComponent method which will draw the ovals at their new locations.

  11. #31
    life4menu is offline Member
    Join Date
    Mar 2012
    Posts
    22
    Rep Power
    0

    Default Re: create moving points with respect to time

    thanks. but the task is not just to change the locations but also to tell the ovals to stay at those points for a specific time that is present in our data... for eg: in my post#25...

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: create moving points with respect to time

    Do it one step at a time.
    1) Move the ovals
    2)do the moves at the desired rate.

  13. #33
    life4menu is offline Member
    Join Date
    Mar 2012
    Posts
    22
    Rep Power
    0

    Default Re: create moving points with respect to time

    Dear Mr. Norm,

    I have created a Timer and a main method to run it. But it doesnot run and it is show the following error...

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at TimerRun.actionPerformed(TimerRun.java:28)
    at javax.swing.Timer.fireActionPerformed(Unknown Source)
    at javax.swing.Timer$DoPostEvent.run(Unknown Source)

    And this is my code...

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class TimerRun implements ActionListener {
    	LadderConstruction lc= new LadderConstruction();
    	private Timer t = new Timer(1000, this);
    	private JPanel ladder;
    
    	public TimerRun( ) {
    
    		t.start( );
    		t.setInitialDelay(074);
    		ladder = new JPanel();
    		ladder.add(lc);
    	}
    
    	public void actionPerformed(ActionEvent ae) {
    		Graphics g = ladder.getGraphics();
    		int x = 270;
    		int y= 350;
    		int y1 = 450;
    		while(x<945){
    			x = x+25;
    			//		System.out.println(x);
    			g.setColor(Color.CYAN);
    			g.drawOval(x,y,10,5);
    			g.drawOval(x,y1,10,5);
    			g.fillOval(x, y, 10, 5);
    			g.fillOval(x, y1, 10, 5);
    
    		}
    
    		//	lc.repaint();
    		t.stop();
    	}
    
    	public static void main(String[] args){
    		TimerRun display = new TimerRun();
    		
    		}
    
    }
    please help me where the mistake is.
    Last edited by Norm; 03-13-2012 at 12:17 PM.

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: create moving points with respect to time

    Go back and read my posts about drawing the shapes. That is done in the paintComponent method.
    The timer method changes the x,y locations and calls repaint(). The timer method does NOT draw any shapes.

    By timer method I mean: actionPerformed() which is called by the Timer object.

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: create moving points with respect to time


  16. #36
    life4menu is offline Member
    Join Date
    Mar 2012
    Posts
    22
    Rep Power
    0

    Default Re: create moving points with respect to time

    Hi,

    Thanks for the reply... Actually I am trying something different, if it could work this way.

    I am drawing 4 rectangles(changed from ovals to rectangles) at a time using while loop... for eg:

    Java Code:
    while(x<320){
    			x = x+25;
    			//		System.out.println(x);
    			g2.setColor(Color.CYAN);
    			g2.drawRect(x, y, 10, 5);
    			g2.drawRect(x, yl, 10, 5);
    			g2.fillRect(x, y, 10, 5);
    			g2.fillRect(x, yl, 10, 5);
    			g2.clearRect(x, y, 10, 5);
    			g2.clearRect(x, yl, 10, 5);
    
    		}
    And then I am drawing the next four rectangles... using another while loop. My problem is I want the rectangles from the 1st while loop to disappear while I draw the next 4 rectangles. Kindly help me how I can do this. Thank you.

  17. #37
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: create moving points with respect to time

    You need to read what I said earlier about how to draw images that show at different locations over time.
    1) in a method: compute the location of where the images should be drawn the first time and call repaint()
    2) in the paintComponent method draw the images at the location set by step 1) (this method called by java program after repaint call)
    3) start a timer to call the method used in step 1) after an interval of time

  18. #38
    life4menu is offline Member
    Join Date
    Mar 2012
    Posts
    22
    Rep Power
    0

    Default Re: create moving points with respect to time

    I did it like this.

    1. I directly calculated the locations and drew the images at that locations in the paint component method.
    2. I created a timer class to do this step by step.

    Luckily its working.

    My question is, I want to delete the shapes I drew in each previous steps. For this I created a second timer in the same Timerclass. But it is not working. I want to delete the shapes that I draw in each step.

  19. #39
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default Re: create moving points with respect to time

    You should separate the calculations from the drawings as per the instructions I have posted before.
    You don't need to delete the old shapes, you draw everything at new locations.
    One timer should be enough.

  20. #40
    life4menu is offline Member
    Join Date
    Mar 2012
    Posts
    22
    Rep Power
    0

    Default Re: create moving points with respect to time

    Hi,

    I drew the shapes at desired points using timer. it worked. but they are not printing one by one. Can u please tell me how I can print them one by one.

    Here is my code:

    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();
    
    
    }
    
    }
    Kindly tell me where the mistake is... And how to print them one by one..
    Last edited by Norm; 03-15-2012 at 02:34 PM. Reason: fixed code tags

Page 2 of 3 FirstFirst 123 LastLast

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
  •