Results 1 to 20 of 30
Thread: Random Lines
- 11-12-2008, 03:01 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
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()); } } }
- 11-12-2008, 03:10 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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 ); } } }
- 11-12-2008, 03:11 AM #3
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
What happens when you run it?
- 11-12-2008, 03:19 AM #4
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
how would I incorperate randoms into the g.drawline?
- 11-12-2008, 03:29 AM #5
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
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 ); } } }
- 11-12-2008, 03:33 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Do you know about Threads in Java?
- 11-12-2008, 03:34 AM #7
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
no, but i know that they have something to do with the path of execution
- 11-12-2008, 03:44 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-12-2008, 03:46 AM #9
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
yea with threads
- 11-12-2008, 03:58 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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-12-2008, 04:00 AM #11
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
since I know almost nothing about threads Im not sure how to make my program use them
- 11-12-2008, 04:03 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 11-12-2008, 04:06 AM #13
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
I understand I need to use threads but how?
- 11-12-2008, 04:13 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-12-2008, 04:14 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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) { } } }
- 11-12-2008, 04:24 AM #16
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
so basicly what youve done is made it pause after each line and then call repaint to paint the next line?
- 11-12-2008, 04:29 AM #17
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
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) { } } }
- 11-12-2008, 04:30 AM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-12-2008, 04:37 AM #19
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
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.
- 11-12-2008, 04:42 AM #20
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Demonstration of drawing lines in SWT
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:27 PM -
How to get the count of all the lines in a file
By Java Tip in forum java.ioReplies: 0Last Post: 04-06-2008, 07:45 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
how to edit lines.
By jason27131 in forum New To JavaReplies: 1Last Post: 08-01-2007, 04:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks