Question

Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows in a graphical way, as shown below, th

Note that the colors will vary depending on the random numbers that will be associated with the different colors as explained

The readFiles method This method will receive the following parameters: A Scanner variable FNames_file which will be pointing

Note that you dont need to open again the Scanner variables given that they were opened in the main method before calling th

The showNames method This method will receive the following list of parameters An array of strings with the Names already rea

The getRightColor method This method will receive an integer number as a parameter and will return a Color class object varia

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

* Program to diplay in different color and coordinates & Drawing Panel Adam Bernie Cameron Daniel Fanny import java.awt.; impProgram

/**
* Program to diplay in different color and coordinates
*/
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;
import javax.swing.*;
//Class to draw
public class DrawingPanel extends JPanel {
   //Constant size
   public static final int MAX_VALUES=100;
   //Global arrays for names and coordinates
   public static String Fnames[]=new String[MAX_VALUES];
   public static int xCoordinates[]=new int[MAX_VALUES];
   public static int yCoordinates[]=new int[MAX_VALUES];
   //Count variable
   public static int values;
   //Draeing method
public void paintComponent(Graphics g) {
      //Panel color
    g.setColor(Color.WHITE);
    //Loop hrough arrays
    for(int i=0;i<values;i++) {
       //random value for color
       Random randVal=new Random();
       int color=randVal.nextInt()%11;
       //Set color
       if(color==0) {
           g.setColor(getForeground().BLACK);
       }
       else if(color==1) {
           g.setColor(getForeground().BLUE);
       }
       else if(color==2) {
           g.setColor(getForeground().CYAN);
       }
       else if(color==3) {
           g.setColor(getForeground().DARK_GRAY);
       }
       else if(color==4) {
           g.setColor(getForeground().GRAY);
       }
       else if(color==5) {
           g.setColor(getForeground().GREEN);
       }
       else if(color==6) {
           g.setColor(getForeground().LIGHT_GRAY);
       }
       else if(color==7) {
           g.setColor(getForeground().MAGENTA);
       }
       else if(color==8) {
           g.setColor(getForeground().ORANGE);
       }
       else if(color==9) {
           g.setColor(getForeground().PINK);
       }
       else if(color==10) {
           g.setColor(getForeground().RED);
       }
       else if(color==11) {
           g.setColor(getForeground().YELLOW);
       }
       //Draw name accordingly
        g.drawString(Fnames[i],xCoordinates[i],yCoordinates[i]);
    }
  
}
//Main method
public static void main(String args[]) {
      //Method to read file data
   values=readFile(Fnames,xCoordinates,yCoordinates);
   //Frame for display
    JFrame frame = new JFrame("Drawing Panel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    DrawingPanel panel = new DrawingPanel();
    frame.add(panel);
    frame.setSize(500,500);
    frame.setVisible(true);
}
//Read data from file and store corresponding array
//Return number of value in the array
public static int readFile(String names[],int xCoordinates[],int yCoordinates[]) {
      int cnt=0;
   try {
       Scanner scName = new Scanner(new File("names1.txt"));
       Scanner scCoordinate = new Scanner(new File("coordinates.txt"));
          while(scName.hasNextLine() && scCoordinate.hasNextLine()) {
           names[cnt]=scName.nextLine();
           String line[]=scCoordinate.nextLine().split(" ");
           xCoordinates[cnt]=Integer.parseInt(line[0]);
           yCoordinates[cnt]=Integer.parseInt(line[1]);
           cnt++;
          }
   } catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
   }
      return cnt;
}
}

-----------------------------------------------------

Output

& Drawing Panel - 0 X Adam Bernie Cameron Daniel Fanny

Add a comment
Know the answer?
Add Answer to:
Tasks Write a program that prints in text mode Project 2 written by YOURNAME And shows...
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
  • In this lab, you will create one class named ArrayFun.java which will contain a main method...

    In this lab, you will create one class named ArrayFun.java which will contain a main method and 4 static methods that will be called from the main method. The process for your main method will be as follows: Declare and create a Scanner object to read from the keyboard Declare and create an array that will hold up to 100 integers Declare a variable to keep track of the number of integers currently in the array Call the fillArray method...

  • The first programming project involves writing a program that computes the minimum, the maximum and the...

    The first programming project involves writing a program that computes the minimum, the maximum and the average weight of a collection of weights represented in pounds and ounces that are read from an input file. This program consists of two classes. The first class is the Weight class, which is specified in integer pounds and ounces stored as a double precision floating point number. It should have five public methods and two private methods: 1. A public constructor that allows...

  • write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.t...

    write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.txt and all output will go to the screen Assume there will not be more than 100 7 23.56 16 88.12 10 75.1 Design a Java class with a main method that does the following 1) Reads the data into two arrays of doubles,...

  • Please run the program and show the result with screenshots after. Thank you (Java Eclipse) Customer...

    Please run the program and show the result with screenshots after. Thank you (Java Eclipse) Customer Data: Area Codes Assume you work for a company that tracks customer information, including name, gender and phone numbers Your company has a file called customers.txt which contains the following information: Jiming Wu F 4082123458 James Brown M 8315678432 Leanna Perez F 4087654433 Xing Li M 8313214555 Stacey Cahill O 8312123333 Mohammed Abbas M 4083134444 Kumari Chakrabarti F 4086667777 Shakil Smith M 4082123333 Jung...

  • in java plz amaining Time: 1 hour, 49 minutes, 15 seconds. Jestion Completion Status: Write a...

    in java plz amaining Time: 1 hour, 49 minutes, 15 seconds. Jestion Completion Status: Write a program that asks the user for an input file name, open, read ar number of students is not known. For each student there is a name and and display a letter grade for each student and the average test score fc • openFile: This method open and test the file, if file is not found and exit. Otherwise, return the opened file to the...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Write a program that reads a file containing an arbitrary number of text integers that are...

    Write a program that reads a file containing an arbitrary number of text integers that are in the range 0 to 99 and counts how many fall into each of eleven ranges. The ranges are 0-9, 10-19, 20-29,..., 90-99 and an eleventh range for "out of range." Each range will correspond to a cell of an array of ints. Find the correct cell for each input integer using integer division. After reading in the file and counting the integers, write...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

  • *Java* Given the attached Question class and quiz text, write a driver program to input the...

    *Java* Given the attached Question class and quiz text, write a driver program to input the questions from the text file, print each quiz question, input the character for the answer, and, after all questions, report the results. Write a Quiz class for the driver, with a main method. There should be a Scanner field for the input file, a field for the array of Questions (initialized to 100 possible questions), and an int field for the actual number of...

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