
-----------------------------------
whoareyou
Thu May 12, 2011 4:01 pm

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" ...}

-----------------------------------
Zren
Thu May 12, 2011 6:38 pm

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 [url=http://download.oracle.com/javase/1.4.2/docs/api/java/util/Random.html]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.

-----------------------------------
whoareyou
Thu May 12, 2011 6:53 pm

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.

-----------------------------------
Zren
Thu May 12, 2011 7:02 pm

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 letters = new ArrayList();
letters.append("M"); // Add string to list

while (true) {
// Start of frame
// Clear screen
for (String letter : letters) {
// draw letter
}
//End of frame
}

-----------------------------------
whoareyou
Thu May 12, 2011 7:16 pm

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.


import java.awt.*;
import hsa.Console;

public class MatrixCode
{
    static Console c;

    public static void main (String

how am I supposed to implement arrays, and show many characters on the screen at once and make them animate??

-----------------------------------
Zren
Thu May 12, 2011 11:11 pm

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};

-----------------------------------
whoareyou
Fri May 13, 2011 7:10 pm

Re: help - Simple Matrix-like Code
-----------------------------------
I did your exercise for practice ... perhaps you could tell me how this relates to the matrix?


        int first = 4;
        int second = 6;
        int third = 10;

        c.println ("Values: " + (first) + ", " + (second) + ", " + (third));

        int added = 0;

        while (added == 0)
        {
            first += 1;
            second += 1;
            third += 1;
            added += 1;
        }

        c.println ("New Values: " + (first) + ", " + (second) + ", " + (third));

        int

-----------------------------------
whoareyou
Fri May 13, 2011 8:27 pm

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.

http://i.imgur.com/5FSqI.jpg

This is my code:


        //Create a font object and set the font and color of the characters
        //in the falling Matrix code
        c.setColor (Color.green);
        c.setFont (new Font ("monospaced", Font.BOLD, 20));

        //Create an array for the x-coordinates of the characters across the
        //top of the screen
        int

-----------------------------------
Zren
Sat May 14, 2011 5:55 am

RE:help - Simple Matrix-like Code
-----------------------------------
Here:
String str2 = str1.substring (nchars 
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.

-----------------------------------
whoareyou
Mon May 16, 2011 3:19 pm

RE:help - Simple Matrix-like Code
-----------------------------------
Ah yes, thank you very much!
