Viewing 25 posts - 1 through 25 (of 25 total)
  • coding bods – Java help please. (processing.org)
  • makecoldplayhistory
    Free Member

    I’ve written the code, of which I’m rather proud as it wasn’t long ago I was struggling with ‘Hello World’.

    It’s in processing.org which uses a simplified java language but still recognisable as java.

    My problem is with the text(s) and I guess my if / else loop. The more complex one works where random text from a string passed into an array displays with random x / y co-ordinates and fades out.

    The simple code for displaying “this text will be instructions” takes on the behaviour of the more complicated one but I just want it to be a simple

    if (millis() < 5000) {
    text(“this text will be instructions”,width/3,height/3);
    }

    Instead, it fades in and out with the random placement of the other text.

    Here’s a link to the whole code.

    http://hastebin.com/emewufuwah.avrasm

    Here’s the relevant code. I think it’ll make sense to all of you with far more knowledge than me!

    if (millis() < 5000) {
    text(“this text will be instructions”,width/3,height/3);
    }
    else if (millis() – lastTime >= timeToDisplay) // if statement to allow text to fade in and out.
    {
    count = int(random(myArray.length)); // use length of array to get word random within its length (size)
    textSize(random(35, 50)); // random sized text – set min / max values
    randomX = random(100, 500); // random placement of text
    randomY = random(100, 500);
    alphaSetting = 255; // set alpha setting of 255 = fully ‘there’
    lastTime = millis();
    }
    fill(white, alphaSetting); // white text
    text(myArray[count], randomX, randomY); // chose word from myArray – an array of the words from
    alphaSetting -= a; // fade out of text by decrememtning alpha

    Thanks to anyone with the inclination to help on a Sunday!

    jam-bo
    Full Member

    Why do you need all that code in the else if section if you don’t want to do what it does?

    makecoldplayhistory
    Free Member

    I want the text from myArray to appear in a random position and fade out and keep doing so for as long as the code is run. The text in myArray is a poem.

    I want instructions to appear for 5 seconds and then disappear. The instructions are the camera controls.

    At the moment, both texts do the fade out / random placement.

    jam-bo
    Full Member

    So use a while millis() < 5000

    And then use your other code?

    Edit: I should add, I’ve never used Java or processing. Python and matlab is my thing.

    makecoldplayhistory
    Free Member

    Nope. Using while still makes both sets of text fade in and out.

    Thanks for the suggestion though jam

    GlennQuagmire
    Free Member

    At which point is millis() given a value?

    makecoldplayhistory
    Free Member

    millis() returns the time since the program was run so for the first text, the instructions, the text should appear for the first five seconds that the program is run.

    If I removed

    if (millis() < 5000) {
    text(“this text will be instructions”,width/3,height/3);
    }

    then the code runs as expected. If I write the simplest of code with that if statement then the “this text will be instructions” appears as expected ie. at location width/3 and height/3 and for the first 5 seconds that the code is run.

    It’s when the two are combined that the instructions don’t follow the if millis() < 5000 … instead they fade away and are placed randomly.

    GlennQuagmire
    Free Member

    Can you echo out the value of millis()?

    I’m assuming it’s an integer but brackets suggest it’s an array.

    These lines will always run since it’s outside of then if..then..else bit

    fill(white, alphaSetting); // white text
    text(myArray[count], randomX, randomY); // chose word from myArray – an array of the words from
    alphaSetting -= a; // fade out of text by decrememtning alpha

    makecoldplayhistory
    Free Member

    what do you mean by echo out the value of millis() Glenn? Does echo out mean comment out?

    If I comment it out from the second text (from myArray) then I get one string per millisecond (or frame, I guess)

    If I use code like below then I get the random string from myArray and also the “this will be instructions” text fading in and out, random placement etc.

    // if (millis() < 5000) {
    text(“this text will be instructions”,width/3,height/3);
    if (millis() – lastTime >= timeToDisplay) // if statement to allow text to fade in and out
    {
    count = int(random(myArray.length)); // use length of array to get word random within its length (size)
    textSize(random(35, 50)); // random sized text – set min / max values
    randomX = random(100, 500); // random placement of text
    randomY = random(100, 500);
    alphaSetting = 255; // set alpha setting of 255 = fully ‘there’
    lastTime = millis();
    }
    fill(white, alphaSetting); // white text
    text(myArray[count], randomX, randomY); // chose word from myArray – an array of the words from
    alphaSetting -= a; // fade

    GlennQuagmire
    Free Member

    Sorry, by echo I mean can you print/display the value in millis()

    But should millis() be defined as an integer rather than an array? I doubt the first if statement will ever evaluate to true since you’re checking an array has a value less than 5000.

    chambord
    Free Member

    It’s not an array it’s a function, it returns an int.

    Personally I’d

    int currentTime = millis();

    At the beginning of the draw loop and check against currentTime in your conditions so you know it’s constant for this draw iteration.

    cam.beginHUD()
    int currentTime = millis();
    if (currentTime < 5000)
    {
        text("this text will be instructions",width/3,height/3);
    }
    else // Else don't show instructions anymore
    {
        if (currentTime - lastTime >= timeToDisplay)
        {
            count = int(random(myArray.length));
            textSize(random(35, 50));
            randomX = random(100, 500);
            randomY = random(100, 500);
            alphaSetting = 255;
            lastTime = currentTime;
        }
        fill(white, alphaSetting); // white text
        text(myArray[count], randomX, randomY);
        alphaSetting -= a;
    }
    cam.endHUD();

    I’m not totally sure what you’re trying to do but I don’t think you want an if else there. If your app has been running less than 5 seconds then show the help text, else you do your other thing.

    I can’t test any of the logic there because I can’t find an online processing editor which as the libraries you’re using.

    Best of luck, hope you are well Mike.

    P.s. If you want to put some code here in the forum, wrap your text in backticks (The key near your escape that looks a bit like an apostrophe). It will give you monospace font and preserve your indenting.

    poly
    Free Member

    You use lastTime before it is assigned a value. I’m not sure if that is causing your problem, because I’m not sure what is actually happening when you run the code.

    Chamborgs code also highlights you are doing stuff outside the elseif on variables that are only set in the elseif?

    Finally you need to understand how text() works. does text(“first”) ; text(“second”); give you two separate instances of text, or one instance just containing second or one instance containing firstsecond – once you get your head round that you will be much better placed to Debug your own code.

    makecoldplayhistory
    Free Member

    Sorry, by echo I mean can you print/display the value in millis()

    But should millis() be defined as an integer rather than an array? I doubt the first if statement will ever evaluate to true since you’re checking an array has a value less than 5000.

    I guess that chambord answered that for you. It is an int == the time the code has been running in milliseconds ie. if the code has run < 5 seconds show “this text will be instructions”

    I’ve written this and added it to my code

    void setup() {
      size(700, 400);
      textFont(createFont("Arial", 30));
    }
    
    void draw() {
      background(255);
      fill(0);
    
      text(millis(), 25,25);
    }

    First, “this text will be instructions” displays for 5000ms, then it moves to strings from the array. The timings work but the issue is that I want the instructions to be in simple plain text. No fading or random placement. Maybe it would have been clearer to have written it as

    if frameCount() < 300
    {
       text("this text will be instructions",width/3,height/3);
    }

    _____________________________________________________________________________

    Chambord – are you the incredibly helpful guy from almost a year to the day who helped with another assignment? If so, how’s the uni going? Hope you’re well too.

    I’m not totally sure what you’re trying to do but I don’t think you want an if else there. If your app has been running less than 5 seconds then show the help text, else you do your other thing.

    Exactly that. The instructions are how to use the mouse to control the camera. After that there’s the arty nonsense (specifically Keats).

    Using your code, the “this will be instructions” stays and the else part never ‘kicks in’.
    ______________________________________________________________________________

    Finally you need to understand how text() works. does text(“first”) ; text(“second”); give you two separate instances of text, or one instance just containing second or one instance containing firstsecond

    I don’t really understand what you mean poly. I know the arguments that text() takes. first it takes the string or character to display, then x, y, z co-ords.

    If you mean, what would this code do

    void setup() {
      size(700, 400);
      textFont(createFont("Arial", 30));
    }
    
    void draw() {
      background(255);
      fill(0);
    
      text(millis(), 50, 100);
      text("second", 50, 100);
      text(frameCount, 100, 200);
    }

    Then it will print both the milliseconds and “second” at the same time and ‘on top’ of each other and then the frame count lower down and across. Because it’s in void draw and not void setup, it’s re-drawn at each new frame so the millis and framecount update as the value changes.

    Thank you for all the replies but I need to move on to SQL and if people who are far better than I am at writing code can’t quickly solve it then it’s time for me to move on. I can’t imagine much (if any) improvement in my grade for the instructions appearing.

    I’ve gone for the old readme.txt in the folder 🙂

    I do appreciate the help though and it’s good to try to justify your code – even if it’s just in your head!

    stevehine
    Full Member

    I’m with chambord – this does what you want – though it’s difficult to say for certain as I’ve had to comment bits out that depend on your image files and so my background isn’t getting cleared 🙂


    cam.beginHUD(); // end peasy cam so that words appear in the screen and aren't changed by 'camera' movement and position
    if (millis() < 5000) {
    text("this text will be instructions",width/3,height/3);
    }
    else
    {
    if (millis() - lastTime >= timeToDisplay) // if statement to allow text to fade in and out.
    {
    count = int(random(myArray.length)); // use length of array to get word random within its length (size)
    textSize(random(35, 50)); // random sized text - set min / max values
    randomX = random(100, 500); // random placement of text
    randomY = random(100, 500);
    alphaSetting = 255; // set alpha setting of 255 = fully 'there'
    lastTime = millis();
    }
    fill(white, alphaSetting); // white text
    text(myArray[count], randomX, randomY); // chose word from myArray - an array of the words from
    alphaSetting -= a; // fade out of text by decrememtning alpha
    }
    cam.endHUD();

    GlennQuagmire
    Free Member

    Yeah, I didn’t know millis() was a function as this isn’t a language I know.

    But I don’t think your far from getting it working – I agree with the other suggestions above as those should work. You just need to re-structure your if…then statement a little bit.

    poly
    Free Member

    mcph,

    “Thank you for all the replies but I need to move on to SQL and if people who are far better than I am at writing code can’t quickly solve it then it’s time for me to move on. I can’t imagine much (if any) improvement in my grade for the instructions appearing.”

    I could write you code to do what I think you want easily. You don’t seem to want advice though (e.g. a while is a much more logical approach, and your else if is probably screwed up).

    It depends if you actually want to understand or just get a mark. Personally I think conditional behaviour and loops, as well as (not) using variables that have no value assigned are probably fairly fundamental things you’ll be marked on – much more important that the user experience itself.

    “I don’t really understand what you mean poly. I know the arguments that text() takes…”

    Well I have a pretty good idea what it should do although I’ve not used processing. My guess is you would learn a lot by methodically working through YOUR code, line by line, and working out what it does (and to which objects) – not what you think it does. A good IDE will let you break point it and check that it actually does what you expect.

    stevehine
    Full Member

    Well I have a pretty good idea what it should do although I’ve not used processing. My guess is you would learn a lot by methodically working through YOUR code, line by line, and working out what it does (and to which objects) – not what you think it does. A good IDE will let you break point it and check that it actually does what you expect.

    I tend to agree (though I didn’t really want to stick a boot in on someone who’s clearly got deadlines + associated stress)

    if people who are far better than I am at writing code can’t quickly solve it then it’s time for me to move on. I can’t imagine much (if any) improvement in my grade for the instructions appearing.

    Take your time. Plan your code. Lay it out in a more logical manner. It’s java – use classes and methods; not just one big global set of variables. An approaching deadline can make it hard to concentrate properly for some people; but the logic error in your if statements was fairly easy to spot. And I’d never used processing before today; and I avoid java as a matter of course.

    Programmers tend to be fairly helpful; but the quickest way of getting ignored is to respond to someone who’s trying to help with “No; that doesn’t work – ah screw it”

    my 2p 🙂

    p.s. Coding is *fun*. No; really it is 🙂

    chambord
    Free Member

    are you the incredibly helpful guy from almost a year to the day who helped with another assignment? If so, how’s the uni going?

    I’ve never been described in such a way but yes that was me :). Uni didn’t work out in the end – I was enjoying bits of it (Programming and teaching) but not the bit that really counted (Research) so I packed it in and got a job doing something I like – writing code..!

    @poly

    In processing the setup() function is called once at the start of the program. The draw() function is called once per UI loop iteration. A bit like:

    main()
    {
      setup();
      while (userHasntClosedWindow())
      {
        draw();
      }
    }

    So loops aren’t required. Also, the lastTime variable is initialised in setup().

    A good IDE will let you break point it and check that it actually does what you expect.

    This is good advice – Intellij is a good free Java IDE which will work well with processing. Here is a blog telling you how to set it up. If you run your code in an IDE you can pause your program at any point, add breakpoints and see what each variable contains, all sorts! Basically you can follow the execution of a program step by step, which is really handy if you’re not sure what some code is doing or if you are learning.

    makecoldplayhistory
    Free Member

    I could write you code to do what I think you want easily. You don’t seem to want advice though (e.g. a while is a much more logical approach, and your else if is probably screwed up).

    Good for you poly. Big hi-5 coming your way. Not sure why you think I don’t want advice. I tried using a while loop (perhaps I used it incorrectly) after jam bo’s suggestion. The code ran okay but the fade in out behaviour applied to both strings. I followed peoples’ advice using if / else loops but didn;t get the desired behaviour either. Again, I may be implementing it incorrectly but I certainly read replies and advice and tried to use them.

    As chambord replied, the variables are initiated and as far as I can possibly see (for my experience and obviously lack of poly-like abilities), it’s set out logically. We must submit an aesthetic critique along with ou code and also a line-by-line account of it. I’ve done that already and can explain every single line of MY code.

    Using an if loop decrementing alpha over time is a pretty standard way of making the text fade in and out in processing, from what I’ve read over the last week.

    A good IDE will let you break point it and check that it actually does what you expect.

    Thank you and thanks @chambord for mentioning IntelliJ. I’ve used it in the past for ‘proper’ Java but had thought processing had to be written in Processing.

    ______________________________________________________________________________

    Take your time. Plan your code. Lay it out in a more logical manner. It’s java – use classes and methods; not just one big global set of variables.

    This is the first time I have planned my code 🙂 I genuinely did think it was logically planned.

    An approaching deadline can make it hard to concentrate properly for some people; but the logic error in your if statements was fairly easy to spot.

    There are 9 days left to the deadline (although I do have an SQL assignment) but yes, I don’t work best when counting down to a deadline.
    See, I still haven’t spotted it… I’m yet to begin ‘thinking’ like a programmer – hopefully it’ll come.

    Programmers tend to be fairly helpful; but the quickest way of getting ignored is to respond to someone who’s trying to help with “No; that doesn’t work – ah screw it”

    True in all walks of life and I absolutely didn’t mean to be ungrateful after people took the time to reply. It was a more a realistic ‘time to move on’.
    Poly asked “It depends if you actually want to understand or just get a mark”. The latter. Absolutely the latter. I’ve been teaching ESL for nearly a decade and want to be a primary teacher. This means I need my undergrad before my postgrad in teaching. Doing the Creative Computing BSc is purely a means to an end.

    p.s. Coding is *fun*. No; really it is

    You spoke sense until then 🙂
    __________________________________________________________________________

    Glad you’re doing something you enjoy chambord.

    I’ll try IntelliJ.

    Thanks again to all for all of the replies. The deadline is approaching but I’ve left myself more than enough time. I’m moving on today to the final assignment before revision for exams starts, but I’ll have time to come back to this. Hopefully my fresh eyes will help.

    Yesterday, besides being stuck in front of a computer on my birthday, I had (have) a hacking cough, a head-cold, a mild case of the trots and have been working every day on assignments for the last 2 1/2 weeks straight. Think I’m getting rickets from lack of sunlight!

    whatnobeer
    Free Member

    After commenting out the stuff using images and libraries I don’t have, the text stuff seemed to work like you suggested you wanted it to? The poem texts fade in and out and the instructions pop up for the first 5 seconds.

    Not sure your translate call is in the right place, translate related to everything you do after you call it so it moves all of the text, not the spheres.

    Also, ‘a’ isnt a good name for a variable. I know it’s commented but give it a descriptive name, it’ll make it easier to read the code without reading all the comments. Good code should read like a (sort of) like a story, it should be clear from your method and variable names what is happening as much as possible without reading comments.

    Final tip, if you’re still using the processing window to code in, you can call println(‘stuff here’) to send output to the console. Useful for checking if code is being called, what order etc.

    mikewsmith
    Free Member

    Just putting this one out there
    http://www.w3schools.com/sql/

    Having read a few of your help posts I’m going to try and make a couple of constructive comments…

    People will help but most people who can/do on the topics have learnt some of the skills to make life easier.
    Writing out variables etc. as a debug means you know if things are being called and if your maths/declarations etc are right.
    Set by step debugging (line by line till it goes wrong) will get you a long way.
    Breaking things out and making sure things work in isolation is good before adding the mess of loops, if, else etc.

    SQL will be fun but you need to become analytical thorough and meticulous. Some things wont work and it’s best to get them in small chunks than try and handle all of it at once.

    Poly asked “It depends if you actually want to understand or just get a mark”. The latter. Absolutely the latter. I’ve been teaching ESL for nearly a decade and want to be a primary teacher. This means I need my undergrad before my postgrad in teaching. Doing the Creative Computing BSc is purely a means to an end.

    Depending on your level of ethics
    https://www.upwork.com/cat/developers/

    makecoldplayhistory
    Free Member

    Which code worked whatnobeer? The else / if? I doesn’t here. I’ll try what you had to do (commenting out as though I don’t have the images / textures / libraries.

    I wonder if it’s to do with the camera and the text being inside the HUD() method. I’ll check it out later.

    Thanks Mikew – w3schools is great.

    I do need to learn to de-bug / check my own code for errors. Definitely a skill I need to get nailed as at the moment, my code either works and I’m happy or it doesn’t and I’m not really sure where to begin looking for errors.

    re. upwork. I have a high level of eithics so wouldn’t use a site like that. Were you looking for work? email in profile lol

    mikewsmith
    Free Member

    not at all, but as your comment was this is a means to an end and your not bothered about understanding how it works just getting an answer it seemed like an alternative to asking for debugging and just getting a solution. Not passing judgement.

    If you don’t learn to debug then basically your asking others to do that for you. One of those things learn how to do it and you will save a lot of time and save the kindness of strangers for when you really need it 😉

    GlennQuagmire
    Free Member

    +1 for debugging – stepping through code to see what’s happening is invaluable. Conditional breakpoints and things like that can save you loads of time.

    I use Visual Studio and being able to view objects and all their values is very useful.

    The most important part of programming – and this applies to all languages – is getting the code designed in an organised, re-usable, manner.

    And feel free to shout if you have any questions!

    footflaps
    Full Member

    I do need to learn to de-bug / check my own code for errors. Definitely a skill I need to get nailed as at the moment, my code either works and I’m happy or it doesn’t and I’m not really sure where to begin looking for errors.

    This is one of the first and most basic skills you need to acquire, otherwise you’re building without foundations and it will never end well…..

Viewing 25 posts - 1 through 25 (of 25 total)

The topic ‘coding bods – Java help please. (processing.org)’ is closed to new replies.