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

📄 mypanel.java

📁 Java画板
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package test.paint;

import java.awt.*;
import java.awt.event.*;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.*;
import javax.swing.undo.*; 

import java.util.ArrayList;
import javax.swing.filechooser.*;
/**
 * 设置画板
 *
 */

/**
 * 作者:钟雯 王珍 曾燕秋
 * 实现画板画图的各项功能
 *  初始时间:2007 5-17
 * 最后一次修改时间:2007 6-17
 */

public class MyPanel extends JPanel implements 
MouseListener, MouseMotionListener {
	    
	    //flag判断多边形是否是第一笔画
	    public int flag=0;
	    //s,s2分别记录多边形第一个点的横,纵坐标,j=0代表第一次画多边形
	    public int s1, s2, j=1;
	    
	    public static final int TOOL_LINE = 0;
	    public static final int TOOL_RECT = 1;
	    public static final int TOOL_OVAL = 2;
	    public static final int TOOL_DIAMOND = 3;
	    public static final int TOOL_PENCIL = 4;
	    public static final int TOOL_ERASER = 5;
	    public static final int TOOL_POLYGON = 6;
	    public static final int TOOL_WORD = 7;
	    public static final int MODEL1 = 0;
	    public static final int MODEL2 = 1;
	    public static final int MODEL3 = 2;
	    public static final int TOOL_SQUARE1=8;
	    public static final int TOOL_SQUARE2=9;
	    public static final int TOOL_SQUARE3=10;
	    public static final int TOOL_CIRCLE1=11;
	    public static final int TOOL_CIRCLE2=12;
	    public static final int TOOL_CIRCLE3=13;
	    public static final int TOOL_FILL = 14;
	    public static final int TOOL_ROUNDRECT = 15;
	    //public  MyPanel myPanel;

	    //声明Stroke的大小
	    public static final Stroke[] STROKES = new Stroke[] {
	        new BasicStroke(1.0f),
	        new BasicStroke(2.0f),
	        new BasicStroke(5.0f),
	        new BasicStroke(7.5f),
	        new BasicStroke(10.0f),
	        new BasicStroke(15.0f)
	    };
	    
	    //定义ERASER_STROKE的大小
	    public static final Stroke[] ERASER_STROKES = new Stroke[] {
	        new BasicStroke(15.0f),
	        new BasicStroke(20.0f),
	        new BasicStroke(30.0f),
	        new BasicStroke(50.0f),
	        new BasicStroke(100.0f)
	    };
	    
	    //声明undo,用于撤消
		private ArrayList undo = new ArrayList();
		
	    private ArrayList shapes;
	    
	    //定义PointSet的对象
	    private PointsSet point;
	   
	    
	    private MyShape currentShape;
	    
	    private int tool;
	    
        private int x1, x2, x3 = 7;
        
        //m用于模板
        private int m;   
	    
	    private String temp ;
	    
	    private int shapeID; 
	    
	    private int strokeIndex, eraserIndex;
	    
	    public MyPanel() {
	    	point = new PointsSet();
	        shapes = new ArrayList();
	        tool = TOOL_LINE;
	        currentShape = null;
	        strokeIndex = 0;
	        eraserIndex = 0;
	        x1 =0;
	        x2 =0;
	        temp = "宋体";
	        setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
	        setOpaque(true);
	        setForeground(Color.black);
	        setBackground(Color.white);
	        
	        addMouseListener(this);
	        addMouseMotionListener(this);
	    }
	    
	    public int getIntersectsShape(double x, double y, double w, double h) {
	        for (int i = shapes.size() - 1; i >= 0; i--) {
	          if ( ( (MyShape)shapes.get(i)).intersects(x, y, w, h)) {
	            return i;
	          }
	        }
	        return -1;
	      }
	    
		
	      public void fillShape(int index, Color col) {
	        ( (MyShape) shapes.get(index)).setIsFill(true);
	        ( (MyShape)shapes.get(index)).setColor(col);
	        repaint();
	      }
	    
	      /**
	       * 撤消
	       *
	       */
	    public void unDo() {
	        if ( canUnDo()) {
	          undo.add(shapes.remove(shapes.size() - 1));
	          //rePaintArea();
	          repaint();
	        }
	      }
	    
	    /**
	     * 判断是否可以撤消
	     * @return
	     */
	    public boolean canUnDo() {
	        return !shapes.isEmpty();
	      }
	    
	    /**
	     * 重做
	     */
	    public void reDo() {
	        if (canReDo()) {
	          shapes.add(undo.remove(undo.size() - 1));
	          //rePaintArea();
	          repaint();
	        }
	     }
	    /**
	     * 判断是否可以重做
	     * @return
	     */
	    public boolean canReDo(){
	        return !undo.isEmpty();
	      }
	    
	    /**
	     * 判断画布是否为空
	     * @return boolean
	     */
	    public boolean isEmpty()
	    {
	      return shapes.isEmpty();
	    }
	    
	   
	    public void setTool(int t) {
	        if (t < TOOL_LINE || t > TOOL_ROUNDRECT )
	            throw new IllegalArgumentException("Invaild Tool Specified!");
	        tool = t;
	    }
	    
	    public void setModel(int n)
	    {
	    	if( n < 0 || n > 2)
	    		throw new IllegalArgumentException("Invaild Tool Specified!");
	    	m = n;
	    }
	    
	    public void setStyle1(String x)
	    {
	    	temp = x;
	    	
	    }
	    
	    public void setStyle2(int a, int b)
	    {
	    	x1 = a;
	    	x2 = b;
	    	
	    }
	    
	    public void setNumber(int c)
	    {
	    	x3 = c;
	    }
	    
	    public void setStrokeIndex(int i) {
	        if (i < 0 || i > 5)
	            throw new IllegalArgumentException("Invaild Weight Specified!");
	        strokeIndex = i;
	    }
	    
	    /**
	     * 返回选中的橡皮擦大小
	     * @param i
	     */
	    public void setEraserIndex(int i) {
	        if (i < 0 || i > 4)
	            throw new IllegalArgumentException("Invaild Size Specified!");
	        eraserIndex = i;
	    }
	    
	    /**
	     * 清除画板
	     *
	     */
	    public void clearBoard() {
	        shapes.clear();
	        repaint();
	        flag = 0;
	    }
	    
	    /**
	     * 
	     * @param element

⌨️ 快捷键说明

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