I made a program involving JFrame / JPanel / JLabels, and it's too much for the computer to handle. Any alternatives?
I have a good, and fast computer - though my program must have caused too much to happen at once, because in the middle of the program, the screen turned black, and the mouse had a 10 second delay period. My program consists of a 2d char array, 65 rows by 300 columns (all the rows put ontop of eachother are enough to fill up the whole screen width and height wise), one third of the columns are chosen at random to hold a random number between 11 and 65- (this number represents how many random verticle characters that column will hold starting at the top). Each character is a randomly picked hyrogliphic character I then create a String array of 65 elements to hold the 65 char rows, I then create a JLabel array with 65 elements, transferring the string array into it. Then I add it to the JPanel inside the JFrame (the JPanel's background is black -- JLabel's foreground is green) I iterate this 12 times in a nested for loop, each time changing the verticle position of the entire picture slightly (50 MS between iterations) -- then the outer for loop will reset all the characters positions, values back to random (outer loop iterates 100 times). By now I'm sure you get the picture that this program is a matrix 'raining code' computer screen animation. I suppose updating that many JFrames & labels is very exhausting on the computer, because of how it crapped out when compiling. Any ideas about how to implement the same algorithm & display, but in a fashion that wont freeze up the computer?
Using about 15 JPanels, 1 JFrame, and 700 JLabels, this nerfed matrix animation version causes absolutely no lag or delay in the computer's function-- ``` import javax.swing.*; import java.awt.*; import java.awt.event.*; public class matrix { JFrame frame; JPanel pan; JLabel[] lab; public static void main(String[] args) { System.out.println("H"); matrix exe = new matrix(); } public matrix() { pan = new JPanel(); pan.setBackground(Color.BLACK); lab = new JLabel[700]; frame = new JFrame(); frame.setUndecorated(true); frame.setExtendedState(Frame.MAXIMIZED_BOTH); frame.add(pan); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); for(int i = 0; i < 700; i++) { String add2Pan = ""; switch(i%5) { case 0: add2Pan = "11011010000101011#0100#010111#0101011010101001000000010011111111110010101001010101010010101011111101011#110101#111010010100100010010101#11010111100101011100111101001010001010#0111#101011#1011100100010101010101001010010101101101010010101001010100000100101010010101001010101010111001110101010101010110"; lab[i] = new JLabel(add2Pan); break; case 1: add2Pan = "100010010101##101010000#101010111##0101000000#101001##101001010000#1010000101011110101000101111001000010101011110100101000###00101001#00101111000101010##0010011111#110101#111010010100100010010101#11010111100101011100111101001010111111001010100101010101001010101111110101001010#0111#10100100000101101"; lab[i] = new JLabel(add2Pan); break; case 2: add2Pan = "10010101#101001010#010010010#01010100#010100100010100101111010101011010100100101001000101010101001001#110101#111010010100100010010101#11010111100101011100111101001010000100101011101010010010010011111010101010#01000001001111111111001010100101010101001010101111110101001010#0111#101010110101111001100#"; lab[i] = new JLabel(add2Pan); break; case 3: add2Pan = "1010010000##10101000#0101011#110101#111010010100100010010101#11010111100101011100111101001010110#010100011001#1001000101##10010100000#1010111011010100#10101001001001111111111001010100101010101001010101111110101001010#0111#101001010111001010#1101001010100101#1010010101010101010101010#101001010010011"; lab[i] = new JLabel(add2Pan); break; default: add2Pan = "101001011010010001001010010111001010#1010100100010101#110101#111010010100100010010101#1101011110010101110011110100101000010110011110#1010101011#110101#111010010100100010010101#1101011110010101110011110100101001001110000100101001001111111111001010100101010101001010101111110101001010#0111#1010##1#101"; lab[i] = new JLabel(add2Pan); break; } if(i < 251) lab[i].setForeground(Color.GREEN); else if(i > 250 && i < 450) { lab[i].setForeground(Color.DARK_GRAY); } else lab[i].setForeground(Color.RED); } for(int i = 0; i < 700; i++) { pan.add(lab[i]); frame.add(pan); frame.setVisible(true); matrix delay = new matrix(true, 25); if(i%52 == 0 && i != 0) { pan = new JPanel(); pan.setBackground(Color.BLACK); } } matrix delay = new matrix(true, 2000); JButton close = new JButton("Enter Matrix"); close.setBackground(Color.GREEN); close.setForeground(Color.BLACK); close.addActionListener(new ClosingSequence()); pan.add(close); frame.add(pan); frame.setVisible(true); } public matrix(boolean A, int B) { A = !A; //B = 432; try{ Thread.sleep(B); }catch(Exception e) { } } private class ClosingSequence implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } } ``` Using the setVisible(true) method after adding each JLabel & delaying 25ms is how it gets the appearance of animation - but without discarding a JPanel or JFrame each time.
A 10 second delay for the mouse is probably swapping/thrashing. Run your program with Task Manager / Activity Monitor and get an idea of how much memory, cpu, network and I/O your program is using.
Oh, thanks for the tip. I'll definitely use that as a gauge of cpu usage ect in the future.
0_0
you like? >:) *cheesy matrix screen** I placed a shortcut to run this program in my startup folder, so when I sign in it pops up automatically lols.
Join our real-time social learning platform and learn together with your friends!