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

    Question create moving points with respect to time

    Hi,

    I want to create 4 moving points in an applet that move from one point to another from right to left. The points should be displayed with respect to time until milli seconds; given x and y coordinates. Please help me.

    Thanks in advance.

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

    Default Re: create moving points with respect to time

    Create a class that extends JPanel, override its paintComponent method and add some Graphics class's drawing method calls. Add the JPanel class to the applet's GUI container.
    Create a Timer and have the method it calls periodically change the x,y positions that the drawing methods are using.

    There is a tutorial on writing custom displays. I don't have the link but there are several references to it here on the forum that you might find if you Search.
    Last edited by Norm; 03-05-2012 at 09:55 PM.

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

    Default Re: create moving points with respect to time

    Thanks Norm for ur reply. I tried doing it. I am a beginner in Java. So dont know many things. I have written a code that can create ovals at desired coordinates. But the problem is
    1. I am unable to run it in an Applet, that I have already created before. I can attach the codes for u.

    2. I want to create just 4 points on the screen 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 want to create these ovals at these points of time. Please help me.

    My Applet Code is:
    Java Code:
    public class CopyOfLadderConstruction extends Applet {
    
    	/**
    	 * 
    	 */
    	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   
    
    
    	}
    
    }
    
    //
    
    And my JFrame code is
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.*;
    import javax.swing.*;
    
    
    public class MovingPoints extends JFrame implements ActionListener {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JPanel container;
    	JLabel labelCounter;
    	private Timer timer1 = new Timer(074, this);
    	int x = 320;
    	int y = 298;
    	int a = 10;
    	int b = 5;
    	int x1 = 320;
    	int y1 = 420;
    	public MovingPoints() {
    		//set initial delay to 1000 milliseconds
    		timer1.setInitialDelay(1150);
    
    		//initialize window
    		container = new JPanel();
    		this.add(container);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(27,27);
    		this.setVisible(true);
    
    		//start timer
    		timer1.start();
    	}
    	/**
    	 * when timer begins this method draws ovals that increase in size
    	 * and descend vertically down the window
    	 */
    	public void actionPerformed(ActionEvent e) {
    		if (x < 950){
    			Graphics g = container.getGraphics();
    			g.fillOval(x, y, a, b);
    			g.setColor(Color.GREEN);
    			g.drawOval(x,y,a,b);
    			x = x+25;
    //			x = x+25;
    //			y = y+2;
    //			z = z+2;
    		}
    		else //stop timer (and drawing) when z coordinate is greater than 99
    			timer1.stop();
    		if (x1 < 950){
    			Graphics g = container.getGraphics();
    			g.fillOval(x1, y1, a, b);
    			g.setColor(Color.GREEN);
    			g.drawOval(x1,y1,a,b);
    			x1 = x1+25;
    //			x = x+25;
    //			y = y+2;
    //			z = z+2;
    		}
    		else //stop timer (and drawing) when z coordinate is greater than 99
    			timer1.stop();
    	}
    	
    	
    }
    Thanks in Advance.
    Last edited by Norm; 03-06-2012 at 12:55 PM. Reason: added code tags

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

    Default Re: create moving points with respect to time

    I am unable to run it in an Applet,
    Please explain what the problem is. Copy and paste here any error messages.
    Describe what happens when you try to execute the program.

    The code you posted does not compile and therefore can not be tested. The code must compile if you expect any one to test it.
    Last edited by Norm; 03-06-2012 at 12:59 PM.

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

    Default Re: create moving points with respect to time

    Both the programs work well independently. But I want to run the class "MovingPoints" in the class "LadderConstruction" or vice versa.

    I tried to call my Applet in the JFrame program. The applet works but the JFrame doesnot give any output. If I try to call my JFrame program again in the main method along with Applet, JFrame works but Applet doesnot work.

    I somehow want to merge both these programs and want both of them to work in the same Applet.

    And THIS CLASS MovingPoints creates 27 points in increasing points of time. I dont want that. But.
    I want to create the points on the screen 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)

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

    Default Re: create moving points with respect to time

    the applet code did not compile. For one thing it is missing the import statements.
    There is no timer in the applet so it won't move anything.

  7. #7
    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. Here is the complete code for Applet:
    Java Code:
    import java.applet.Applet;
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Point2D;
    
    
    /**
     * @author Akshintala
     */
    
    /**
     * @param args
     */
    
    public class CopyOfLadderConstruction extends Applet {
    
    	/**
    	 * 
    	 */
    	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   
    
    
    	}
    
    }
    
     And here is the complete code for JFrame:
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.*;
    import javax.swing.*;
    
    
    public class MovingPoints extends JFrame implements ActionListener {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JPanel container;
    //	JLabel labelCounter;
    	private Timer timer1 = new Timer(074, this);
    	int x = 320;
    	int y = 298;
    	int a = 10;
    	int b = 5;
    	int x1 = 320;
    	int y1 = 420;
    	public MovingPoints() {
    		//set initial delay to 1000 milliseconds
    		timer1.setInitialDelay(1150);
    
    		//initialize window
    		container = new JPanel();
    		this.add(container);
    //        Applet theApplet = new LadderConstruction();
    //
    //		this.getContentPane().add(theApplet);
    
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	
    		setSize(27,27);
    		this.setVisible(true);
    
    		//start timer
    		timer1.start();
    	}
    	/**
    	 * when timer begins this method draws ovals 
    	 */
    	public void actionPerformed(ActionEvent e) {
    		if (x < 950){
    			Graphics g = container.getGraphics();
    			g.fillOval(x, y, a, b);
    			g.setColor(Color.GREEN);
    			g.drawOval(x,y,a,b);
    			x = x+25;
    //			x = x+25;
    //			y = y+2;
    //			z = z+2;
    		}
    		else //stop timer (and drawing) when z coordinate is greater than 99
    			timer1.stop();
    		if (x1 < 950){
    			Graphics g = container.getGraphics();
    			g.fillOval(x1, y1, a, b);
    			g.setColor(Color.GREEN);
    			g.drawOval(x1,y1,a,b);
    			x1 = x1+25;
    //			x = x+25;
    //			y = y+2;
    //			z = z+2;
    		}
    		else //stop timer (and drawing) when z coordinate is greater than 99
    			timer1.stop();
    	}
    	
    
    	public static void main(String[] args) {
            //... Create an initialize the applet.
    		 
            JApplet theApplet = new LadderConstruction();
            new MovingPoints().container.setVisible(true);
    
           
            //theApplet.init();         // Needed if overridden in applet
            //theApplet.start();        // Needed if overridden in applet
            
            //... Create a window (JFrame) and make applet the content pane.
            JFrame MovingPoints = new JFrame("Sample Applet and Application");
            MovingPoints.setContentPane(theApplet);
            MovingPoints.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MovingPoints.pack(); // Arrange the components.
         
            System.out.println(theApplet.getSize());
            MovingPoints.setVisible(true);    // Make the window visible.
           
        }
    }
    Thanks for your time...
    Last edited by Norm; 03-06-2012 at 01:19 PM. Reason: added code tags

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

    Default Re: create moving points with respect to time

    Why are you mixing an JFrame application with an Applet?
    Applets are executed in a browser by coding an HTML page with an <APPLET tag and having a browser read the html file.
    If your are not going to execute in a browser, your CopyOfLadderConstruction class should not extend Applet. See my post#2

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

    Default Re: create moving points with respect to time

    I am just a beginner. So I thought it could be possible to do this way... If u think this is not the correct way, can u please tell me how to do it? I mainly want to print the points with respect to time.

    And I have read ur post#2... I cannot understand because I dont have that deep knowledge.

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

    Default Re: create moving points with respect to time

    I cannot understand because I dont have that deep knowledge.
    You should put this code aside for now and work on a simpler program.
    Create a class that extends JPanel.
    Override the paintComponet method in that class.
    Add a draw method to the paintComponent method that draws a shape.
    In the main class, create an instance of the above class.
    add that class to a JFrame, set the size and make the frame visible.

    Compile and execute. The shape should be drawn.

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

    Default Re: create moving points with respect to time

    Ok Thanks... I will try that...

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

    Default Re: create moving points with respect to time

    Thanks a lot... It worked. I could draw the ladder in a JFrame now. But now I want to draw the ovals with respect to time in milliseconds at specific coordinates. can u please tell me how I can do that?

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

    Default Re: create moving points with respect to time

    Add a timer that changes the x,y values used to draw the shape in the paintComponent method.
    After the method called by the timer changes the x,y values, it should call repaint().

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

    Default Re: create moving points with respect to time

    Hi Mr. Norm,

    I tried a lot to create and add this timer. But I couldnot succeed. This is completely new for me. Can u pls help me how I can do it by giving an example code. Thanks a lot...

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: create moving points with respect to time

    Plenty of example code in the Oracle Swing tutorials, many of which are linked from the API for the related class.

    So far, which of those tutorials have you gone through?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: create moving points with respect to time

    You posted your response in the wrong thread. I've deleted it from there.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: create moving points with respect to time

    I have gone through the examples, "How to use Swing Timers"...

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: create moving points with respect to time

    Which part of that tutorial did you not understand?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: create moving points with respect to time

    I haven't understand actionPerformed part?
    Java Code:
    public void actionPerformed(ActionEvent e) {
        //If still loading, can't animate.
        if (!worker.isDone()) {
            return;
        }
    
        loopslot++;
    
        if (loopslot >= nimgs) {
            loopslot = 0;
            off += offset;
    
            if (off < 0) {
                off = width - maxWidth;
            } else if (off + maxWidth > width) {
                off = 0;
            }
        }
    
        animator.repaint();
    
        if (loopslot == nimgs - 1) {
            timer.restart();
        }
    }
    Last edited by Norm; 03-12-2012 at 12:07 PM. Reason: added code tags

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: create moving points with respect to time

    Quote Originally Posted by life4menu View Post
    I haven't understand actionPerformed part?
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    1. Let's see more initiative on your part. This lesson is part of the same tutorial. Look for other sections that are useful to you.

    2. Look around the FAQs section of this site and learn how to post code to retain its formatting. A moderator won't add the code tags for you every time.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 1 of 3 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
  •