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

📄 drawingboard.java

📁 环境配置:系统装有jre运行环境。 使用说明: 1.双击drawer.jar文件
💻 JAVA
字号:
///////////////////////////////////////////////////////////
// Name: Drawer                                          //
// Author:Zhanghan                                       //
// Date:  2005-9-10                                      //
// Email: zhang_han04@ncic.ac.cn                         //
///////////////////////////////////////////////////////////

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

//                   Drawing Board
public class DrawingBoard extends JPanel
    implements MouseListener, MouseMotionListener
{

    public static final Stroke STROKES[] = {
        new BasicStroke(1.0F), new BasicStroke(2.0F), new BasicStroke(5F), new BasicStroke(7.5F), new BasicStroke(10F)
    };

    private int tool = 0;
    private int strokeIndex = 0;
    private ArrayList shapes = null;
    public Shape currentShape = null;
    public static int cursor = 0; 

    public DrawingBoard()
    {
        shapes = new ArrayList();
        setCursor(new Cursor(1));
        setOpaque(true);
        setForeground(Color.black);
        setBackground(Color.white);
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    //Register Oval or Triangle
    public void setTool(int i)
    {
        if(i < 0 || i > 6)
        {
            throw new IllegalArgumentException("Invaild Tool Specified!");
        }
        else
        {
            tool = i;
            return;
        }
    }

    public void setStrokeIndex(int i)
    {
        if(i < 0 || i > 4)
        {
            throw new IllegalArgumentException("Invaild Weight Specified!");
        }
        else
        {
        	strokeIndex = i;
            currentShape.stroke = STROKES[i];
            currentShape.repaint();
            repaint();            
            return;
        }
    }

    public void setCurrentX(int x)
    {
            currentShape.currentX = x;
            currentShape.repaint();
            repaint();
            return;
    }

    public void setCurrentY(int y)
    {
            currentShape.currentY = y;
            currentShape.repaint();
            repaint();
            return;
    }

    public void setCurrentD(int d)
    {
            currentShape.currentD = d;
            currentShape.repaint();
            this.repaint();
            return;
    }

    //Clear The Board
    public void clearBoard()
    {
        shapes.clear();
        repaint();
    }

    protected void paintComponent(Graphics g)
    {
      super.paintComponent(g);
      int i = shapes.size();
      Graphics2D graphics2d = (Graphics2D)g;
      for(int j = 0; j < i; j++)
            ((Shape)shapes.get(j)).draw(graphics2d);
    }


    public void mousePressed(MouseEvent mouseevent)
    {
      if (cursor == 1)
          return;
      else{
        if (mouseevent.getButton() == 1) {//Press mouse's left button
          switch (tool) {
            case 0: // '\000'
              currentShape = new Oval(getForeground(), STROKES[strokeIndex],
                                      mouseevent.getX(), mouseevent.getY());
              break;
            case 1: // '\001'
              currentShape = new Triangle(getForeground(), STROKES[strokeIndex],
                                          mouseevent.getX(), mouseevent.getY());
              break;
          }
          shapes.add(currentShape);
          repaint();
        }
      }
    }

    public void mouseDragged(MouseEvent mouseevent)
    {
     		currentShape.mouseDragged(mouseevent);
    		repaint();
     }


    public void mouseReleased(MouseEvent mouseevent)
    {
        cursor = 1;
        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }


    public void mouseClicked(MouseEvent mouseevent)
    {      
    	if (mouseevent.getButton() == 3){//Press mouse's right button
    		AttributeDialog dialog = new AttributeDialog(this, strokeIndex);
      }else if (mouseevent.getButton() == 1) {//Press mouse's left button{
            currentShape.mouseDragged(mouseevent);
            repaint();
      }
    }

    public void mouseEntered(MouseEvent mouseevent)
    {
      if (cursor == 0)
        setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    }

    public void mouseExited(MouseEvent mouseevent) {}
    public void mouseMoved(MouseEvent mouseevent) {}

}

⌨️ 快捷键说明

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