Timer setup
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Timer setup

  1. #1
    Join Date
    Mar 2012
    Posts
    2

    Timer setup

    Hi. I want to create a timer in a JFrame.

    It should perform the following action:

    1. create ovals at given (x,y) coordinates at different points of time. (the time is in milliseconds). The time points are not periodical. they r random.
    2. 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)
    19:01:22.504 (x,y)

    Please help me.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Timer setup

    We won't do your homework for you but if you show you have made some effort we will help you.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Mar 2012
    Posts
    2

    Re: Timer setup

    I have created a JFrame with the following code:


    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;


    /**
    * @author Akshintala
    */

    /**
    * @param args
    */

    public class CopyOfLadderConstruction extends JFrame {

    /**
    *
    */
    private static final long serialVersionUID = 1L;


    public void paint(Graphics g){
    Graphics2D g2 = (Graphics2D) g;

    g2.setStroke(new BasicStroke(2));
    g.setColor( Color.GRAY);

    // Draw the rungs:
    for (int x=300; x<=950; x+=25) {
    g.drawLine (x,300, x,395);

    }

    for (int x=300; x<=950; x+=25) {
    g.drawLine (x,405, x,500);
    g.setColor( Color.GRAY);

    }


    for (int x=298; x<=945; x+=25) {
    for(int i=1;i<=27;i++){

    String iStr = Integer.toString(i);
    g.setColor( Color.BLACK);
    // g.drawString(iStr,x,295);

    }
    }

    // Draw the side and middle walls

    g.setColor( Color.BLACK );
    g2.setStroke(new BasicStroke(4));
    g.drawLine(300,300,950,300); // Upper side wall
    g.drawLine(300,500,950,500); // Lower side wall
    g2.setStroke(new BasicStroke(6));
    g.drawLine(300,400,950,400); // Middle ridge


    }

    public static void main (String args[]){

    LadderConstruction lc= new LadderConstruction();

    JFrame frame = new JFrame("Ladder Rung Sensor");
    frame.add(lc);
    frame.setSize(200,100);
    frame.setVisible(true);



    }


    }




    Now, I dont know how to create a timer with my requirements mentioned in Post 1. I am a complete beginner in Java. Can u pls help me now? Ofcourse, I dont ask u if I didnot put any effort.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Timer setup

    When posting code please use code tags.

    Your class should not extend JFrame, it should extend JPanel.
    You should not override the paint() method, you should override paintComponent() instead.

    As for the timer, you could use a javax.swing.Timer and change the delay time each time the listener is called to randomize the delay or you could create a background thread that loops around and in the loop puts the thread to sleep for a random amount of time before calling repaint().
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Nov 2011
    Posts
    189

    Re: Timer setup

    After trying keang's method you could try this if you find it easier.

    Code:
    Public class t1 implements Runnable()
    {
    Int curentTime = 0;
    Random rand1 = new Random();
    Int time1 = 1+rand1.nextInt(100);//the upper limit of time
    time1 = time1*1000; // we want to use seconds not milis
    
    Public void Run(){
    while(curentTime>100000)
    {
    //put code to check if time1 == curentTime.. This can also be applied to other timers
    // put code to draw circles if needed
    Thread.sleep(1000);
    CurentTime+= 1000;
    }
    }
    }

    But if i were you i'd use keangs way since mine looks a little too complicated

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center