Needs: Spinner, Mover, Pic1, Pic2, Pic3, Pic4, Pic5, Pic6, Pic7, Pic8
Dragging over the entire folder from the I:// may be easier.
1. Draw a UML for the above situation. (Get a sheet of paper, Lazy.)
2. At least 4 objects are declared. What are their names and types?
3. Write out the line of code where the following occurs.
a. The image array is declared.
b. The pictures are loaded into the Image array.
c. The count on the image array is increased. (What if it goes beyond 8?)
d. The image is drawn
e. The thread has a nap.
f. The spinner object is declared.
g. A thread object is created.
h. A thread object is started.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*;
public class Mover extends JPanel implements ActionListener
{
spinner spinIT;
public Mover ()
{
super (new BorderLayout ());
spinIT = new spinner (50, 50); //a new selection spin object (other file)
setBackground (Color.white);
//spin button stuff
JButton spin = new JButton ("Start");
spin.setActionCommand ("Start");
spin.addActionListener (this);
//reset button stuff
JButton reset = new JButton ("Stop");
reset.setActionCommand ("Stop");
reset.addActionListener (this);
//panel & interface stuff JPanel p = new JPanel (new FlowLayout ()); p.setBackground (Color.white); p.add (spin); p.add (reset); add (p, "North"); add (spinIT, "Center"); }
public void actionPerformed (ActionEvent e)
{ //if spin button is presed
if (e.getActionCommand ().equals ("Start"))
{ //start a new thread and start spining
Thread spiningThread = new Thread (spinIT, "Spinnerthread");
spiningThread.start ();
}
else if (e.getActionCommand ().equals ("Stop"))
{ //stop the thread and repaint the randomized array
spinIT.stopSpinner ();
}
}
public static void main (String [] args)
{
//Create and set up the window.
JFrame frame = new JFrame ("Spinner Demo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane. JComponent newContentPane = new Mover (); frame.setContentPane (newContentPane); frame.setSize (150, 230);
//Display the window. frame.setVisible (true); } }
import java.awt.*; import javax.swing.*;
public class spinner extends JComponent implements Runnable
{
private int x;
private int y;
private boolean stopped;
Image picture [] = new Image [8];
int count = 0;
public spinner (int xx, int yy)
{ //constructor, sets up array
x = xx;
y = yy;
for (int i = 0 ; i < 8 ; i++)
{
String s = "pic" + (i + 1) + ".gif";
picture [i] = Toolkit.getDefaultToolkit ().getImage (s);
}
}
public void run ()
{ //run method, starts when "start" is pressed
stopped = false;
while (!stopped)
{
repaint ();
count++;
//sleep for a bit after drawing it
try
{
Thread.sleep (100);
}
catch (InterruptedException x)
{
;
}
}
}
public void stopSpinner ()
{
stopped = true;
}
public void paint (Graphics g)
{
g.drawImage (picture [(count % 8)], x, y, this);
}
}