Hey all, I've been working on this program for a few days now and i cant seem to get the animation aspect of it to work.. Any help would be greatly appreciated
------MAIN TAB-----
import javax.swing.JOptionPane;
int numSmileys;
int noOfMousePresses = 0;
int maxCircles;
boolean moving = false;
Smiley blueSmiley = new Smiley();
// add any method you define here
//setup
void setup() {
background(255);
size(500, 500);
smooth();
do {
String input = JOptionPane.showInputDialog("Enter number of Smileys (4-8)");
numSmileys = Integer.parseInt(input);
}
while (numSmileys < 4 || numSmileys > 8);
}
//draw
void draw() {
//background(255);
if (noOfMousePresses > numSmileys)
blueSmiley.move();
}
//any other mouse / key event methods
void mousePressed() {
noOfMousePresses++;
if (noOfMousePresses <= numSmileys) {
blueSmiley.display();
}
}
------Class Tab-----
class Smiley {
float speedX, speedY;
float x, y, dia, eye, mouth;
float moveX, moveY;
boolean alive = false;
boolean moving = false;
//constructor, with values set
Smiley() {
// values of data members
dia = 60;
eye = 10;
mouth = 15;
speedX = (int)random(1, 4);
speedY = (int)random(1, 4);
}
void display() {
x=mouseX;
y=mouseY;
stroke(1);
fill(255);
ellipse (x, y, dia, dia);
fill(0);
line(x-mouth, y+eye, x+mouth, y+eye);
fill(0, 0, 255);
noStroke();
ellipse (x-mouth, y-eye, eye, eye);
ellipse (x+mouth, y-eye, eye, eye);
}
void setAlive() {
alive = true;
}
void setMoving() {
moving = true;
}
void move() { //actual moving code goes here
blueSmiley.setMoving();
x+=speedX;
y+=speedY;
print("Working!");
//smiley bounces off walls
if (x>width-dia/2 || x<dia/2) {
speedX = speedX*-1;
}
if (y>height-dia/2 || y<dia/2) {
speedY = speedY*-1;
}
}
}
So basically what im trying to do is to have a user input a number (4-8). Then with that number create that many 'smileys'. Once the mouse has been pressed AFTER all the smileys have been generated. I want them to move in random directions, and bounce of walls. Eventually i would like to code them to ensure they collide.
For example, user inputs '4', the first 4 mouse presses will generate 4 smileys, the 5th mouse press will cause them to move.
If anyone would like any extra information to help me solve this, do not hesitate to ask :D.
I was casually doing a practical task for my class until i get a major stone wall (as usual). Although i think i've managed to do all of it, its not 100% complete. Any help would be greatly appreciated :).
Write the class definition for StockItem containing quantityLeft and unitPrice, containing -
1. Default constructor //I believe this is done
2. Parameterized constructor (with validation checks performed) //and this - but without validation checks.
3. display() method //almost there
4. getTotalPrice() method //almost there
5. isMoreExpensiveThan(StockItem other) method (comparison based on unit price) //almost there
Also write a client code that creates an object myItem that has 8 quantities left @ $4.5 per
item, yourItem that has 6 quantities left @ $5.6 per item, and invoke each of the methods
on myItem, passing yourItem as a parameter to isMoreExpensiveThan method.
class StockItem {
int quantityLeft;
float unitPrice;
//default constructor
StockItem() {
quantityLeft = 0;
unitPrice = 0;
}
//parameterized constructor 1
StockItem(int _quantityLeft, float _unitPrice) { //StockItem m =
I've been having some trouble with my program and getting it to do a few things.. basically i need each seperate ellipse (4 of them) to fade out when a diameter of 200 is reached, and also, if possible, for the latter ellipse drawn be thinner than the previous ones.. (for example - first ellipse has a strokeWeight(5) second one has a strokeWeight(4), etc..)
I've managed to get the fade & strokeWeight to work - although my code seems... bulky. I've tried endlessly to convert it into a loop but have failed :(. Below is my code: (Yes, its really messy - Sorry about that!)
boolean start = false;
float dia;
int Xco;
int Yco;
int fade;
void setup() {
size (400, 400);
background(255);
smooth();
}
void mousePressed() {
start = true;
dia = 0;
Xco = mouseX;
Yco = mouseY;
fade = 255;
strokeWeight(0);
}
void draw() {
background(255);
if (start == true) {
{
strokeWeight(5); //Circle 1
stroke(0, fade);
ellipse( Xco, Yco, dia, dia);
if (dia > 50) { // Circle 2
strokeWeight(4);
stroke(0, fade);
fade--;
ellipse( Xco, Yco, dia-50, dia-50);
}
if (dia > 100) { //Circle 3
strokeWeight(3);
stroke(0, fade);
fade--;
ellipse( Xco, Yco, dia-100, dia-100);
}
if (dia > 150) { //Circle 4
strokeWeight(2);
stroke(0, fade);
fade--;
ellipse( Xco, Yco, dia-150, dia-150);
}
dia+=2;
}
}
}
Yes this is for an assignment - and just as a side note to my classmates who've been copying the adjusted code off of here every week and handing it in as your own... shame on you!
I've embarked on a new task today and im having some difficulty trying to implement the code for it.. Im trying to do 2 things.
#1 - when a key is pressed (lets say 'g') i want an just one ellipse to be drawn randomly in the window (with a fixed diameter).
#2 - I have already created the code when the mouse is pressed an ellipse will roll across the screen.. now if the rolling ellipse comes in contact with the first ellipse i want only the rolling ellipse to disappear.. and when the mouse is pressed again the rolling ellipse to reappear at its starting location and begin rolling again.
I've been trying to write a method which checks the users input (a word) with a list of letters I have stored in a string. If the word entered contains the letters in the string I want it to return true, otherwise false. Below is my futile attempt at it :S
boolean IsMirrorImage(String input){
String mirror = "AHIMOTUVWXYimnouvwx";
for (int i=0; i<input.length(); i++){
input.charAt(i);
if(input.(has any value within String mirror))
return true;
else
return false;
}
}
I'm wondering if the String mirror is written correctly and what im trying to do with it is actually possible. All help would be appreciated,
I was trying to do something using strings but i've hit a major stone wall..
What i need to is create a few methods and strings which
inputs a string from the user, and displays “Mirror image!” if the String is a mirror image
, and “Meh, another boring image :(” otherwise.
For example "madam" is not considered a mirror image because of the "d" however it IS a palindrome.
A mirror image would be a word such as "HOIOH" & "lol"
There is also a list of letters which, if mirrored would be considered true in this particular case... these are:
String input = JOptionPane.showInputDialog("Enter your word"); if (IsMirrorImage(input)==true) { text ("Mirror image!", width/2, height/2); } else { text("Meh, another boring image :(", width/2, height/2); }
}
I have created 2 methods to aid in my design and they are also below..
//reversing the string entered
String reverse(String input) {
String result = "";
for(int i=0; i<input.length(); i++)
result = input.charAt(i)+result;
return result;
}
//checking if its a palindrome using the above method
boolean isPalindrome(String input) {
if(input.equals(reverse(input)))
return true;
else
return false;
}
I know i have severely butchered the IsMirrorImage method so if anyone could give me some advice instead of saying "Incorrect code! Heres the right code!" it would be greatly appreciated.