Results 1 to 2 of 2
  1. #1
    newb101 is offline Member
    Join Date
    Sep 2008
    Rep Power

    Default Help! Need to randomly assign boxes

    Hello All,

    I am working on a project for school and I need to be able to randomly assign boxes within the canvas. This is the first programming class I have ever had, and I am so so so lost. The professor hinted at using RandomIntGenerator, but I cannot for the life of me figure out how to use that to distribute the boxes randomly. Any help is VERY much appreciated. Thanks!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    Java Code:
    import java.util.Random;
    public class RandomBoxes {
        static Random seed = new Random();
        public static void main(String[] args) {
            // Inside your painting method...
            int componentWidth = 400;  // getWidth()
            int componentHeight = 300; // getHeight()
            int rectWidth = 75;
            int rectHeight = 50;
            int x = seed.nextInt(componentWidth - rectWidth);
            int y = seed.nextInt(componentHeight - rectHeight);
            System.out.printf("x = %d  y = %d  rectWidth = %d  rectHeight = %d%n",
                               x, y, rectWidth, rectHeight);
            //g.drawRect(x, y, rectWidth, rectHeight);

Similar Threads

  1. Assign a keyboard key to a JButton.
    By gszauer in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2007, 10:42 PM
  2. Randomly accessing files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-12-2007, 10:46 AM
  3. Accessing a file randomly
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 08:15 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