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

📄 canvaspanel.java

📁 可以话空心和实心的巨星
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package painter;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.*;

public class CanvasPanel extends JPanel implements MouseListener,MouseMotionListener, Serializable
{
	protected final static int LINE=1,SQUARE=2,OVAL=3,POLYGON=4,ROUND_RECT=5,FREE_HAND=6,
								SOLID_SQUARE=22, SOLID_OVAL=33, SOLID_POLYGON=44,
								SOLID_ROUND_RECT=55;
	protected static Vector vLine,vSquare,vOval,vPolygon,vRoundRect,vFreeHand,
						 	vSolidSquare,vSolidOval,vSolidPolygon,vSolidRoundRect,vFile,
						 	xPolygon, yPolygon;						 	
	
	private Stack undoStack, redoStack;
	
	private Color foreGroundColor, backGroundColor; 
	
	private int x1,y1,x2,y2,linex1,linex2,liney1,liney2, drawMode=0;
	private boolean solidMode, polygonBuffer;
	
	private File fileName;
					    
	public CanvasPanel()
	{
		vLine 			= new Vector();
		vSquare 		= new Vector();
		vOval			= new Vector();
		vPolygon		= new Vector();
		vRoundRect		= new Vector();
		vFreeHand		= new Vector();
		vSolidSquare	= new Vector();
		vSolidOval		= new Vector();
		vSolidPolygon	= new Vector();
		vSolidRoundRect	= new Vector();
		vFile			= new Vector();
		xPolygon		= new Vector();
		yPolygon		= new Vector();
		
		addMouseListener(this);
		addMouseMotionListener(this);
		
		solidMode 		= false;
		polygonBuffer 	= false; 
		
		foreGroundColor = Color.BLACK;
		backGroundColor = Color.WHITE;
		setBackground(backGroundColor);
		
		undoStack = new Stack();
		redoStack = new Stack();
		
		repaint();		
	}
/*----------------------------------------------------------------------------*/		
	public void mousePressed(MouseEvent event)
	{
		x1 = linex1 = linex2 = event.getX();
        y1 = liney1 = liney2 = event.getY();
	}
/*----------------------------------------------------------------------------*/
	public void mouseClicked(MouseEvent event){}
	public void mouseMoved(MouseEvent event){}
/*----------------------------------------------------------------------------*/
	public void mouseReleased(MouseEvent event)
	{
		if (drawMode == LINE)
        {	
           	vLine.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
           	undoStack.push(new StepInfo(LINE ,new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor)));
        }
        if (drawMode == SQUARE) 
        {
            if(solidMode)
           	{
           		if(x1 > event.getX() || y1 > event.getY())
				{
           			vSolidSquare.add(new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor));
           			undoStack.push(new StepInfo(SOLID_SQUARE, new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor)));
           		}
           		else
           		{
           			vSolidSquare.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
           			undoStack.push(new StepInfo(SOLID_SQUARE, new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor)));
           		}
           	}
            else
            {
           		if(x1 > event.getX() || y1 > event.getY())
           		{
           			vSquare.add(new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor));
           			undoStack.push(new StepInfo(SQUARE, new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor)));
           		}
           		else
           		{
           			vSquare.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
           			undoStack.push(new StepInfo(SQUARE, new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor)));
           		}
           	}
        }
        if (drawMode == this.OVAL) 
        {
          	if(solidMode)
          	{
          		if(x1 > event.getX() || y1 > event.getY())
          		{
          			vSolidOval.add(new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor));
          			undoStack.push(new StepInfo(SOLID_OVAL, new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor)));
          		}
          		else
          		{
          			vSolidOval.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
          			undoStack.push(new StepInfo(SOLID_OVAL, new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor)));
          		}
           	}
           	else
           	{
           		if(x1 > event.getX() || y1 > event.getY())
           		{
           			vOval.add(new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor));
           			undoStack.push(new StepInfo(OVAL, new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor)));
           		}
           		else	
           		{
           			vOval.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
           			undoStack.push(new StepInfo(OVAL, new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor)));
           		}
           	}
        }
        if (drawMode == this.POLYGON || drawMode == this.SOLID_POLYGON) 
        {
        	xPolygon.add(new Integer(event.getX()));
        	yPolygon.add(new Integer(event.getY()));
        	polygonBuffer = true;
        	repaint();       	      
        }
        if (drawMode == this.ROUND_RECT) 
        {
          	if(solidMode)
          	{
          		if(x1 > event.getX() || y1 > event.getY())
          		{
          			vSolidRoundRect.add(new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor));
          			undoStack.push(new StepInfo(SOLID_ROUND_RECT, new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor)));
          		}
          		else
          		{
           			vSolidRoundRect.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
           			undoStack.push(new StepInfo(SOLID_ROUND_RECT, new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor)));
           		}
           	}
           	else
           	{
           		if(x1 > event.getX() || y1 > event.getY())
           		{
           			vRoundRect.add(new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor));
           			undoStack.push(new StepInfo(ROUND_RECT, new Coordinate(event.getX(),event.getY(),x1,y1,foreGroundColor)));
           		}
           		else
           		{
           			vRoundRect.add(new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor));
           			undoStack.push(new StepInfo(ROUND_RECT, new Coordinate(x1,y1,event.getX(),event.getY(),foreGroundColor)));
           		}
           	}
        }           
        x1=linex1=x2=linex2=0;
        y1=liney1=y2=liney2=0;
	}
/*----------------------------------------------------------------------------*/
	public void mouseEntered(MouseEvent event)
	{
		setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
	}
