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

📄 canvaspanel.java

📁 可以话空心和实心的巨星
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			redoStack.push(tempInfo);
		}
		repaint();
	}
/*----------------------------------------------------------------------------*/
	public void redo()
	{
		StepInfo tempInfo;
		
		if(redoStack.isEmpty())
			JOptionPane.showMessageDialog(null,"Can't Redo","Painter",JOptionPane.INFORMATION_MESSAGE);
		else
		{
			tempInfo = (StepInfo)redoStack.pop();
			
			switch(tempInfo.getStepType())
			{
				case 1:	vLine.add(tempInfo.getStepCoordinate());
					break;
				case 2:	vSquare.add(tempInfo.getStepCoordinate());
					break;
				case 3:	vOval.add(tempInfo.getStepCoordinate());
					break;
				case 4:	vPolygon.add(tempInfo.getStepCoordinate());
					break;	
				case 5:	vRoundRect.add(tempInfo.getStepCoordinate());
					break;
				case 6:	vFreeHand.add(tempInfo.getStepCoordinate());
					break;
				case 22:vSolidSquare.add(tempInfo.getStepCoordinate());
					break;
				case 33:vSolidOval.add(tempInfo.getStepCoordinate());
					break;
				case 44:vSolidPolygon.add(tempInfo.getStepCoordinate());
					break;
				case 55:vSolidRoundRect.add(tempInfo.getStepCoordinate());
					break;
			}
			undoStack.push(tempInfo);
		}
		repaint();
	}
/*----------------------------------------------------------------------------*/
	public void clearCanvas()
	{
		vFreeHand.removeAllElements();
		vLine.removeAllElements();
		vOval.removeAllElements();
		vPolygon.removeAllElements();
		vRoundRect.removeAllElements();
		vSolidOval.removeAllElements();
		vSolidPolygon.removeAllElements();
		vSolidRoundRect.removeAllElements();
		vSolidSquare.removeAllElements();
		vSquare.removeAllElements();
		undoStack.clear();
		redoStack.clear();
		repaint();
	}
/*----------------------------------------------------------------------------*/	
	public void SaveCanvasToFile()
	{
		if(fileName != null)
		{
			vFile.removeAllElements();
			vFile.addElement(vFreeHand);
			vFile.addElement(vLine);
			vFile.addElement(vOval);
			vFile.addElement(vPolygon);
			vFile.addElement(vRoundRect);
			vFile.addElement(vSolidOval);
			vFile.addElement(vSolidPolygon);
			vFile.addElement(vSolidRoundRect);
			vFile.addElement(vSolidSquare);
			vFile.addElement(vSquare);
			vFile.addElement(new Color(backGroundColor.getRGB()));
			RenderedImage rendImage = myCreateImage();
			
			try
			{
				FileOutputStream fos = new FileOutputStream(fileName);
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				oos.writeObject(vFile);
				JOptionPane.showMessageDialog(null,"File Saved","Painter",JOptionPane.INFORMATION_MESSAGE);
			}catch(Exception exp){}
			
			try 
			{
        		File file = new File(fileName.toString() + ".jpg");        		
        		ImageIO.write(rendImage, "jpg", file);
    		}catch (IOException e) {}
		}
		else
		{
			SaveAsCanvasToFile();
		}
		repaint();
	}
