Results 1 to 3 of 3
  1. #1
    pranav21_1983 is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default need some urgent help for drawing random points inside a rectangle

    Hi,

    I'm working on a project and I'm stuck in this....
    I need to build an applet and when that loads, enter an integer in a textfield and plot (whatever will be that # in the textfield - say 100) random points in a (500X300) rectangle.

    The problem I'm facing is that when I click the "Draw" button then all the points should be plotted in one go inside the rectangle, but right now what's happening is that I've to click the "Draw" button 1-by-1 to plot those points.

    Here's the code I've got so far. Please somebody help me out on this urgently. I'll really appreciate your help.

    Java Code:
     import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.Point;
    import java.awt.Graphics2D;
    import java.awt.geom.*;
    
    public class MasterPaper extends JApplet implements ActionListener
    {
        JTextField drawPoints = new JTextField("0", 5);   /*TextField*/
        JLabel l = new JLabel("Enter number of sensor nodes: ");
        Random r = new Random();
        Point p = new Point();
        Vector Points = new Vector();
        JButton b;
        int num_points = 0, x = 0, y = 0;
        
        public void init()
        {
            Container content = getContentPane();
            content.setLayout(new FlowLayout());
            setBackground(Color.WHITE);
            content.add(l);
            b = new JButton("Draw Nodes");
            drawPoints.setEditable(true);
            content.add(drawPoints);       
            content.add(b);
            b.addActionListener(this);
        }
        
        public void paint(Graphics g1)
        {
            super.paint(g1);
            Graphics2D g2 = (Graphics2D) g1;
            g2.setColor(Color.BLACK);
            Rectangle2D.Double rect = new Rectangle2D.Double(10, 50, 500, 300);
            g2.draw(rect);
            Cross c = new Cross();
            num_points = Integer.parseInt(drawPoints.getText());
            for (int i = 1; i <= num_points; i++)
            {
                c.x = Math.abs(r.nextInt(50));
                c.y = Math.abs(r.nextInt(50));
                Points.addElement(c);
            }
            //final int shapeRadius = 5;
            int numShapes = Points.size();
    
            for (int i = 1; i <= numShapes; i++)
            {
                c = (Cross) Points.elementAt(i);
                c.draw(g1);
            }   
        }
        
        public void actionPerformed(ActionEvent e) 
        {
            if (e.getSource() == b)
                repaint();
        }
    }
    
    class Cross 
    {
        static public final int shapeRadius = 5;
        Color color;
        int x;
        int y;
       
        void draw(Graphics g) 
        {
            g.setColor(this.color);
            g.drawString("*", this.x, this.y);
            //g.drawLine(this.x - shapeRadius, this.y, this.x + shapeRadius, this.y);
            //g.drawLine(this.x, this.y - shapeRadius, this.x, this.y + shapeRadius);
        }
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 04-10-2010 at 07:24 PM. Reason: Moderator Edit: Code tags added

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

    Default

    A couple of notes and suggestions:

    1) First off, welcome to the forum.
    2) Please use code tags when posting code here. I've done this for you for your post above, and to learn how to do this yourself with your next post, please read the link in my signature below.
    3) Please avoid using "Urgent" and similar statements in your post. Yes, it may be urgent to you, but not to us, and no one likes to feel rushed or pushed, least of all volunteers who might otherwise help you.

    Best of luck, and again, welcome!

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

    Default

    Please see some comments in your code.

    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.Point;
    import java.awt.Graphics2D;
    import java.awt.geom.*;
    
    @SuppressWarnings({"unchecked", "serial"})
    public class MasterPaper extends JApplet implements ActionListener {
      JTextField drawPoints = new JTextField("0", 5); 
      JLabel l = new JLabel("Enter number of sensor nodes: ");
      Random r = new Random();
      Point p = new Point();
      Vector Points = new Vector();
      JButton b;
      int num_points = 0, x = 0, y = 0;
    
      public void init() {
        Container content = getContentPane();
        content.setLayout(new FlowLayout());
        
        // you should set the background for the contentPane, not the JApplet.  Better still
        // would be to set the background of a JPanel that the drawing is done in 
        // (see below).
        setBackground(Color.WHITE);  
        
        content.add(l);
        b = new JButton("Draw Nodes");
        
        // this is the default setting, so not necessary.
        drawPoints.setEditable(true);
        
        content.add(drawPoints);
        content.add(b);
        b.addActionListener(this);
      }
    
      // you should avoid painting directly in the JApplet. 
      // Much better is to use a JPanel and draw in its paintComponent method, and then
      // add this JPanel to the JApplet's contentPane.
      public void paint(Graphics g1) {
        super.paint(g1);
        Graphics2D g2 = (Graphics2D) g1;
        g2.setColor(Color.BLACK);
        
        // avoid the magic numbers 10, 50, 500, 300.  These should be constants for your app
        Rectangle2D.Double rect = new Rectangle2D.Double(10, 50, 500, 300);
        g2.draw(rect);
        
        // this code below is program logic and does not belong in the paint or paintComponent method.
        // instead your app should have a list of Cross objects, List<Cross>, or an array of
        // Cross objects, Cross[], that are filled in the non-paint portion of your program.
        // paint or better a JPanel's paintComponent method should do nothing more than iterate
        // through the list or array and paint the Cross objects.
        Cross c = new Cross();
        num_points = Integer.parseInt(drawPoints.getText());
        for (int i = 1; i <= num_points; i++) {
          c.x = Math.abs(r.nextInt(50));
          c.y = Math.abs(r.nextInt(50));
          Points.addElement(c);
        }
        // final int shapeRadius = 5;
        int numShapes = Points.size();
    
        // this is what your paint / paintComponent method needs.  But remember that arrays and lists
        // start at 0.  So i should go from 0 to < numShapes.
        for (int i = 1; i <= numShapes; i++) {
          c = (Cross) Points.elementAt(i);
          c.draw(g1);
        }
      }
    
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b)
          
          // here is where your program logic should go.  You should fill your Cross list or array
          // from in here, then call repaint.
          
          repaint();
      }
    }
    
    class Cross {
      static public final int shapeRadius = 5;
      Color color;
      int x;
      int y;
    
      void draw(Graphics g) {
        g.setColor(this.color);
        g.drawString("*", this.x, this.y);
        // g.drawLine(this.x - shapeRadius, this.y, this.x + shapeRadius, this.y);
        // g.drawLine(this.x, this.y - shapeRadius, this.x, this.y + shapeRadius);
      }
    }

    Best of luck and HTH.

Similar Threads

  1. drawing points with mouse
    By josephdcoleman in forum New To Java
    Replies: 4
    Last Post: 03-03-2009, 01:39 AM
  2. Drawing points on a JPanel
    By josephdcoleman in forum New To Java
    Replies: 6
    Last Post: 02-25-2009, 04:47 PM
  3. Random Points inside a Polygon
    By nidhirastogi in forum Advanced Java
    Replies: 1
    Last Post: 09-23-2008, 04:28 AM
  4. How To Add A Jbutton Inside A Rectangle
    By SANDY_INDIA in forum AWT / Swing
    Replies: 9
    Last Post: 07-06-2008, 09:06 AM
  5. Replies: 6
    Last Post: 06-12-2008, 10:14 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
  •