home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-06-17 | 2.6 KB | 114 lines |
- //**************************************************************
- // ConnectTheDots.java: Applet
- //
- //**************************************************************
- import java.applet.*;
- import java.awt.*;
- import java.util.Vector;
-
- public class ConnectTheDots extends Applet
- {
- // the following is a recording of all
- // previous clicked locations
- Vector m_vLocs;
-
- // the current cursor location
- Dimension m_dimCursorLoc;
-
- public ConnectTheDots()
- {
- m_vLocs = new Vector();
- m_dimCursorLoc = new Dimension(0, 0);
- }
-
- public String getAppletInfo()
- {
- return "Name: ConnectTheDots\r\n" +
- "Author: Stephen R. Davis\r\n" +
- "Created for Learn Java Now (c)";
- }
-
-
- public void init()
- {
- resize(400, 400);
-
- }
-
- public void destroy()
- {
- }
-
- public void paint(Graphics g)
- {
- // put a cross where the cursor is located
- int nX = m_dimCursorLoc.width;
- int nY = m_dimCursorLoc.height;
- g.drawLine(nX - 2, nY, nX + 2, nY);
- g.drawLine( nX, nY - 2, nX, nY + 2);
-
- // now draw a line from each clicked location
- // to every other clicked location
- Dimension dimFrom;
- Dimension dimTo;
- int nSize = m_vLocs.size();
- for (int i = 0; i < nSize - 1; i++)
- {
- dimFrom = (Dimension)m_vLocs.elementAt(i);
- for (int j = i + 1; j < nSize; j++)
- {
- dimTo = (Dimension)m_vLocs.elementAt(j);
- g.drawLine(dimFrom.width, dimFrom.height,
- dimTo.width, dimTo.height );
- }
- }
- }
-
- public void start()
- {
- }
-
- public void stop()
- {
- }
-
-
- public boolean mouseDown(Event evt, int x, int y)
- {
- // in the event of a double click...
- if (evt.clickCount > 1)
- {
- // ...remove all elements from the vector...
- m_vLocs.removeAllElements();
- }
- else
- {
- // ...otherwise, add an element
- m_vLocs.addElement(new Dimension(x, y));
- }
- repaint();
- return true;
- }
-
- public boolean mouseUp(Event evt, int x, int y)
- {
- // ignore the mouseUp event
- return true;
- }
-
- public boolean mouseDrag(Event evt, int x, int y)
- {
- // ignore the mouseDrag event
- return true;
- }
-
- public boolean mouseMove(Event evt, int x, int y)
- {
- // record the mouse location and repaint it
- m_dimCursorLoc = new Dimension(x, y);
- repaint();
- return true;
- }
-
- }
-