Closed Thread
Results 1 to 6 of 6

Thread: "Easy" Math.random() problem

  1. #1
    Darkone85 is offline Learning Programmer
    Join Date
    Nov 2009
    Location
    Karlstad, Sweden
    Posts
    30
    Rep Power
    0

    "Easy" Math.random() problem

    Hey, im new to programming and studying at the university in Karlstad Sweden and have this assignment to make some blobs talk, wink, move and stuff.

    I have these classes (and some more thats not important):

    Code:
    /**
     * Chapter 3: BlobApp.java
     * Our first interactive program.
     * Displays two "Blobs" that respond to mouse clicks.
     * @author Manne
     */
    import wheels.users.*;
    
    public class BlobApp extends Frame {
    	private WinkingBlob _winkingBlob;
    	private TalkativeBlob _talkativeBlob;
    	private HappyBlob _happyBlob;
    	private SadBlob _sadBlob;
    	private ShyBlob _shyBlob;
    	
    	public BlobApp() {
    		super();
    		_winkingBlob = new WinkingBlob(300, 300);
    		_talkativeBlob = new TalkativeBlob(400, 200, "I'm so happy!");
    		_happyBlob = new HappyBlob(600, 400, "I'm always happy! ^^");
    		_sadBlob = new SadBlob (100, 100, "I'm always sad :(");
    		_shyBlob = new ShyBlob (200, 200);
    	}
    	
    	public static void main(String [] args) {
    		BlobApp app = new BlobApp();
    	}
    }
    ----------------

    Code:
    /**
     * Chapter 3: Blob.java
     * models a creature with two eyes.
     * @author Manne
     */
    
    import wheels.users.*;
    
    public class Blob extends Ellipse {
    	protected Ellipse _leftEye, _rightEye;
    	
    	public Blob(int x, int y) {
    		super(x, y);
    		_leftEye = new Ellipse(java.awt.Color.black);
    		_rightEye = new Ellipse(java.awt.Color.black);
    		this.setSize(100, 100);
    		_leftEye.setSize(30, 30);
    		_rightEye.setSize(30, 30);
    		_leftEye.setLocation(this.getLocation().x+22, this.getLocation().y+10);
    		_rightEye.setLocation(this.getLocation().x+47, this.getLocation().y+10);
    }
    	
    	public void mousePressed(java.awt.event.MouseEvent e) {
    		this.setFillColor(java.awt.Color.blue);
    	}
    	
    	public void mouseReleased(java.awt.event.MouseEvent e) {
    		this.setFillColor(java.awt.Color.red);
    	}
    }
    ------------

    Code:
    /**
     * chapter 3: WinkingBlob.java
     * Extends the Blob class to add the ability to wink when clicked on.
     * @author Manne
     */
    
    public class WinkingBlob extends Blob {
    	public WinkingBlob(int x, int y) {
    		super(x, y);
    	}
    	
    	public void mousePressed(java.awt.event.MouseEvent e) {
    		super.mousePressed(e);
    		_rightEye.setSize(30, 5);
    		_rightEye.setLocation(_rightEye.getLocation().x, _rightEye.getLocation().y+15);
    	}
    	
    	public void mouseReleased(java.awt.event.MouseEvent e) {
    		super.mouseReleased(e);
    		_rightEye.setSize(30, 30);
    		_rightEye.setLocation(_rightEye.getLocation().x, _rightEye.getLocation().y-15);
    	}
    }
    ---------


    Code:
    /**
     * Chapter 3: Modifying BlobApp-program and extends the Blob class so there's a new, 
     * "ShyBlob" who moves away when clicked on.
     * @author Manne
     */
    
    public class ShyBlob extends Blob {
    	public ShyBlob(int x, int y) {
    		super(x, y);
    	}
    	
    	public void mousePressed(java.awt.event.MouseEvent e) {
    		//super.mousePressed(e);
    	}
    	
    	public void mouseReleased(java.awt.event.MouseEvent e, ShyBlob _shyBlob) {
    		//super.mouseReleased(e);
    	}
    }
    --- NOW to the problem, how do I use Math.random() to say whenever I click shyblob he will disappear and appear on a new random x&y? I tried reading the API but it didnt tell me much.

    Grateful for answers,
    Manne
    Last edited by ZekeDragon; 03-04-2010 at 08:35 AM. Reason: Remember to put [CODE] tags in your posts with program code!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Darkone85 is offline Learning Programmer
    Join Date
    Nov 2009
    Location
    Karlstad, Sweden
    Posts
    30
    Rep Power
    0

    Re: "Easy" Math.random() problem

    i could use:

    this.setLocation(this.getLocation().x+50, this.getLocation().y+40); under the methods mousePressed/Released, but the thing is that they want us to use Math.random() and i dont know how to.

    Tried to this.setLocation(Math.random()); but it wont work since it uses double

    gaaaah!

  4. #3
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: "Easy" Math.random() problem

    Code:
    public void mousePressed(java.awt.event.MouseEvent e)
    {
        int[] nums = { (int) (Math.random() * (SCREEN_X - 50) + 50),
                       (int) (Math.random() * (SCREEN_Y - 50) + 50) };
        this.setLocation(nums[0], nums[1]);
    }
    That should work fine. Go ahead and give it a shot.
    Wow I changed my sig!

  5. #4
    Darkone85 is offline Learning Programmer
    Join Date
    Nov 2009
    Location
    Karlstad, Sweden
    Posts
    30
    Rep Power
    0

    Re: "Easy" Math.random() problem

    Well, it does work in one way and i think i understand it too. U create an vector with 2 elements which u cast from doubles to ints and says that setLocation= place [0] and [1] right..?

    Only, since i need (?) to create local variables for screen_x and_y and initialize them to something the blob will always jump to those x&y and then just hop around 50x50 for each other click.

    Maybe I can initialize them to = "between max and min" somehow?

  6. #5
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: "Easy" Math.random() problem

    Actually the reason I did all those maths was so there were 50 pixels from each border, so if the screen were, say, 800 x 600 pixels, it'd only select a region from 50 - 750 on the X coordinate and 50 - 550 on the Y coordinate. If you test it with those numbers set for SCREEN_X and SCREEN_Y, that's what will be returned. I chose 50 pixels because the blobs size is 100x100, but now that I think about it, the blobs location would be on the top left corner of the blob, not the center:
    Code:
    int[] nums = { (int) (Math.random() * (SCREEN_X - 100)),
                   (int) (Math.random() * (SCREEN_Y - 100)) };
    And the reason I made it an array like that was basically for Zero, One, Infinity. It'd be better to use a loop and DRY, but that'd require a little more writing and wasn't the point of the post.
    Wow I changed my sig!

  7. #6
    Darkone85 is offline Learning Programmer
    Join Date
    Nov 2009
    Location
    Karlstad, Sweden
    Posts
    30
    Rep Power
    0

    Re: "Easy" Math.random() problem

    I seeee! I just love it when I understand new concepts.

    The thing i had problems was that to use math.random in the same line as this.setLocation - which is impossible? Only way you get around it, is like this, casting it to int instead? I tried with .floor and so on.

    Well, thank you young Skywalker. Ill be back!

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Logic for a "Random chat" like Chatroulette
    By Chicohuman in forum General Programming
    Replies: 0
    Last Post: 01-16-2011, 07:39 AM
  2. Replies: 2
    Last Post: 11-01-2010, 11:52 PM
  3. "random" number generator is not random?
    By schwza in forum PHP Development
    Replies: 1
    Last Post: 06-04-2010, 01:58 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts