Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 319|回复: 0

[Swing学习]在Applet中如何鼠标拖曳

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-29 23:55:47 | 显示全部楼层 |阅读模式
    1. // CheckerDrag.java
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. public class CheckerDrag extends java.applet.Applet
    5. {
    6.    // dimension of checkerboard square
    7.    final static int SQUAREDIM = 40;
    8.    // dimension of checkerboard -- includes black outline
    9.    final static int BOARDDIM = 8 * SQUAREDIM + 2;
    10.    // dimension of checker -- 3/4 the dimension of a square
    11.    final static int CHECKERDIM = 3 * SQUAREDIM / 4;
    12.    // square colors are dark green or white
    13.    final static Color darkGreen = new Color (0, 128, 0);
    14.    // dragging flag -- set to true when user presses mouse button over checker
    15.    // and cleared to false when user releases mouse button
    16.    boolean inDrag = false;
    17.    // left coordinate of checkerboard"s upperleft corner
    18.    int boardx;
    19.    // top coordinate of checkerboard"s upperleft corner
    20.    int boardy;
    21.    // left coordinate of checker rectangle origin (upperleft corner)
    22.    int ox;
    23.    // top coordinate of checker rectangle origin (upperleft corner)
    24.    int oy;
    25.    // left displacement between mouse coordinates at time of press and checker
    26.    // rectangle origin
    27.    int relx;
    28.    // top displacement between mouse coordinates at time of press and checker
    29.    // rectangle origin
    30.    int rely;
    31.    // width of applet drawing area
    32.    int width;
    33.    // height of applet drawing area
    34.    int height;
    35.    // image buffer
    36.    Image imBuffer;
    37.    // graphics context associated with image buffer
    38.    Graphics imG;
    39.    public void init ()
    40.    {
    41.       // Obtain the size of the applet"s drawing area.
    42.       width = getSize ().width;
    43.       height = getSize ().height;
    44.       // Create image buffer.
    45.       imBuffer = createImage (width, height);
    46.       // Retrieve graphics context associated with image buffer.
    47.       imG = imBuffer.getGraphics ();
    48.       // Initialize checkerboard"s origin, so that board is centered.
    49.       boardx = (width - BOARDDIM) / 2 + 1;
    50.       boardy = (height - BOARDDIM) / 2 + 1;
    51.       // Initialize checker"s rectangle"s starting origin so that checker is
    52.       // centered in the square located in the top row and second column from
    53.       // the left.
    54.       ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1;
    55.       oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1;
    56.       // Attach a mouse listener to the applet. That listener listens for
    57.       // mouse button press and mouse button release events.
    58.       addMouseListener (new MouseAdapter ()
    59.                         {
    60.                             public void mousePressed (MouseEvent e)
    61.                             {
    62.                                // Obtain mouse coordinates at time of press.
    63.                                int x = e.getX ();
    64.                                int y = e.getY ();
    65.                                // If mouse is over draggable checker at time
    66.                                // of press (i.e., contains (x, y) returns
    67.                                // true), save distance between current mouse
    68.                                // coordinates and draggable checker origin
    69.                                // (which will always be positive) and set drag
    70.                                // flag to true (to indicate drag in progress).
    71.                                if (contains (x, y))
    72.                                {
    73.                                    relx = x - ox;
    74.                                    rely = y - oy;
    75.                                    inDrag = true;
    76.                                }
    77.                             }
    78.                             boolean contains (int x, int y)
    79.                             {
    80.                                // Calculate center of draggable checker.
    81.                                int cox = ox + CHECKERDIM / 2;
    82.                                int coy = oy + CHECKERDIM / 2;
    83.                                // Return true if (x, y) locates with bounds
    84.                                // of draggable checker. CHECKERDIM / 2 is the
    85.                                // radius.
    86.                                return (cox - x) * (cox - x) +
    87.                                       (coy - y) * (coy - y) <
    88.                                       CHECKERDIM / 2 * CHECKERDIM / 2;
    89.                             }
    90.                             public void mouseReleased (MouseEvent e)
    91.                             {
    92.                                // When mouse released, clear inDrag (to
    93.                                // indicate no drag in progress) if inDrag is
    94.                                // already set.
    95.                                if (inDrag)
    96.                                    inDrag = false;
    97.                             }
    98.                         });
    99.       // Attach a mouse motion listener to the applet. That listener listens
    100.       // for mouse drag events.
    101.       addMouseMotionListener (new MouseMotionAdapter ()
    102.                               {
    103.                                   public void mouseDragged (MouseEvent e)
    104.                                   {
    105.                                      if (inDrag)
    106.                                      {
    107.                                          // Calculate draggable checker"s new
    108.                                          // origin (the upper-left corner of
    109.                                          // the checker rectangle).
    110.                                          int tmpox = e.getX () - relx;
    111.                                          int tmpoy = e.getY () - rely;
    112.                                          // If the checker is not being moved
    113.                                          // (at least partly) off board,
    114.                                          // assign the previously calculated
    115.                                          // origin (tmpox, tmpoy) as the
    116.                                          // permanent origin (ox, oy), and
    117.                                          // redraw the display area (with the
    118.                                          // draggable checker at the new
    119.                                          // coordinates.
    120.                                          if (tmpox > boardx &&
    121.                                              tmpoy > boardy &&
    122.                                              tmpox + CHECKERDIM
    123.                                              < boardx + BOARDDIM &&
    124.                                              tmpoy + CHECKERDIM
    125.                                              < boardy + BOARDDIM)
    126.                                          {
    127.                                              ox = tmpox;
    128.                                              oy = tmpoy;
    129.                                              repaint ();
    130.                                          }
    131.                                      }
    132.                                   }
    133.                               });
    134.    }
    135.    public void paint (Graphics g)
    136.    {
    137.       // Paint the checkerboard over which the checker will be dragged.
    138.       paintCheckerBoard (imG, boardx, boardy);
    139.       // Paint the checker that will be dragged.
    140.       paintChecker (imG, ox, oy);
    141.       // Draw contents of image buffer.
    142.       g.drawImage (imBuffer, 0, 0, this);
    143.    }
    144.    void paintChecker (Graphics g, int x, int y)
    145.    {
    146.       // Set checker shadow color.
    147.       g.setColor (Color.black);
    148.       // Paint checker shadow.
    149.       g.fillOval (x, y, CHECKERDIM, CHECKERDIM);
    150.       // Set checker color.
    151.       g.setColor (Color.red);
    152.       // Paint checker.
    153.       g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13);
    154.    }
    155.    void paintCheckerBoard (Graphics g, int x, int y)
    156.    {
    157.       // Paint checkerboard outline.
    158.       g.setColor (Color.black);
    159.       g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1);
    160.       // Paint checkerboard.
    161.       for (int row = 0; row < 8; row++)
    162.       {
    163.            g.setColor (((row & 1) != 0) ? darkGreen : Color.white);
    164.            for (int col = 0; col < 8; col++)
    165.            {
    166.                 g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM,
    167.                             SQUAREDIM, SQUAREDIM);
    168.                 g.setColor ((g.getColor () == darkGreen) ? Color.white :
    169.                             darkGreen);
    170.            }
    171.       }
    172.    }
    173.    // The AWT invokes the update() method in response to the repaint() method
    174.    // calls that are made as a checker is dragged. The default implementation
    175.    // of this method, which is inherited from the Container class, clears the
    176.    // applet"s drawing area to the background color prior to calling paint().
    177.    // This clearing followed by drawing causes flicker. CheckerDrag overrides
    178.    // update() to prevent the background from being cleared, which eliminates
    179.    // the flicker.
    180.    public void update (Graphics g)
    181.    {
    182.       paint (g);
    183.    }
    184. }
    复制代码
    程序运行图:
      

      
      
       
       

         
       

         
       
      



    源码下载:http://file.javaxxz.com/2014/10/29/235547375.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-18 20:18 , Processed in 0.399128 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表