/*----------------------------------------------------------------------------*/
	public void SaveAsCanvasToFile()
	{
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);	
		int result = fileChooser.showSaveDialog(null);
	
		if(result == JFileChooser.CANCEL_OPTION) return;
			
		fileName = fileChooser.getSelectedFile();

		if(fileName == null || fileName.getName().equals(""))
			JOptionPane.showMessageDialog(null,"Invalid File Name","Painter",JOptionPane.ERROR_MESSAGE);
		else
		{
			vFile.removeAllElements();
			vFile.addElement(vFreeHand);
			vFile.addElement(vLine);
			vFile.addElement(vOval);
			vFile.addElement(vPolygon);
			vFile.addElement(vRoundRect);
			vFile.addElement(vSolidOval);
			vFile.addElement(vSolidPolygon);
			vFile.addElement(vSolidRoundRect);
			vFile.addElement(vSolidSquare);
			vFile.addElement(vSquare);	
			vFile.addElement(new Color(backGroundColor.getRGB()));
			
			RenderedImage rendImage = myCreateImage();
			
			try
			{
				FileOutputStream fos = new FileOutputStream(fileName);
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				oos.writeObject(vFile);
				JOptionPane.showMessageDialog(null,"File Saved","Painter",JOptionPane.INFORMATION_MESSAGE);
			}catch(Exception exp){}
			
			try {
        		File file = new File(fileName.toString() + ".jpg");        		
        		ImageIO.write(rendImage, "jpg", file);
    		}catch (IOException e) {}
		}		    
	repaint();
	}
/*----------------------------------------------------------------------------*/
	public void OpenCanvasFile()
	{
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
			
		int result = fileChooser.showOpenDialog(null);
		if(result == JFileChooser.CANCEL_OPTION) return;
			
		fileName = fileChooser.getSelectedFile();
		
		if(fileName != null)
		{
			try{
				FileInputStream fis = new FileInputStream(fileName);
				ObjectInputStream ois = new ObjectInputStream(fis);
				vFile = (Vector) ois.readObject();
				
				this.clearCanvas();
				vFreeHand 		= (Vector)vFile.elementAt(0);
				vLine 			= (Vector)vFile.elementAt(1);
				vOval			= (Vector)vFile.elementAt(2);
				vPolygon		= (Vector)vFile.elementAt(3);
				vRoundRect		= (Vector)vFile.elementAt(4);
				vSolidOval		= (Vector)vFile.elementAt(5);
				vSolidPolygon	= (Vector)vFile.elementAt(6);
				vSolidRoundRect	= (Vector)vFile.elementAt(7);
				vSolidSquare	= (Vector)vFile.elementAt(8);
				vSquare			= (Vector)vFile.elementAt(9);
				backGroundColor = (Color)vFile.elementAt(10);
				
				this.setBackground(backGroundColor);
			}
			catch(Exception exp){
				JOptionPane.showMessageDialog(null,"Can't Open File","Painter",JOptionPane.INFORMATION_MESSAGE);
			}	
		}
		else{
			fileName = null;
		}
		repaint();
	}
/*----------------------------------------------------------------------------*/
	public boolean isExistPolygonBuffer()
	{
		return polygonBuffer;
	} 
/*----------------------------------------------------------------------------*/	
	public void flushPolygonBuffer()
	{
		if(!solidMode)
		{
			vPolygon.add(new Coordinate(xPolygon, yPolygon, foreGroundColor));
			undoStack.push(new StepInfo(POLYGON,new Coordinate(xPolygon, yPolygon, foreGroundColor)));
		}
		else
		{
			vSolidPolygon.add(new Coordinate(xPolygon, yPolygon, foreGroundColor));
			undoStack.push(new StepInfo(SOLID_POLYGON,new Coordinate(xPolygon, yPolygon, foreGroundColor)));
		}
		
		xPolygon.removeAllElements();
		yPolygon.removeAllElements();
			
		polygonBuffer = false;
		repaint();
	}
