Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: Random Lines

  1. #1
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default Random Lines

    Im trying to make an applet that draws 100 random lines, one at a time.
    I new to applets and I have a feeling im not doing this right? Heres what I have:

    Java Code:
    package saver1;
    
    import java.awt.*;
    import java.applet.*;
    import java.util.*;
    
    public class saver1 extends Applet {
    int width;
    int height ;
    private int randomX1()
    { 
       return ( int ) ( Math.random() * width ); 
    }
    
    private int randomY1()
    { 
       return ( int ) ( Math.random() * height ); 
    }
    private int randomX2()
    { 
       return ( int ) ( Math.random() * width ); 
    }
    
    private int randomY2()
    { 
       return ( int ) ( Math.random() * height ); 
    }
    
    	
    
    	public static void main(String[] args) {}
    		public void paint(Graphics g)
    		  {
    		    
    		    
    		for ( int x = 0; x<100; x++ ){
    			g.drawLine(randomX1(), randomY1(), randomX2(), randomY2()); 
    		}
    
    	}
    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    First of write a simple application/applet to draw a single line. The way you have work out is completely wrong. Here is a simple example. See how I workout there.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class DrawingLines extends Applet {
    
       int width, height;
    
       public void init() {
          width = getSize().width;
          height = getSize().height;
          setBackground( Color.black );
       }
    
       public void paint( Graphics g ) {
          g.setColor( Color.green );
          for ( int i = 0; i < 10; ++i ) {
             g.drawLine( width, height, i * width / 10, 0 );
          }
       }
    }

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    What happens when you run it?

  4. #4
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    how would I incorperate randoms into the g.drawline?

  5. #5
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    ok, I figured out how to draw 100 random lines but how do I change it to draw them one at a time instead of all at the same time?

    Java Code:
    package saver1;
    
    import java.applet.*;
    import java.awt.*;
    import java.util.Random;
    
    public class saver1 extends Applet {
    
       int width, height;
    Random rnd = new Random();
       public void init() {
          width = getSize().width;
          height = getSize().height;
          setBackground( Color.black );
       }
    
       public void paint( Graphics g ) {
          g.setColor( Color.green );
          for ( int i = 0; i < 100; ++i ) {
        	  int x1 = rnd.nextInt( getSize().width );
              int y1 = rnd.nextInt( getSize().height );
              int x2 = rnd.nextInt( getSize().width );
              int y2 = rnd.nextInt( getSize().height );
             
        	  g.drawLine( x1, y1, x2, y2 );
          }
       }
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  7. #7
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    no, but i know that they have something to do with the path of execution

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Urgle View Post
    no, but i know that they have something to do with the path of execution
    With what? Threads?

  9. #9
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    yea with threads

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Ok, basically thread are unit of a program execution. You can have multiple such units. In case of you application we can think about that in this way.

    One unit for the drawing lines, and one unit for the display one by one.

    Do you know more about thread? There are two ways to do that. Using Thread class or Runnable interface. If you can do a simple example on Threads first of all, it's much better.

  11. #11
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    since I know almost nothing about threads Im not sure how to make my program use them

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    To do such things we have only two choices. Swing timer and Threads. Nothing to do with Swing timer here in your application. Only option is Threads. That's the easiest way.

  13. #13
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    I understand I need to use threads but how?

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Urgle View Post
    I understand I need to use threads but how?
    That what you should lean. About threads.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I've change that my previous code according to your requirement. Try to identify what's going on there.

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class DrawingLines extends Applet implements Runnable {
    
        int width, height;
        int i = 0;
        Thread t = null;
        boolean threadSuspended;
    
        public void init() {
            width = getSize().width;
            height = getSize().height;
            setBackground( Color.black );
        }
    
        public void paint( Graphics g ) {
          g.setColor( Color.green );
          g.drawLine( width, height, i * width / 10, 0 );
        }
    
        public void start() {
            if ( t == null ) {
                t = new Thread( this );
                threadSuspended = false;
                t.start();
            }
            else {
                if ( threadSuspended ) {
                    threadSuspended = false;
                    synchronized( this ) {
                        notify();
                    }
                }
            }
        }
       
        public void run() {
            try {
                while(true) {
                    ++i;
                    if(i ==10) {
                        i = 0;
                    }
                    
                    if(threadSuspended) {
                        synchronized(this) {
                            while(threadSuspended) {
                                wait();
                            }
                        }
                    }
                    repaint();
                    t.sleep(1000);
                }
            }
            catch(InterruptedException ex) {
                
            }
        }
    }

  16. #16
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    so basicly what youve done is made it pause after each line and then call repaint to paint the next line?

  17. #17
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    ive modified my code to this but im getting some syntax errors that i dont know how to fix.

    Java Code:
    package saver1;
    
    import java.applet.*;
    import java.awt.*;
    import java.util.Random;
    
    public class saver1 extends Applet {
    
        int width, height;
        int i = 0;
        Thread t = null;
        boolean threadSuspended;
        Random rnd = new Random();
    
        public void init() {
            width = getSize().width;
            height = getSize().height;
            setBackground( Color.black );
        }
    public void paint( Graphics g ) {
          g.setColor( Color.green );
          for ( int i = 0; i < 100; ++i ) {
        	  int x1 = rnd.nextInt( getSize().width );
              int y1 = rnd.nextInt( getSize().height );
              int x2 = rnd.nextInt( getSize().width );
              int y2 = rnd.nextInt( getSize().height );
             
        	  [U]g.drawLine( x1, y1, x2, y2 ); // <- SYNTAX ERROR[/U]
          }
        
    
        public void start() {
            if ( t == null ) {
                [U]t = new Thread( this ); // <- SYNTAX ERROR[/U]
                threadSuspended = false;
                t.start();
            }
            else {
                if ( threadSuspended ) {
                    threadSuspended = false;
                    synchronized( this ) {
                        notify();
                    }
                }
            }
        }
       
        public void run() {
            try {
                while(true) {
                    ++i;
                    if(i ==10) {
                        i = 0;
                    }
                    
                    if(threadSuspended) {
                        synchronized(this) {
                            while(threadSuspended) {
                                wait();
                            }
                        }
                    }
                    repaint();
                    t.sleep(1000);
                }
            }
            catch(InterruptedException ex) {
                
            }
        }
    }

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Urgle View Post
    so basicly what youve done is made it pause after each line and then call repaint to paint the next line?
    Yes, you got the point. That's the final thing we have do is.

  19. #19
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    hmm i think i fixed the syntax but now it just updates once a second, still doesnt draw one at a time
    Last edited by Urgle; 11-12-2008 at 04:42 AM.

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Urgle View Post
    still stumped on the syntax errors above
    Yes you have done couple of mistakes in your code.

    1. You didn't implement the Runnable interface.

    Java Code:
    public class saver1 extends Applet implements Runnable {
    2. Misses the ending } in paint(Graphics g) method.

    Correct them and see what happen.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. Demonstration of drawing lines in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:27 PM
  3. Replies: 0
    Last Post: 04-06-2008, 07:45 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. how to edit lines.
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:41 AM

Posting Permissions

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