Question

Hello, I have a question for the graphic section of java. I'm trying to make a...

Hello, I have a question for the graphic section of java.

I'm trying to make a number of stars whose numbers are determined by console number(Using Scanner) and color, size, location, and angular rotation are changed randomly.

My instructors required me to extend jPanel and use ArrayList to store each start's data.

It would really appreciate solving this problem

thanks

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Copy Star.java and StarsFrame.java into separate files

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

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Polygon;

import javax.swing.JPanel;

public class Star extends JPanel {

      // attributes to store angle and color

      private double angle;

      private Color color;

      // constructor taking angle, color and size

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

            this.angle = angle;

            this.color = color;

            // using a fully transparent color as background

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

            // using size*size panel size

            setPreferredSize(new Dimension(size, size));

      }

      @Override

      protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            // starting angle

            double angle_temp = angle;

            // center coordinates

            double centerX = getWidth() / 2, centerY = getHeight() / 2;

            // distance between center of star to the edge

            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;

            // creating a polygon

            Polygon polygon = new Polygon();

            double x, y;

            // looping for 10 times

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

                  // finding the next (x,y) point based on the value of i (alternating

                  // between long and short radii)

                  if (i % 2 == 0) {

                        // finding the point which is radius distant away from center at

                        // angle_temp degrees

                        x = centerX + radius * Math.cos(Math.toRadians(angle_temp));

                        y = centerY + radius * Math.sin(Math.toRadians(angle_temp));

                  } else {

                        // finding the point which is innerRadius distant away from

                        // center at angle_temp degrees

                        x = centerX + innerRadius

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

                        y = centerY + innerRadius

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

                  }

                  // adding to polygon

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

                  // updating angle_temp

                  angle_temp += angle_difference;

            }

            // using while color, filling polygon

            g.setColor(color);

            g.fillPolygon(polygon);

            // using black color, drawing an outline effect of the polygon

            g.setColor(Color.BLACK);

            g.drawPolygon(polygon);

      }

}

//StarsFrame.java to draw the user specified number of stars

import java.awt.Color;

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

import javax.swing.JFrame;

public class StarsFrame extends JFrame {

      // window size

      private final int WINDOW_SIZE = 700;

      // array list of stars

      private ArrayList<Star> starsList;

      // constructor to initialize the GUI, taking number of stars to draw

      public StarsFrame(int numStars) {

            // passing a title to super class constructor

            super("Stars");

            // initializing random number genertor

            Random random = new Random();

            // using no layout managers (absolute positioning)

            setLayout(null);

            // initializing array list

            starsList = new ArrayList<Star>();

            // looping for numStars number of times

            for (int i = 0; i < numStars; 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 to window

                  add(star);

                  // adding to the list, so that each star can be altered later, if

                  // you want to.

                  starsList.add(star);

            }

           

            //setting up and displaying the window

            setDefaultCloseOperation(EXIT_ON_CLOSE);

            setSize(WINDOW_SIZE, WINDOW_SIZE);

            setVisible(true);

      }

      public static void main(String[] args) {

            //using a scanner, asking and reading number of stars

            Scanner scanner = new Scanner(System.in);

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

            int stars = scanner.nextInt();

            // initializing the GUI with specified number of stars

            new StarsFrame(stars);

      }

}

/*OUTPUT (for number of stars=20)*/


Add a comment
Know the answer?
Add Answer to:
Hello, I have a question for the graphic section of java. I'm trying to make a...
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
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