Computer Science Canada help - Simple Matrix-like Code |
Author: | whoareyou [ Thu May 12, 2011 4:01 pm ] |
Post subject: | help - Simple Matrix-like Code |
Basically, I have to write a program by using arrays to create a "Matrix"-like animation. The hints that my teacher gave us were that the falling characters, their x coordinates, and their y-coordinates should be stored in arrays. His example program is attached. I just don't know where to start. I know for sure that I will need an array for the characters that I will use. Any help is appreciated ![]() ps. if I have a string array, will I have to go through each character like this? : {"a", "b", "c", "d", "e" ...} |
Author: | Zren [ Thu May 12, 2011 6:38 pm ] |
Post subject: | RE:help - Simple Matrix-like Code |
Make a new object with a string and two numbers for the x,y coordinates. Then use an ArrayList as you'll be adding and removing from it often (or simply reseting a fixed number of characters). The hints that you should be using an object class are when you need to have two or more arrays for a similar thing. If you haven't learnt objects then stick with what your teach mentioned. For the letters... Check out the Random. And googling "random letter java" gives some need tricks: http://www.free2code.net/forums/view/topic7554.html char ch = (char)(random.nextInt('Z'-'A'+1)+'A') Alternatively you could have a chracter map and using a random index from it like you were going after. String chars = "abcdABCD"; chars.substring(1,2) // = b String[] chars = "absdfas".split("|"); // Split all characters. chars[1] Googling "java draw string" or the graphics class should aid you. And for gods sake learn double buffering. Your teachers program made me want to hurl with all that flickering. |
Author: | whoareyou [ Thu May 12, 2011 6:53 pm ] |
Post subject: | RE:help - Simple Matrix-like Code |
So, I kind of used a for loop after it generates a random letter and random x-coordinate to begin with ... the y-coordinate changes with the for-loop. But, by doing this, only one character shows up on the screen, and then once it reaches the bottom, the program ends. I can solve that by looping the program infinitely (probably with a while-loop) but how do I display more than one character on the screen at once ? Oh, and I guess I could random the y-coordinate to, so it doesn't all start from the top each time. |
Author: | Zren [ Thu May 12, 2011 7:02 pm ] |
Post subject: | RE:help - Simple Matrix-like Code |
Try setting the initial y value as a negative (minimally top-fontHeight) so it appears that the character floats down. Use a for loop to cycle through each letter per frame. List<String> letters = new ArrayList<String>(); letters.append("M"); // Add string to list while (true) { // Start of frame // Clear screen for (String letter : letters) { // draw letter } //End of frame } |
Author: | whoareyou [ Thu May 12, 2011 7:16 pm ] | ||
Post subject: | Re: help - Simple Matrix-like Code | ||
lol, i have no idea what you're talking about. here's my code : no arrays, no nothing yet, just something to start with.
how am I supposed to implement arrays, and show many characters on the screen at once and make them animate?? |
Author: | Zren [ Thu May 12, 2011 11:11 pm ] |
Post subject: | RE:help - Simple Matrix-like Code |
Right now you're only dealing with one counter (the variable i). You need to get around having more than one counter going at once. Here's a little exercise to help. Using a loop of your choice, add 1 to each of the variables, then print the value of the variables to the screen. int a = 4, b = 6, c = 10; Now try it using an array. int arr = new int[] { 4, 6, 10}; |
Author: | whoareyou [ Fri May 13, 2011 7:10 pm ] | ||
Post subject: | Re: help - Simple Matrix-like Code | ||
I did your exercise for practice ... perhaps you could tell me how this relates to the matrix?
|
Author: | whoareyou [ Fri May 13, 2011 8:27 pm ] | ||
Post subject: | Re: help - Simple Matrix-like Code | ||
So I came up with something, but it isn't working properly. I mean, it has characters and it goes down the screen but it seems as if the text is right-justified. ![]() This is my code:
|
Author: | Zren [ Sat May 14, 2011 5:55 am ] |
Post subject: | RE:help - Simple Matrix-like Code |
Here: String str2 = str1.substring (nchars [i]); Your're using: http://download.oracle.com/javase/6/docs/api/java/lang/String.html#substring(int) instead of: http://download.oracle.com/javase/6/docs/api/java/lang/String.html#substring(int, int) As for the practice thing, your answers weren't what I was intending, but you understood the logic in your matrix code. |
Author: | whoareyou [ Mon May 16, 2011 3:19 pm ] |
Post subject: | RE:help - Simple Matrix-like Code |
Ah yes, thank you very much! |