Milky Way
This application had several requirements:
- Learn to use Loops / Repetitions
- Work with random numbers
- Write a program that draws 100 circles at random positions in the window and with random diameters up to 100 pixels.
- Circles should be painted in random colors.
The Code
The main thing that was standing in my way was understanding how my loop was working. Initially, I was drawing random spheres, 100 at a time, per a few hundred milliseconds. I couldn't understand why my code wouldn't stop after 100 spheres were drawn. Eventually, I came to understand that it the loop was working as intended, because I would continually reset 'i' to '0' every loop, resulting in 100 spheres every loop. I fixed the issue by limiting how many times the loop could run, which was only once per button press. In the future I would like to add some physics to the applet, and make the stars behave as they would with mass in space. I think it would be pretty awesome.
Here is the code for my application. Please do not steal it. You will get caught.
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferStrategy; import java.util.Random; import javax.swing.*; /** * Class MilkyWay - write a description of the class here * * @author Tyler Persohn * @version 1.0 */ public class MilkyWay extends JApplet implements ActionListener, Runnable { private JLabel title; private JButton BEGIN, OFF; private Canvas canvas; private enum UniverseState { BEGIN, OFF; } private UniverseState state = UniverseState.OFF; private Random random = new Random(); public void init() { // this is a workaround for a security conflict with some browsers JRootPane rootPane = this.getRootPane(); rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE); Container contentPane = getContentPane(); // Size of the entire applet. contentPane.setBackground(Color.BLACK); // Background color. contentPane.setLayout(new BorderLayout()); contentPane.add(createTitlePanel(), BorderLayout.NORTH); // Title placement. contentPane.add(createButtonPanel(), BorderLayout.SOUTH);// Button placement. // Because repaint sucks. canvas = new Canvas(); getContentPane().add(canvas, BorderLayout.CENTER); canvas.createBufferStrategy(2); // Image buffer run(); } private Component createButtonPanel() { // Button Controls JPanel controls = new JPanel(); controls.setLayout(new FlowLayout()); controls.add(BEGIN = new JButton("ADD STARS")); //Button title. BEGIN.addActionListener(this); controls.add(OFF = new JButton("RESET")); OFF.addActionListener(this); return controls; } private Component createTitlePanel() { // Title Attributes JPanel titlePanel = new JPanel(); titlePanel.setLayout(new FlowLayout()); titlePanel.add(title = new JLabel("Milky Way", JLabel.CENTER)); //App title. title.setForeground(Color.WHITE); titlePanel.setBackground(Color.BLACK); return titlePanel; } public void render(Graphics g) { switch (state) { case BEGIN: int maxCircles = 100; int diameter; int x, y; for (int i = 0; i < maxCircles; ++i) { // Draw from 0 until 100. diameter = random.nextInt(100); // Random diameter. x = random.nextInt(canvas.getWidth()); // Random x position on canvas. y = random.nextInt(canvas.getHeight());// Random y position on canvas. g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); //Set color to random. g.fillOval(x, y, diameter, diameter); // Draw the circle. } break; case OFF: //When resetting canvas g.setColor(Color.BLACK); g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); break; } } @Override public void run() { BufferStrategy bs = canvas.getBufferStrategy(); Graphics2D g = (Graphics2D) bs.getDrawGraphics(); // Get graphics render(g); // Call render g.dispose(); // Cleanup bs.show(); // Pull new frame } @Override public void actionPerformed(ActionEvent event) { //Set state according to user input. if (event.getSource() == BEGIN) { state = UniverseState.BEGIN; run(); } if (event.getSource() == OFF) { state = UniverseState.OFF; run(); } } }