Results 21 to 30 of 30
Thread: Random Lines
- 11-12-2008, 04:44 AM #21
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
- 11-12-2008, 04:49 AM #22
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-12-2008, 04:51 AM #23
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
oh ok sorry, but whats wrong with my program?
-
ah heck. I like Swing and prefer to use a Timer.
Java Code:import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Line2D; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JComponent; import javax.swing.Timer; public class RandomLines { private static final int TIMER_DELAY = 400; private static final int LINE_COUNT_MAX = 100; private List<Line2D> lineList = new ArrayList<Line2D>(); // parallel arrays == bad! private List<Color> colorList = new ArrayList<Color>(); private Random random = new Random(); private JPanel mainPanel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); myPaint(g); } }; private Timer swingTimer; public RandomLines() { mainPanel.setPreferredSize(new Dimension(800, 600)); start(); } public void start() { lineList.clear(); colorList.clear(); swingTimer = new Timer(TIMER_DELAY, new ActionListener() { private int lineCount = 0; public void actionPerformed(ActionEvent e) { if (lineCount < LINE_COUNT_MAX) { int width = mainPanel.getWidth(); int height = mainPanel.getHeight(); lineList.add(new Line2D.Double( random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height))); colorList.add(new Color( random.nextInt(256), random.nextInt(256), random.nextInt(256))); mainPanel.repaint(); lineCount++; } else { Timer timer = (Timer)e.getSource(); timer.stop(); } } }); swingTimer.start(); } private void myPaint(Graphics g) { Graphics2D g2d = (Graphics2D)g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setStroke(new BasicStroke(5)); g2d.setColor(Color.blue); for (int i = 0; i < lineList.size(); i++) { g2d.setColor(colorList.get(i)); g2d.draw(lineList.get(i)); } } public JComponent getComponent() { return mainPanel; } private static void createAndShowUI() { JFrame frame = new JFrame("RandomLines"); frame.getContentPane().add(new RandomLines().getComponent()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }Java Code:import javax.swing.JApplet; public class RandomLinesApplet extends JApplet { public void init() { try { javax.swing.SwingUtilities.invokeAndWait(new Runnable() { public void run() { createGUI(); } }); } catch (Exception e) { System.err.println("createGUI didn't successfully complete"); } } private void createGUI() { RandomLines randomLines = new RandomLines(); getContentPane().add(randomLines.getComponent()); randomLines.start(); } }
- 11-12-2008, 04:56 AM #25
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
oh, thanks for the input fubarable, but I dont know much about swing so honestly i have no clue what half of you code says
Last edited by Urgle; 11-12-2008 at 05:46 AM. Reason: misspelled name >.<
- 11-12-2008, 05:45 AM #26
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
I still need help.
Cross-Posted here:
New To Java - Random Lines
-
If you are still having problems it would be best for you to redefine as specifically as possible, just what is not working in your current code or exactly what you don't understand, and also to paste your latest bit of compilable code.
Remember, your job is to make it as easy as possible for someone else to help you, right?
- 11-12-2008, 05:59 AM #28
Member
- Join Date
- Oct 2008
- Posts
- 30
- Rep Power
- 0
yea it is. Well whats happening is it draws 100 lines all at the same time and then updates every second and draws another 100 lines. What I want it to do is draw lines one at a time. The swing code you gave me worked, but I dont understand it. Heres what I have so far ( not swing ):
Java Code:package saver1; import java.applet.*; import java.awt.*; import java.util.Random; public class saver1 extends Applet implements Runnable { 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 ); g.drawLine( x1, y1, x2, y2 ); } } 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) { } } }
-
It's doing just what you tell it to do. Each time you call repaint (or any time the applet repaints itself, say by resizing the applet) you tell it in a for loop to paint 100 lines.
Then you have a background thread where you pause for 1 second, but never add one line, never increment a counter and test it to see if it's greater than 100.
You also have program logic within your paint method. Suggestion: hold an array of either Line2D (I used an ArrayList of these) or create a class yourself that holds 4 ints to represent the coordinates of the two points that define each line, and in your background thread, add a new line to this array with each loop, and call repaint. The paint method should just iterate through the array, drawing lines for every line that is defined in the array.
- 11-12-2008, 03:42 PM #30
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
It's all about this.
As I said earlier after drawing a line, hold a second and draw another one. That's the final task you want to do. So what I'm doing in my code is call repaint() after an each second.Java Code: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 ); // <- SYNTAX ERROR } }
Look at your paint() method. you are drawing 100 lines in each call of paint(). So what you should do? Remove the loop.
Java Code:public void paint( Graphics g ) { g.setColor( Color.green ); 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 ); }
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