/*----------------------------------------------------------------------------*/
	private class Coordinate implements Serializable
	{
		private int x1,y1,x2,y2;
		private Color foreColor;
		private Vector xPoly, yPoly;
		
		public Coordinate (int inx1,int iny1,int inx2, int iny2, Color color) 
		{
        	x1 = inx1;
         	y1 = iny1;
         	x2 = inx2;
         	y2 = iny2;
         	foreColor = color;
      	}
      	public Coordinate(Vector inXPolygon, Vector inYPolygon, Color color)
      	{
      		xPoly = (Vector)inXPolygon.clone();
      		yPoly = (Vector)inYPolygon.clone();
      		foreColor = color;
      	}
      	public Color colour()
      	{
        	return foreColor;
      	}
      	public int getX1 () 
      	{
        	return x1;
      	}
      	public int getX2 () 
      	{
        	return x2;
      	}
      	public int getY1 () 
      	{
        	return y1;
      	}
      	public int getY2 () 
      	{
        	return y2;
      	}
      	public Vector getXPolygon()
      	{
      		return xPoly;
      	}
      	public Vector getYPolygon()
      	{
      		return yPoly;
      	}
	}		
/*----------------------------------------------------------------------------*/	
	private class StepInfo implements Serializable
	{
		private int stepType;
		private Coordinate stepCoordinate;
		
		public StepInfo(int inStepType, Coordinate inStepCoordinate)
		{
			stepType = inStepType;
			stepCoordinate = inStepCoordinate;
		}
		public int getStepType()
		{
			return stepType;
		}
		public Coordinate getStepCoordinate()
		{
			return stepCoordinate;
		}
	}
/*----------------------------------------------------------------------------*/	
	private RenderedImage myCreateImage() 
	{
        BufferedImage bufferedImage = new BufferedImage(600,390, BufferedImage.TYPE_INT_RGB);

        Graphics g = bufferedImage.createGraphics();
    	redrawVectorBuffer(g);

      	g.dispose();
      	return bufferedImage;
    }
