Results 1 to 4 of 4
- 07-27-2010, 04:40 PM #1
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); } }
- 07-27-2010, 04:48 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 07-27-2010, 05:10 PM #3
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); } } }
- 07-27-2010, 06:02 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Changing Cursor
By ridvan in forum AWT / SwingReplies: 2Last Post: 06-27-2010, 04:11 AM -
Changing Project Name
By nvts in forum NetBeansReplies: 1Last Post: 05-06-2010, 12:36 AM -
Changing the values of a map.
By Onra in forum New To JavaReplies: 1Last Post: 02-26-2010, 12:20 AM -
its not changing bgcolor
By javanoobita in forum Java AppletsReplies: 1Last Post: 02-21-2009, 02:29 PM -
Changing the Jframe
By Nemo1959 in forum New To JavaReplies: 13Last Post: 09-19-2008, 03:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks