TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- // CheckerDrag.java
- import java.awt.*;
- import java.awt.event.*;
- public class CheckerDrag extends java.applet.Applet
- {
- // dimension of checkerboard square
- final static int SQUAREDIM = 40;
- // dimension of checkerboard -- includes black outline
- final static int BOARDDIM = 8 * SQUAREDIM + 2;
- // dimension of checker -- 3/4 the dimension of a square
- final static int CHECKERDIM = 3 * SQUAREDIM / 4;
- // square colors are dark green or white
- final static Color darkGreen = new Color (0, 128, 0);
- // dragging flag -- set to true when user presses mouse button over checker
- // and cleared to false when user releases mouse button
- boolean inDrag = false;
- // left coordinate of checkerboard"s upperleft corner
- int boardx;
- // top coordinate of checkerboard"s upperleft corner
- int boardy;
- // left coordinate of checker rectangle origin (upperleft corner)
- int ox;
- // top coordinate of checker rectangle origin (upperleft corner)
- int oy;
- // left displacement between mouse coordinates at time of press and checker
- // rectangle origin
- int relx;
- // top displacement between mouse coordinates at time of press and checker
- // rectangle origin
- int rely;
- // width of applet drawing area
- int width;
- // height of applet drawing area
- int height;
- // image buffer
- Image imBuffer;
- // graphics context associated with image buffer
- Graphics imG;
- public void init ()
- {
- // Obtain the size of the applet"s drawing area.
- width = getSize ().width;
- height = getSize ().height;
- // Create image buffer.
- imBuffer = createImage (width, height);
- // Retrieve graphics context associated with image buffer.
- imG = imBuffer.getGraphics ();
- // Initialize checkerboard"s origin, so that board is centered.
- boardx = (width - BOARDDIM) / 2 + 1;
- boardy = (height - BOARDDIM) / 2 + 1;
- // Initialize checker"s rectangle"s starting origin so that checker is
- // centered in the square located in the top row and second column from
- // the left.
- ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1;
- oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1;
- // Attach a mouse listener to the applet. That listener listens for
- // mouse button press and mouse button release events.
- addMouseListener (new MouseAdapter ()
- {
- public void mousePressed (MouseEvent e)
- {
- // Obtain mouse coordinates at time of press.
- int x = e.getX ();
- int y = e.getY ();
- // If mouse is over draggable checker at time
- // of press (i.e., contains (x, y) returns
- // true), save distance between current mouse
- // coordinates and draggable checker origin
- // (which will always be positive) and set drag
- // flag to true (to indicate drag in progress).
- if (contains (x, y))
- {
- relx = x - ox;
- rely = y - oy;
- inDrag = true;
- }
- }
- boolean contains (int x, int y)
- {
- // Calculate center of draggable checker.
- int cox = ox + CHECKERDIM / 2;
- int coy = oy + CHECKERDIM / 2;
- // Return true if (x, y) locates with bounds
- // of draggable checker. CHECKERDIM / 2 is the
- // radius.
- return (cox - x) * (cox - x) +
- (coy - y) * (coy - y) <
- CHECKERDIM / 2 * CHECKERDIM / 2;
- }
- public void mouseReleased (MouseEvent e)
- {
- // When mouse released, clear inDrag (to
- // indicate no drag in progress) if inDrag is
- // already set.
- if (inDrag)
- inDrag = false;
- }
- });
- // Attach a mouse motion listener to the applet. That listener listens
- // for mouse drag events.
- addMouseMotionListener (new MouseMotionAdapter ()
- {
- public void mouseDragged (MouseEvent e)
- {
- if (inDrag)
- {
- // Calculate draggable checker"s new
- // origin (the upper-left corner of
- // the checker rectangle).
- int tmpox = e.getX () - relx;
- int tmpoy = e.getY () - rely;
- // If the checker is not being moved
- // (at least partly) off board,
- // assign the previously calculated
- // origin (tmpox, tmpoy) as the
- // permanent origin (ox, oy), and
- // redraw the display area (with the
- // draggable checker at the new
- // coordinates.
- if (tmpox > boardx &&
- tmpoy > boardy &&
- tmpox + CHECKERDIM
- < boardx + BOARDDIM &&
- tmpoy + CHECKERDIM
- < boardy + BOARDDIM)
- {
- ox = tmpox;
- oy = tmpoy;
- repaint ();
- }
- }
- }
- });
- }
- public void paint (Graphics g)
- {
- // Paint the checkerboard over which the checker will be dragged.
- paintCheckerBoard (imG, boardx, boardy);
- // Paint the checker that will be dragged.
- paintChecker (imG, ox, oy);
- // Draw contents of image buffer.
- g.drawImage (imBuffer, 0, 0, this);
- }
- void paintChecker (Graphics g, int x, int y)
- {
- // Set checker shadow color.
- g.setColor (Color.black);
- // Paint checker shadow.
- g.fillOval (x, y, CHECKERDIM, CHECKERDIM);
- // Set checker color.
- g.setColor (Color.red);
- // Paint checker.
- g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13);
- }
- void paintCheckerBoard (Graphics g, int x, int y)
- {
- // Paint checkerboard outline.
- g.setColor (Color.black);
- g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1);
- // Paint checkerboard.
- for (int row = 0; row < 8; row++)
- {
- g.setColor (((row & 1) != 0) ? darkGreen : Color.white);
- for (int col = 0; col < 8; col++)
- {
- g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM,
- SQUAREDIM, SQUAREDIM);
- g.setColor ((g.getColor () == darkGreen) ? Color.white :
- darkGreen);
- }
- }
- }
- // The AWT invokes the update() method in response to the repaint() method
- // calls that are made as a checker is dragged. The default implementation
- // of this method, which is inherited from the Container class, clears the
- // applet"s drawing area to the background color prior to calling paint().
- // This clearing followed by drawing causes flicker. CheckerDrag overrides
- // update() to prevent the background from being cleared, which eliminates
- // the flicker.
- public void update (Graphics g)
- {
- paint (g);
- }
- }
复制代码 程序运行图:
源码下载:http://file.javaxxz.com/2014/10/29/235547375.zip |
|