help - Simple Matrix-like Code
Author |
Message |
whoareyou
|
Posted: 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" ...}
Description: |
|
Download |
Filename: |
Matrix.rar |
Filesize: |
168.42 KB |
Downloaded: |
381 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: 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.
|
|
|
|
|
|
whoareyou
|
Posted: 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.
|
|
|
|
|
|
Zren
|
Posted: 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
}
|
|
|
|
|
|
whoareyou
|
Posted: 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.
Java: |
import java.awt.*;
import hsa.Console;
public class MatrixCode
{
static Console c;
public static void main (String[] args)
{
c = new Console ();
while (true)
{
c.setColor (Color.black);
c.fillRect (0, 0, c.maxx (), c.maxy ());
c.setColor (Color.green);
c.setFont (new Font ("monospaced", Font.BOLD, 20));
int x, y;
x = (int) (Math.random () * c.maxx ());
y = 0;
for (int i = 0 ; i < c.maxy () ; i++)
{
y = i;
c.drawString ("M", x, y);
}
}
}
}
|
how am I supposed to implement arrays, and show many characters on the screen at once and make them animate??
|
|
|
|
|
|
Zren
|
Posted: 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};
|
|
|
|
|
|
whoareyou
|
Posted: 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?
Java: |
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[] arr = {4, 6, 10};
c.print ("Array Values: ");
for (int i = 0 ; i < arr.length ; i++)
{
if (i == arr.length - 1)
{
c.println (arr [i]);
}
else
{
c.print (arr [i] + ", ");
}
}
for (int i = 0 ; i < arr.length ; i++)
{
arr [i]++;
}
c.println ("New Array Values: " + (first) + ", " + (second) + ", " + (third));
|
|
|
|
|
|
|
whoareyou
|
Posted: 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:
Java: |
//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[] xchars = new int [100];
//Create an array for the y-coordinates of the characters going
//down the console screen
int[] ychars = new int [100];
//Create an array to store a list of numbers representing characters in
//the string that stores all of chracters that will be displayed
int[] nchars = new int [100];
//Create a list of characters used in Matrix by storing them in a string
//Individual characters can be accessed by using the subString () method
String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-+][{}\\|'\";:/?.>,<";
//Assign different x, y, and character values to the arrays created with the
//specified length
for (int i = 0 ; i < 100 ; i++)
{
nchars [i] = (int) (Math.random () * str1.length ());
xchars [i] = (int) (Math.random () * c.maxx ());
ychars [i] = (int) (Math.random () * c.maxy ());
}
//Use an infinite while loop to display the characters on the screen
//and change the required character's location
while (true)
{
c.setColor (Color.black);
c.fillRect (0, 0, c.maxx (), c.maxy ());
for (int i = 0 ; i < 100 ; i++)
{
ychars [i] += 20;
if (ychars [i] >= c.maxy ())
{
nchars [i] = (int) (Math.random () * str1.length ());
xchars [i] = (int) (Math.random () * c.maxx ());
ychars [i] = 0;
}
c.setColor(Color.green);
String str2 = str1.substring (nchars [i]);
for (int j = 0 ; j < 1 ; j++)
{
c.drawString (str2, xchars [i], ychars [i]);
}
}
Thread.sleep (50);
}
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
|
|
|
|
whoareyou
|
Posted: Mon May 16, 2011 3:19 pm Post subject: RE:help - Simple Matrix-like Code |
|
|
Ah yes, thank you very much!
|
|
|
|
|
|
|
|