/*----------------------------------------------------------------------------*/
	public void mouseExited(MouseEvent event)
	{
		setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
	}
/*----------------------------------------------------------------------------*/
	public void mouseDragged(MouseEvent event)
	{
        x2 = event.getX();
        y2 = event.getY();
                        
        if (drawMode == this.FREE_HAND) 
        {
            linex1 = linex2;
            liney1 = liney2;           
            linex2 = x2;
            liney2 = y2;
               
            vFreeHand.add(new Coordinate(linex1,liney1,linex2,liney2,foreGroundColor));
         	undoStack.push(new StepInfo(FREE_HAND, new Coordinate(linex1,liney1,linex2,liney2,foreGroundColor)));
         }
         repaint();
	}
/*----------------------------------------------------------------------------*/
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
      	redrawVectorBuffer(g);

	  	g.setColor(foreGroundColor);
      
      	if (drawMode == LINE) 
      	{
        	g.drawLine(x1,y1,x2,y2);
      	}
      	if (drawMode == OVAL) 
      	{
      	 	if(solidMode)
      	 	{
         		if(x1 > x2 || y1 > y2)
         			g.fillOval(x2,y2,x1-x2,y1-y2);
         		else	
         			g.fillOval(x1,y1,x2-x1,y2-y1);
      	 	}
         	else
         	{
         		if(x1 > x2 || y1 > y2)
         			g.drawOval (x2,y2,x1-x2,y1-y2);
         		else
         			g.drawOval (x1,y1,x2-x1,y2-y1);
         	}
      	}
      	if (drawMode == ROUND_RECT) 
      	{
         	if(solidMode)
         	{
         		if(x1 > x2 || y1 > y2)
         			g.fillRoundRect(x2,y2,x1-x2,y1-y2,25,25);
         		else
         			g.fillRoundRect(x1,y1,x2-x1,y2-y1,25,25);
         	}
         	else
         	{
         		if(x1 > x2 || y1 > y2)
         			g.drawRoundRect(x2,y2,x1-x2,y1-y2,25,25);
         		else
         			g.drawRoundRect(x1,y1,x2-x1,y2-y1,25,25);
         	}
      	}
      	if (drawMode == SQUARE) 
      	{
      	 	if(solidMode)
      	 	{
      	 		if(x1 > x2 || y1 > y2)
      	 			g.fillRect (x2,y2,x1-x2,y1-y2);
      	 		else
      	 			g.fillRect (x1,y1,x2-x1,y2-y1);
      	 	}
         	else
         	{
         		if(x1 > x2 || y1 > y2)
         			g.drawRect (x2,y2,x1-x2,y1-y2);
         		else
         			g.drawRect (x1,y1,x2-x1,y2-y1);
         	}
      	}
      	if (drawMode == POLYGON || drawMode == SOLID_POLYGON)
      	{
      		int xPos[] = new int[xPolygon.size()];
      	 	int yPos[] = new int[yPolygon.size()];
      	 
      	 	for(int count=0;count<xPos.length;count++)
      	 	{
      	 		xPos[count] = ((Integer)(xPolygon.elementAt(count))).intValue();
      	 		yPos[count] = ((Integer)(yPolygon.elementAt(count))).intValue();
      	 	}
      	 	g.drawPolyline(xPos,yPos,xPos.length);
      	 	polygonBuffer = true;
	  	}
      	if (drawMode == FREE_HAND) 
      	{
         	g.drawLine(linex1,liney1,linex2,liney2);
      	}
	}
/*----------------------------------------------------------------------------*/
	public void setDrawMode(int mode)
	{
		drawMode = mode;
	}
	public int getDrawMode()
	{	
		return drawMode;	
	}
/*----------------------------------------------------------------------------*/
	public void setSolidMode(Boolean inSolidMode)
	{
		solidMode = inSolidMode.booleanValue();
	}
	public Boolean getSolidMode()
	{
		return Boolean.valueOf(solidMode);
	}
/*----------------------------------------------------------------------------*/
	public void setForeGroundColor(Color inputColor)
	{
		foreGroundColor = inputColor;
	}
	public Color getForeGroundColor()
	{
		return foreGroundColor;
	}
/*----------------------------------------------------------------------------*/
	public void setBackGroundColor(Color inputColor)
	{
		backGroundColor = inputColor;
		this.setBackground(backGroundColor);
	}
	public Color getBackGroundColor()
	{
		return backGroundColor;
	}
/*----------------------------------------------------------------------------*/
	public void undo()
	{
		StepInfo tempInfo;
		
		if(undoStack.isEmpty())
			JOptionPane.showMessageDialog(null, "Can't Undo","Painter", JOptionPane.INFORMATION_MESSAGE);
		else
		{
			tempInfo = (StepInfo)undoStack.pop();

			switch(tempInfo.getStepType())
			{
				case 1:	vLine.remove(vLine.size()-1);
					break;
				case 2:	vSquare.remove(vSquare.size()-1);
					break;
				case 3:	vOval.remove(vOval.size()-1);
					break;
				case 4:	vPolygon.remove(vPolygon.size()-1);
					break;	
				case 5:	vRoundRect.remove(vRoundRect.size()-1);
					break;
				case 6:	vFreeHand.remove(vFreeHand.size()-1);
					break;
				case 22:vSolidSquare.remove(vSolidSquare.size()-1);
					break;
				case 33:vSolidOval.remove(vSolidOval.size()-1);
					break;
				case 44:vSolidPolygon.remove(vSolidPolygon.size()-1);
					break;
				case 55:vSolidRoundRect.remove(vSolidRoundRect.size()-1);
					break;
			}

⌨️ 快捷键说明

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