Results 1 to 2 of 2
  1. #1
    AnimeKitty's Avatar
    AnimeKitty is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default Initializing an array before running a thread

    I am making an array of 1s/0s then starting a thread to paint them as red. Because the array was before the thread the pixels should remain static as red 1s/0s. another part of paint should draw random colors continually as the red remains static.

    Java Code:
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JComponent;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.Color;
    
    public class Colors extends JComponent implements Runnable {
    
    	//int[][] a = new int[100][100];
    
    
    	public static void main(String[] args) {
        	JFrame f = new JFrame("Colors");
        	f.add(new Colors());
        	f.setSize(600, 600);
        	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	f.setVisible(true);
      	}
    
    	public void initialize() {
        	new Thread(this).start();
        }
    	public void run() {
          		repaint();
      	}
    
    	public void dd() {
      	//	int[][] a = new int[100][100];
    		Random generator = new Random();
    		for (int x = 0; x < 100; x++) {
    			for (int y = 0; y < 100; y++) {
    				a[x][y] = generator.nextInt(2);
    			}
    		}
    	}
    
    	public void paint(Graphics g) {
        //	initialize();
        	Dimension size = getSize();
    
    		Random generator = new Random();
    
    		g.setColor(Color.black);
    		g.fillRect(0, 0, 300, 300);
    
    		for (int x = 0; x < 100; x++) {
    			for (int y = 0; y < 100; y++) {
    
    				if (a[x][y] == 0){
    				g.setColor(Color.red);
    				g.fillRect(x, y, 1, 1);
    				}
    			}
    		}
    
    		for (int x = 50; x < 100; x++) {
         		for (int y = 50; y < 100; y++) {
    
         			int r = generator.nextInt(6);
    
              		g.fillRect(x, y, 1, 1);
    
              		if(r==0){
              			g.setColor(Color.red);
              		}
              		if(r==1){
              			g.setColor(Color.orange);
              		}
              		if(r==2){
              			g.setColor(Color.yellow);
              		}
              		if(r==3){
              			g.setColor(Color.green);
              		}
              		if(r==4){
              			g.setColor(Color.blue);
              		}
              		if(r==5){
              			g.setColor(Color.magenta);
              		}
         		}
    		}
      	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Initializing an array before running a thread

    Do you have a question or a problem? If so, please ask it.

    A comment on your code:
    The list of if statements at the end of the paint method are mutually exclusive. If one is true none of the others can be true.
    They should be coded as if/else if to show that. Why test for r == 2 if you've already found that r is 1?

    After setting the color, you don't draw anything using that color.
    Last edited by Norm; 01-31-2012 at 01:13 PM.

Similar Threads

  1. Replies: 2
    Last Post: 08-12-2011, 08:48 PM
  2. Replies: 4
    Last Post: 07-20-2011, 01:58 AM
  3. Is it possible to keep a thread running?
    By Fortu in forum Threads and Synchronization
    Replies: 5
    Last Post: 01-07-2011, 06:36 PM
  4. How to keep a thread running
    By Fortu in forum New To Java
    Replies: 5
    Last Post: 12-31-2010, 09:45 AM
  5. Running thread a second time
    By Fleur in forum New To Java
    Replies: 18
    Last Post: 11-17-2009, 12:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •