Question

In: Computer Science

Hello. I'm trying to write down java code making stars whose numbers are determined by user...

Hello. I'm trying to write down java code making stars whose numbers are determined by user input on the console (each star's data are stored in ArrayList).

the class extends JFrame and its color, size(Zoom in and Zoom out), and location are changing every time.

Moreover, its angular rotation is changed at a certain determined degree.

Does anyone know how to write down the java code of this question?

Solutions

Expert Solution

Solution:

stars_frame.java:

//stars_frame.java file to draw number of stars as input given by user

import java.awt.Color;

import java.util.Scanner;

import javax.swing.JFrame;

import java.util.ArrayList;

import java.util.Random;

public class stars_frame extends JFrame {

// size of window

private final int WINDOW_SIZE = 600;

// array list of stars_frame

private ArrayList<Star> stars_frameList;

//n is numbers of starts given as input

public stars_frame(int n) {

// passing a title to super class constructor

super("stars_frame");

Random random = new Random();

// no layout manager for absolute positioning

setLayout(null);

// initializing array list

stars_frameList = new ArrayList<Star>();

// looping for number of times to draw stars_frame

for (int i = 0; i < n; i++) {

// generating a random size between 50 and 150

int size = random.nextInt(100) + 50;

// creating a star with random angle, random color and above size

Star star = new Star(random.nextInt(360), new Color(

random.nextInt(256), random.nextInt(256),

random.nextInt(256)), size);

// specifying star in a random location on the window

star.setBounds(random.nextInt(WINDOW_SIZE - size),

random.nextInt(WINDOW_SIZE - size), size, size);

// adding stars_frame to window

add(star);

// adding to list, so that each star can be altered later when you want

stars_frameList.add(star);

}


//display window

setDefaultCloseOperation(EXIT_ON_CLOSE);

setSize(WINDOW_SIZE, WINDOW_SIZE);

setVisible(true);

}

public static void main(String[] args) {

//taking input as number of stars_frame

Scanner scanner = new Scanner(System.in);

System.out.print("Enter number of stars_frame: ");

int no_stars = scanner.nextInt();

new stars_frame(no_stars);

}

}

Star.java:

//Star.java extending JPanel to represent a single star

import java.awt.Color;//color package

import java.awt.Dimension;//for dimensions

import java.awt.Graphics;//graphics package

import java.awt.Polygon;

import javax.swing.JPanel;

public class Star extends JPanel {

// attributes using to store angle and color

private double angle;

private Color color;

// constructor to take angle, color and size

public Star(double angle, Color color, int size) {

this.angle = angle;

this.color = color;

// trnsperant background color

setBackground(new Color(0, 0, 0, 0));

setPreferredSize(new Dimension(size, size));

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

// angle

double temp_angle = angle;

// center-coordinates

double Center_X = getWidth() / 2, Center_Y = getHeight() / 2;

// distance between edge and center of the star

double radius = getWidth() / 2;

// distance between center of star to the vertex between edges

double innerRadius = getWidth() / 5;

// dividing a circle into 10 parts

int angle_difference = 360 / 10;

// polygan

Polygon polygon = new Polygon();

double x, y;

// looping for 10 times

for (int i = 0; i < 10; i++) {

if (i % 2 == 0) {

x = Center_X + radius * Math.cos(Math.toRadians(temp_angle));

y = Center_Y + radius * Math.sin(Math.toRadians(temp_angle));

} else {


x = Center_X + innerRadius

* Math.cos(Math.toRadians(temp_angle));

y = Center_Y + innerRadius

* Math.sin(Math.toRadians(temp_angle));

}

// adding to polygon

polygon.addPoint((int) x, (int) y);

// updating temp_angle

temp_angle += angle_difference;

}

// white colour to fill polygon

g.setColor(color);

g.fillPolygon(polygon);

// black colour for polygan outline

g.setColor(Color.BLACK);

g.drawPolygon(polygon);

}

}

Example input of n=20:

Thank you, Have a great day:-)


Related Solutions

Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed text. I'm having the absolute hardest time getting stared and figuring out how to identify in the code if the input needs to be encrypted/decrypted and identifying the string to encrypt/decrypt with. These are the instructions: Objectives Command line input File input and output Rethrowing exceptions Program Description Gaius Julius Caesar encoded his battle messages so that the opponent could not read them should...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters...
Write Java code that allows a user to repeatedly enter numbers. Each time the user enters a number, the program should print out the average of the last 3 numbers (or all numbers if 3 or less have been entered). I would like a detailed explanation so that a beginner level Java programmer could understand.
Write a java code to ask the user for a string, and assuming the user enters...
Write a java code to ask the user for a string, and assuming the user enters a string containing character "a", your java program must convert all "a" to "b" in the string so for example if user types : "AMAZON" , your program must convert it to "BMBZON" the driver program must ask : enter a string containing "a" or "A" --------- (user enters string) then it should say; HERE IS YOUR NEW STRING ---------- (string with all a's...
MIPS Program I'm trying to write a program that will take in two numbers from the...
MIPS Program I'm trying to write a program that will take in two numbers from the user and output the sum at the end. However, I keep getting very wrong answers. For example, I add 2 and 2 and get 268501000. Help would be appreciated. .data #starts data use userIn1:    .word 4 #sets aside space for input    userIn2:    .word 4 #sets aside space for input total:    .word 4 #sets space aside for input    request:   ...
I'm trying to get my code to loop back to let the user input multiple patients....
I'm trying to get my code to loop back to let the user input multiple patients. import java.util.Scanner; public class XYZ {    public static void main(String[] args) {           String response;        do{            //import scanner for input            Scanner input = new Scanner(System.in);               //prompt user input for last name            System.out.print("Enter last name ");            //output for last name            String...
write down the java code for garden sprinkler system.
write down the java code for garden sprinkler system.
Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199 Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199   Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT