Question

/* Create a Picture which is an NxN grid of color squares * each square is a random color of si...

/* Create a Picture which is an NxN grid of color squares
  * each square is a random color of size*size pixels */
  public static Picture create(int N, int size)

 /* Create a two-dimensional NxN array of random Colors*/
  public static Color[][] createColors(int N)

 /* Generate a random Color (R,G,B each between 0 and 255) */
  public static Color generateRandomColor()
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:::

DrawPicture.java

package graphichal;


import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JComponent;
import javax.swing.JFrame;

class Picture{
   int size;
   Color colors[][];
   Picture(int size){
       this.size = size;
   }

   void drawPicture(Graphics g){
       for(int i=0;i<colors.length;i++){
           for(int j=0;j<colors.length;j++){
               g.setColor(colors[i][j]);
               g.fillRect(i*size, j*size, size, size);
           }  
       }
   }
}

public class DrawPicture {

   /* Create a Picture which is an NxN grid of color squares
   * each square is a random color of size*size pixels */
   public static Picture create(int N, int size){
       Picture picture = new Picture(size);
       picture.colors = createColors(N);
       return picture;
   }

   /* Create a two-dimensional NxN array of random Colors*/
   public static Color[][] createColors(int N){
       Color colors[][] = new Color[N][N];
       for(int i=0;i<N;i++){
           for(int j=0;j<N;j++){
               colors[i][j] = generateRandomColor();
           }  
       }
       return colors;
   }

   /* Generate a random Color (R,G,B each between 0 and 255) */
   public static Color generateRandomColor(){
       Random rand = new Random();
       Color color = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
       return color;
   }

   public static void main(String[] a) {
       JFrame window = new JFrame("Picture");
       window.setSize(1000,1000);
       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       window.getContentPane().add(new JComponent() {
           private static final long serialVersionUID = 1L;
           //paint function call recursion function once
           public void paint(Graphics g) {
               Picture pic = create(20, 20);
               pic.drawPicture(g);
           }
       });
       window.setVisible(true);
   }

}

output:

ture.java Eclipse Run | Picture

Add a comment
Know the answer?
Add Answer to:
/* Create a Picture which is an NxN grid of color squares * each square is a random color of si...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program which will Ask the user a series of questions using the Scanner object Based on t...

    Write a program which will Ask the user a series of questions using the Scanner object Based on the input, draw a grid of star figures on the DrawingPanel You program should ask your user: . What R, G & B values to create a color to draw the figure? How many stars across should the fgure be? How many stars tall should the figure be? Note that your program does not need to error check the users responses Your...

  • in java Original: Sound Visualization Write a program that uses StdAudio and Picture to create an...

    in java Original: Sound Visualization Write a program that uses StdAudio and Picture to create an interesting two-dimensional color visualization of a sound file while it is playing. Be creative! Additional Notes: double[] data = StdAudio.read(<filename>); -> Takes a sound file and store it as a 2D array of real numbers. Picture pic = new Picture(300,200); -> Creates a picture 300 pixels wide and 200 pixels high Color c= new Color(<red>,<green>,<blue>); -> Each value of <red>, <green>, <blue> is between...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • A two-dimensional square array of integers is a Latin square if the following conditions are true. The first row has no duplicate values. All values in the first row of the square appear in each row...

    A two-dimensional square array of integers is a Latin square if the following conditions are true. The first row has no duplicate values. All values in the first row of the square appear in each row of the square. All values in the first row of the square appear in each column of the square. Examples of Latin Squares 10 30 200 0 20 30 10 30 0 10 20 20 10 0 30 Examples that are NOT Latin Squares...

  • 1. create a class called MemoryBoard. a. MemoryBoard will represent a grid of memory squares. it...

    1. create a class called MemoryBoard. a. MemoryBoard will represent a grid of memory squares. it needs to track all of the squares on the board, the cars set it's using, the width and the height of the board, the number of turns that have been taken, and the current player's id. 2. create a arraylist to store the squares on the board. it should store Square objects. the class should also store the width(an int), the number of turns...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • in java / You will: // 1- create a square 2 dimensional array of random size...

    in java / You will: // 1- create a square 2 dimensional array of random size (less than 11) // 2- fill the array with random integers from 1 to the size limit // 3- print the array // 4- prompt to see if the user wants to do another //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 1- The square can use the same random value for x and y. Don't allow 0 size arrays. // 2- Maybe a nested set of for loops? to...

  • MasterMind in Java Activity MasterMind project Create a new Java Application project named MasterMind allowing Netbeans...

    MasterMind in Java Activity MasterMind project Create a new Java Application project named MasterMind allowing Netbeans IDE to create the main class called MasterMind.java MasterMind class Method main() should: Call static method System.out.println() and result in displaying “Welcome to MasterMind!” Call static method JOptionPane.showMessageDialog(arg1, arg2) and result in displaying a message dialog displaying “Let’s Play MasterMind!” Instantiate an instance of class Game() constants Create package Constants class Create class Constants Create constants by declaring them as “public static final”: public...

  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT