⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawjpanel.java

📁 金旭亮的java教案
💻 JAVA
字号:
// DrawJPanel.java
//此类用于提供绘图表面
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//从JPanel中派生出一个绘图面板
public class DrawJPanel extends JPanel
{
   // 鼠标的当前座标
   private Point currentPoint;

   // 绘制图形的颜色
   private final Color DRAW_COLOR = Color.BLUE;
   private final int DRAW_DIAMETER = 8;//绘制线条宽度

   // 用于擦除图形的背景色
   private final Color ERASE_COLOR = getBackground();
   private final int ERASE_DIAMETER = 8;

   // 用于绘制圆的颜色和线宽
   private Color drawColor;
   private int drawDiameter;
   
   // 构造函数
   public DrawJPanel()
   {
      addMouseListener(

         new MouseListener() // 匿名内部类,对鼠标点击进行响应
         {
            // 单击
            public void mouseClicked( MouseEvent event )
            {
            }

            // 鼠标进入
            public void mouseEntered( MouseEvent event )
            {
            }

            // 鼠标离开
            public void mouseExited( MouseEvent event )
            {
            }

            // 按下鼠标按钮
            public void mousePressed( MouseEvent event )
            {
               drawJPanelMousePressed( event );
            }

            // 松开鼠标按钮
            public void mouseReleased( MouseEvent event )
            {
            }

         } // end anonymous inner class

      ); // end call to addMouseListener

	  //响应鼠标移动
      addMouseMotionListener(

         new MouseMotionListener() 
         {
            // 拖动鼠标
            public void mouseDragged( MouseEvent event )
            {
               drawJPanelMouseDragged( event );
            }

            // 鼠标移动,必须写,这是由接口规定的
            public void mouseMoved( MouseEvent event )
            {
            }

         } // end anonymous inner class

      ); // end call to addMouseMotionListener

   }  // end constructor

   // 当鼠标按下时画一个小圆
   private void drawJPanelMousePressed( MouseEvent event )
   {
      // 获取鼠标当前所在位置
      currentPoint = event.getPoint();

      if ( event.isMetaDown() ) // 按下右键
      {
      	//准备清除画面
         drawColor = ERASE_COLOR;
         drawDiameter = ERASE_DIAMETER;
      }
      else // 按下左键,准备绘图
      {
         drawColor = DRAW_COLOR;
         drawDiameter = DRAW_DIAMETER;
      }

      repaint(); // 重绘屏幕,将会导致paintComponent()方法被调用

   } // end method drawJPanelMousePressed

   // 在鼠标当前位置画一个小圆,覆盖了JComponent的同名方法
   public void paintComponent( Graphics g )
   {
      g.setColor( drawColor ); // set the color

      if ( currentPoint != null )
      {
         // 画圆
         g.fillOval( currentPoint.x, currentPoint.y, 
            drawDiameter, drawDiameter );
      }

   } // end method paintComponent

   // 鼠标拖动
   private void drawJPanelMouseDragged( MouseEvent event )
   {
      // store the location of the mouse
      currentPoint = event.getPoint();

      repaint(); //重绘屏幕
   } // end method drawJPanelMouseDragged

} // end class DrawJPanel

/**************************************************************************
 * (C) Copyright 1992-2004 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************/
 
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -