Results 1 to 4 of 4
  1. #1
    aiman is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default Draw more than one line on run time

    I'm trying to show an empty form then I'll run a loop to draw some lines!
    How can I take the loop out of the paint() method? and make the program repaints itself everytime I add a new line to it?

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class ack 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 );
          }
       }
    }

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

    Default

    You could create an ArrayList of points or Line2D's and then iterate through the points or Line2D's in the paint method. If you want to create a new line you create a new Line2D object, add it to the ArrayList and then instruct the GUI to repaint itself.

  3. #3
    aiman is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks

    how ca I make the GUI repaint itself

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

    Default

    I'm not 100% sure for AWT. I use Swing (which I recommend that you use as well), and with Swing we call repaint() on the component. Perhaps you can do the same with AWT components, but again, don't hold me to this.

Similar Threads

  1. Replies: 4
    Last Post: 09-10-2009, 11:18 AM
  2. How to: draw line between 2 nodes of 2 trees
    By kholostoi in forum Java 2D
    Replies: 1
    Last Post: 09-03-2009, 08:10 PM
  3. When do we draw the line for loose coupling.
    By h8alfred in forum Advanced Java
    Replies: 0
    Last Post: 03-27-2009, 04:05 AM
  4. How to Draw line in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:08 AM
  5. How to draw a thick line
    By johnt in forum Java 2D
    Replies: 1
    Last Post: 05-31-2007, 05:27 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
  •