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

    Default help changing the RGB of a fillRect

    I have written this program that makes a JPanel that creates random black and white pixels using a fillRect but now I would like to make random colors instead. How would I set the RGB values of an individual fillRect?

    Java Code:
    import java.util.Random;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Basic extends JPanel {
    	public void paintComponent(Graphics g) {
        	    super.paintComponent(g);
        	    Graphics2D graphics = (Graphics2D) g;
        	    graphics.setColor(Color.black);
    
        	    Dimension size = getSize();
    	    Random number = new Random();
    
    	    int image[][];
        	    int a = 0;
        
        	    for (int x = 0; x < size.width; x++) {
         		for (int y = 0; y < size.height; y++) {
          	  		a = number.nextInt(2);
              		graphics.fillRect(x, y, a, a);
          		}
        	    }
      	}
    
      	public static void main(String[] args) {
        	    Basic basic = new Basic();
        	    basic.setPreferredSize(new Dimension(400, 400));
        	    JFrame frame = new JFrame("Basic");
        	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	    frame.add(basic);
        	    frame.pack();
        	    frame.setLocationRelativeTo(null);
        	    frame.setVisible(true);
      	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,446
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by AnimeKitty View Post
    I have written this program that makes a JPanel that creates random black and white pixels using a fillRect but now I would like to make random colors instead. How would I set the RGB values of an individual fillRect?
    Basically you want to generate a (pseudo) random color; have a look the Color API and the Random class API documentation; generating a (pseudo) random number in the range [0, 0xffffff] isn't rocket science.

    kind regards,

    Jos

  3. #3
    AnimeKitty's Avatar
    AnimeKitty is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    Got it! :)
    Thanks JosAH

    Java Code:
    public void paintComponent(Graphics g) {
        	super.paintComponent(g);
        	Graphics2D graphics = (Graphics2D) g;
        	Dimension size = getSize();
    
    	Random r = new Random();
    	int image[][];
        	int a = 0;
    
        	for (int x = 0; x < size.width; x++) {
         		for (int y = 0; y < size.height; y++) {
          	  		a = r.nextInt(2);
              		graphics.fillRect(x, y, a, a);
              		Color  c = new Color(r.nextInt(256), r.nextInt(256),  
                            r.nextInt(256));
          	  		graphics.setColor(c);
          		}
        	}
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,446
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by AnimeKitty View Post
    Got it! :)
    Thanks JosAH

    Java Code:
    public void paintComponent(Graphics g) {
        	super.paintComponent(g);
        	Graphics2D graphics = (Graphics2D) g;
        	Dimension size = getSize();
    
    	Random r = new Random();
    	int image[][];
        	int a = 0;
    
        	for (int x = 0; x < size.width; x++) {
         		for (int y = 0; y < size.height; y++) {
          	  		a = r.nextInt(2);
              		graphics.fillRect(x, y, a, a);
              		Color  c = new Color(r.nextInt(256), r.nextInt(256),  
                            r.nextInt(256));
          	  		graphics.setColor(c);
          		}
        	}
    }
    Good, there's another Color constructor that takes a single int representing the rgb values so you can create one as:

    Java Code:
    Color c= new Color(r.nextInt(0x1000000));
    just a tip ;-)

    kind regards,

    Jos

Similar Threads

  1. Changing Cursor
    By ridvan in forum AWT / Swing
    Replies: 2
    Last Post: 06-27-2010, 04:11 AM
  2. Changing Project Name
    By nvts in forum NetBeans
    Replies: 1
    Last Post: 05-06-2010, 12:36 AM
  3. Changing the values of a map.
    By Onra in forum New To Java
    Replies: 1
    Last Post: 02-26-2010, 12:20 AM
  4. its not changing bgcolor
    By javanoobita in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 02:29 PM
  5. Changing the Jframe
    By Nemo1959 in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 03:58 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
  •