Results 1 to 2 of 2
- 09-16-2008, 10:45 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 1
- Rep Power
- 0
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!
- 09-16-2008, 10:57 PM #2
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
-
Assign a keyboard key to a JButton.
By gszauer in forum AWT / SwingReplies: 1Last Post: 12-15-2007, 10:42 PM -
Randomly accessing files
By Java Tip in forum Java TipReplies: 0Last Post: 12-12-2007, 10:46 AM -
Accessing a file randomly
By Java Tip in forum Java TipReplies: 0Last Post: 11-10-2007, 08:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks