GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Scrolling String - Change of Speed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scrolling String - Change of Speed" Watch "Scrolling String - Change of Speed" New topic
Author

Scrolling String - Change of Speed

Aldoria Robichaud
Greenhorn

Joined: Jun 01, 2010
Posts: 5
Hey,

I have written a script for my Computer Science 120 course. The only problem is that I don't understand why the first string moves fast, and then the other four do not move as quickly. I thought it had to do with the runner.sleep(time); method, so I threw that into every scrolling statement. However, this didn't solve the problem or make the speed identical for all scrolling strings.

I would really appreciate it if someone could take a look at this and help me figure out why it isn't working :p I'm not looking for an answer, I would prefer an explanation

FiveSongs.java


FiveSongs.html


Thanks again

PS. The boolean checkEnd is there for a later part of the script that I need to do :p


I'm not the brightest light bulb. I'm not the sharpest tool in the drawer. I'm not the smartest student in the classroom. I couldn't cut the pickle. But hey, at least I try, right? Or is that a bad thing..
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Hi,

I've rewritten it somewhat (for fun ) and it still runs slow. I think so many redraws are tiresome for the procesor (CPU/GPU). By the way, great song selection!

Code:



SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

We encourage our users to do their own homework (please see DoYourOwnHomework), and we're NotACodeMill. As such, we strive not to provide ready-made answers, but rather provide hints, suggestions, and so on. Particularly when it's explicitly stated this is for a course.
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
I'm Sorry I didn't know that.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Not a problem--no apologies necessary. Maybe posting hints regarding some of the cleanup you did would be helpful--it seemed easier to deal with than the original version.
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Aldoria Robichaud,

it seems that your code somewhat repeats itself. For example the part(s) that move your strings. Is there a way to do that in a different way, with less code, maybe with a flow control statement?

You are using several variables in a connected kind of a way, as if they were a whole. Java is object oriented, try thinking with objects and properties.

Keep in mind, that sleep(long) method that you're invoking is static. That means that it is not invoked on your instance of Thread (the runner variable) but on the Thread class itself. Hope that made sense It is good practice, as far as I understand, to invoke that kind of methods on a 'class level' ie. Thread.sleep(20), result is the same.

Also the thread goes to sleep for about the time you provide, results may vary. You can expect the thread to sleep for 20ms or more.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Perfect :) Thanks for following up!
Aldoria Robichaud
Greenhorn

Joined: Jun 01, 2010
Posts: 5
Hey guys !
Thanks for the help. I got my full applet to work now

And Vanyavchich,
I don't even know what a flow control statement is.. we haven't learned about them yet.

Also, I figured out why the scrolling kept getting slower and slower. It was, in fact, due to all of my 'spammed and unnecessary'

Anyways, I have completed and submitted my final assignment. Here is my the final submitted code:


Now I was just wondering if someone could give me a short example of how this would be done using Objects as Vanyavchich had mentioned. Instead of doing it with all 5 strings, there could be less. I just want to see a better programming style that accomplishes the same thing so I can compare and see the differences, and hopefully it will help me improve myself

Thanks for all the help!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Aldoria Robichaud wrote:I don't even know what a flow control statement is.. we haven't learned about them yet.

Sure you have, you just don't realize it: flow control statements.
Now I was just wondering if someone could give me a short example of how this would be done using Objects as Vanyavchich had mentioned. Instead of doing it with all 5 strings, there could be less. I just want to see a better programming style that accomplishes the same thing so I can compare and see the differences, and hopefully it will help me improve myself

Well, the first thing I'd do is start by looking to see where the repetition is: the first thing I notice is that there are a large number of statements that look exactly alike, but with slightly different values. I take aim at that kind of repetition first, to nail down the essence of what I'm trying to do.

Then I try to isolate *where* that functionality belongs. Martin's solution involved moving song information (title, position, color, visibility) into a Song class, and creating a method that would take a song and move it. Maybe try sketching out something doing that--even in pseudocode. Once you have that, I can repost his answer for comparison.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
Martin Vanyavchich wrote:
Thank you for noticing, David.
 
GeeCON Prague 2014
 
subject: Scrolling String - Change of Speed