/*----------------------------------------------------------------------------*/	
    private void redrawVectorBuffer(Graphics g)
    {
    	for (int i=0;i<vFreeHand.size();i++){
        	g.setColor(((Coordinate)vFreeHand.elementAt(i)).colour());
         	g.drawLine(((Coordinate)vFreeHand.elementAt(i)).getX1(),((Coordinate)vFreeHand.elementAt(i)).getY1(),((Coordinate)vFreeHand.elementAt(i)).getX2(),((Coordinate)vFreeHand.elementAt(i)).getY2());
      	}
      	for (int i=0;i<vLine.size();i++){
         	g.setColor(((Coordinate)vLine.elementAt(i)).colour());
         	g.drawLine(((Coordinate)vLine.elementAt(i)).getX1(),((Coordinate)vLine.elementAt(i)).getY1(),((Coordinate)vLine.elementAt(i)).getX2(),((Coordinate)vLine.elementAt(i)).getY2());
      	}
	  	for (int i=0;i<vOval.size();i++){	
         	g.setColor(((Coordinate)vOval.elementAt(i)).colour());
         	g.drawOval(((Coordinate)vOval.elementAt(i)).getX1(),((Coordinate)vOval.elementAt(i)).getY1(),((Coordinate)vOval.elementAt(i)).getX2()-((Coordinate)vOval.elementAt(i)).getX1(),((Coordinate)vOval.elementAt(i)).getY2()-((Coordinate)vOval.elementAt(i)).getY1());
      	}
      	for (int i=0;i<vRoundRect.size();i++){
         	g.setColor(((Coordinate)vRoundRect.elementAt(i)).colour());
         	g.drawRoundRect(((Coordinate)vRoundRect.elementAt(i)).getX1(),((Coordinate)vRoundRect.elementAt(i)).getY1(),((Coordinate)vRoundRect.elementAt(i)).getX2()-((Coordinate)vRoundRect.elementAt(i)).getX1(),((Coordinate)vRoundRect.elementAt(i)).getY2()-((Coordinate)vRoundRect.elementAt(i)).getY1(),25,25);
      	}
      	for (int i=0;i<vSolidOval.size();i++){
         	g.setColor(((Coordinate)vSolidOval.elementAt(i)).colour());
         	g.fillOval(((Coordinate)vSolidOval.elementAt(i)).getX1(),((Coordinate)vSolidOval.elementAt(i)).getY1(),((Coordinate)vSolidOval.elementAt(i)).getX2()-((Coordinate)vSolidOval.elementAt(i)).getX1(),((Coordinate)vSolidOval.elementAt(i)).getY2()-((Coordinate)vSolidOval.elementAt(i)).getY1());
      	}
      	for (int i=0;i<vSolidRoundRect.size();i++){
         	g.setColor(((Coordinate)vSolidRoundRect.elementAt(i)).colour());
       	 	g.fillRoundRect(((Coordinate)vSolidRoundRect.elementAt(i)).getX1(),((Coordinate)vSolidRoundRect.elementAt(i)).getY1(),((Coordinate)vSolidRoundRect.elementAt(i)).getX2()-((Coordinate)vSolidRoundRect.elementAt(i)).getX1(),((Coordinate)vSolidRoundRect.elementAt(i)).getY2()-((Coordinate)vSolidRoundRect.elementAt(i)).getY1(),25,25);
      	}
      	for (int i=0;i<vSquare.size();i++){
         	g.setColor(((Coordinate)vSquare.elementAt(i)).colour());
         	g.drawRect(((Coordinate)vSquare.elementAt(i)).getX1(),((Coordinate)vSquare.elementAt(i)).getY1(),((Coordinate)vSquare.elementAt(i)).getX2()-((Coordinate)vSquare.elementAt(i)).getX1(),((Coordinate)vSquare.elementAt(i)).getY2()-((Coordinate)vSquare.elementAt(i)).getY1());
      	}
      	for (int i=0;i<vSolidSquare.size();i++){
         	g.setColor(((Coordinate)vSolidSquare.elementAt(i)).colour());
         	g.fillRect(((Coordinate)vSolidSquare.elementAt(i)).getX1(),((Coordinate)vSolidSquare.elementAt(i)).getY1(),((Coordinate)vSolidSquare.elementAt(i)).getX2()-((Coordinate)vSolidSquare.elementAt(i)).getX1(),((Coordinate)vSolidSquare.elementAt(i)).getY2()-((Coordinate)vSolidSquare.elementAt(i)).getY1());
      	}
      	for(int i=0;i<vPolygon.size();i++){
      	 	int xPos[] = new int[((Coordinate)vPolygon.elementAt(i)).getXPolygon().size()];
      	 	int yPos[] = new int[((Coordinate)vPolygon.elementAt(i)).getYPolygon().size()];
      	 
      	 	for(int count=0;count<xPos.length;count++)
      	 	{
      	 		xPos[count] = ((Integer)((Coordinate)vPolygon.elementAt(i)).getXPolygon().elementAt(count)).intValue();
      	 		yPos[count] = ((Integer)((Coordinate)vPolygon.elementAt(i)).getYPolygon().elementAt(count)).intValue();
      	 	}     	 
      	 	g.setColor(((Coordinate)vPolygon.elementAt(i)).colour());
      	 	g.drawPolygon(xPos,yPos,xPos.length);
	  	}
	  	for(int i=0;i<vSolidPolygon.size();i++){
      	 	int xPos[] = new int[((Coordinate)vSolidPolygon.elementAt(i)).getXPolygon().size()];
      	 	int yPos[] = new int[((Coordinate)vSolidPolygon.elementAt(i)).getYPolygon().size()];
      	 
      	 	for(int count=0;count<xPos.length;count++)
      	 	{
      	 		xPos[count] = ((Integer)((Coordinate)vSolidPolygon.elementAt(i)).getXPolygon().elementAt(count)).intValue();
      	 		yPos[count] = ((Integer)((Coordinate)vSolidPolygon.elementAt(i)).getYPolygon().elementAt(count)).intValue();
      	 	}
      	 	g.setColor(((Coordinate)vSolidPolygon.elementAt(i)).colour());
      	 	g.fillPolygon(xPos,yPos,xPos.length);
      	}	
    }
    
}

⌨️ 快捷键说明

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