import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.geom.*;
import java.util.Random;
import javax.swing.*;
import java.awt.event.*;
public class assig5
{
public static void main(String [] args)
{
new assig5();
int sz = Integer.parseInt(args[0]);
int rate = Integer.parseInt(args[1]);
new A5Help(sz, rate);
}
public class A5Help
{
private MyPanel thePanel; // StopWatch is a subclass of JPanel
private JFrame theWindow;
public A5Help(int sz, int rate)
{
theWindow = new JFrame("CS 401 A5 Help Program");
thePanel = new MyPanel(sz, rate); // The argument to this constructor
// determines the size of the digits in the stopwatch
Container c = theWindow.getContentPane();
c.add(thePanel, BorderLayout.CENTER);
theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theWindow.pack();
theWindow.setVisible(true);
}
}
class CrazyLED extends LED
{
private int active;
public CrazyLED(int X, int Y, int sz)
{
super(X, Y, sz);
active = 0;
}
// This method is abstract in LED, so it must be implemented
// here. Note what is being done.
public void draw(Graphics2D g2)
{
// I am increasing the stroke width so the LED looks better
g2.setStroke(new BasicStroke(8));
g2.setColor(Color.blue);
g2.draw(segments[active]);
// Setting the stroke back to the default here -- see what happens
// if you don't do this.
g2.setStroke(new BasicStroke());
}
// Simple mutator to change the segment that is being
// displayed
public void mutate()
{
active = (active + 1) % segments.length;
}
}
public class MyPanel extends JPanel implements ActionListener
{
// Declare instance variables as shown.
private CrazyLED theLED; // Object to be drawn graphically
private JButton start, stop, move; // Some buttons
private JPanel buttonPanel; // Another panel to hold the buttons
private ButtonListener bListener; // listener for the buttons
private Timer T; // Timer -- see the API in javax.swing.Timer
private int size;
private int prefWid, prefHt; // see details on these below
public MyPanel(int sz, int rate)
{
size = sz;
// Set the preferred width and height of the panel based on the
// size argument that is passed in. See the getPreferredSize
// method below for more information on this.
prefWid = 5 * size;
prefHt = 4 * size;
// Randomly place the CrazyLED in the panel
Random R = new Random();
int x = R.nextInt(prefWid - size);
int y = R.nextInt(prefHt - 2*size);
theLED = new CrazyLED(x, y, size);
// Make some buttons and put them in a new subpanel. We separate
// the panel with the buttons from the main panel so that the
// graphics don't interfere with the rendering of the components.
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 2));
bListener = new ButtonListener();
start = new JButton("Go");
start.addActionListener(bListener);
buttonPanel.add(start);
stop = new JButton("Stop");
stop.addActionListener(bListener);
stop.setEnabled(false);
buttonPanel.add(stop);
this.add(buttonPanel, BorderLayout.NORTH);
// Create a new Timer, setting the delay to rate milliseconds.
// The second argument to the constructor must be an
// ActionListener. Since the current object (a MyPanel object)
// implements ActionListener, we can pass it to the Timer constructor
// using the "this" keyword.
T = new Timer(rate, this);
}
// When a panel is added to a JFrame, the JFrame needs to determine how
// much space to give it. The getPreferredSize method allows the panel to
// "tell" the JFrame that it wants the specified amount of space. However,
// it is only preferred -- the JFrame is not obliged to give it this much
// space.
public Dimension getPreferredSize()
{
return new Dimension(prefWid, prefHt);
}
// Handle the buttons but starting and stopping the timer.
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == start)
{
T.start();
start.setEnabled(false);
stop.setEnabled(true);
}
else if (e.getSource() == stop)
{
T.stop();
stop.setEnabled(false);
start.setEnabled(true);
}
}
}
// To draw graphics within a JPanel in Java, we override the
// paintComponent() method. Experiment with this method by putting
// other graphical drawing code in here.
public void paintComponent(Graphics g)
{
super.paintComponent(g); // Make sure you call the super version
// before anything elses
Graphics2D g2 = (Graphics2D) g; // The Graphics2D class is a
// subclass of Graphics with added functionality. By
// casting we get access to this functionality, including
// the ability to draw simple objects such as
// Rectangle2D, Ellipse2D and Line2D
if (theLED != null) // It's a good idea to check for null here
theLED.draw(g2);
}
// The ActionListener that is fired by the Timer
public void actionPerformed(ActionEvent e)
{
theLED.mutate();
repaint(); // This method call requests that the graphics be redrawn
// See what happens if it is not done.
}
}
public abstract class LED implements Drawable
{
protected Line2D [] segments;
protected int size;
public LED(int X, int Y, int sz)
{
size = sz;
segments = new Line2D[7];
int startX = X, startY = Y;
int endX = startX + size, endY = startY;
segments[0] = new Line2D.Double(startX, startY, endX, endY);
startX = endX; startY = endY; endY = endY + size;
segments[1] = new Line2D.Double(startX, startY, endX, endY);
startX = endX; startY = endY; endY = endY + size;
segments[2] = new Line2D.Double(startX, startY, endX, endY);
startX = endX; startY = endY; endX = endX - size;
segments[3] = new Line2D.Double(startX, startY, endX, endY);
startX = endX; startY = endY; endY = endY - size;
segments[4] = new Line2D.Double(startX, startY, endX, endY);
startX = endX; startY = endY; endY = endY - size;
segments[5] = new Line2D.Double(startX, startY, endX, endY);
startX = X; startY = Y + size; endX = X + size; endY = startY;
segments[6] = new Line2D.Double(startX, startY, endX, endY);
}
public abstract void draw(Graphics2D g);
}
}