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); } }
--- 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.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); } }
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!
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!
That should work fine. Go ahead and give it a shot.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]); }
Wow I changed my sig!
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?
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:
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.Code:int[] nums = { (int) (Math.random() * (SCREEN_X - 100)), (int) (Math.random() * (SCREEN_Y - 100)) };
Wow I changed my sig!
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!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks