Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30

Thread: Random Lines

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

    Default

    i fixed those but im still having issues

    hmm i think i fixed the syntax but now it just updates once a second, still doesnt draw one at a time

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

    Default

    Quote Originally Posted by Urgle View Post
    hmm i think i fixed the syntax but now it just updates once a second, still doesnt draw one at a time
    Please don't edit posts when you fix it. If someone else read this thread he/she can confused on that. Keep in mind, don't do it again. :)

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

    Default

    oh ok sorry, but whats wrong with my program?

  4. #24
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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();
      }
    }

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

    Default

    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 >.<

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

    Default

    I still need help.

    Cross-Posted here:

    New To Java - Random Lines

  7. #27
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

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

    Default

    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) {
                
            }
        }
    }

  9. #29
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

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

    Default

    Quote Originally Posted by Urgle View Post
    oh ok sorry, but whats wrong with my program?

    It's all about this.

    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
          }
    }
    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.

    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 );
    }

Page 2 of 2 FirstFirst 12

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 Tips
    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
  •