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

📄 mywindowsplot.java

📁 java画图程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        }

        //使用擦皮
        public void drawBrush()
        {
        	contentPane.removeMouseListener(mlOneFillPolygon);
        	contentPane.removeMouseListener(mlOneFillOval);
  			contentPane.removeMouseListener(mlTwoFillOval);
  			contentPane.removeMouseMotionListener(mmlFillOval);
        	contentPane.removeMouseListener(mlOneFillRect);
        	contentPane.removeMouseListener(mlTwoFillRect);
        	contentPane.removeMouseMotionListener(mmlFillRect);
        	contentPane.removeMouseListener(mlOneDrawBrush);
			contentPane.removeMouseMotionListener(mmlDrawBrush);
        	contentPane.removeMouseListener(mlOneDrawRect);
			contentPane.removeMouseListener(mlTwoDrawRect);
			contentPane.removeMouseMotionListener(mmlDrawRect);
			contentPane.removeMouseListener(mlOneDrawOval);
			contentPane.removeMouseListener(mlTwoDrawOval);
			contentPane.removeMouseMotionListener(mmlDrawOval);
			contentPane.removeMouseListener(mlOneDrawPolygon);
			contentPane.removeMouseListener(mlOneDrawLine);
        	contentPane.removeMouseListener(mlTwoDrawLine);
        	contentPane.removeMouseListener(mlOneDrawPencil);
			contentPane.removeMouseMotionListener(mmlDrawPencil); 
        	
			mlOneDrawBrush = new MouseAdapter()
			{
				public void mousePressed(MouseEvent e)
				{
					mouseDrawBrushX[countDrawBrush]	= e.getX();
					mouseDrawBrushY[countDrawBrush]	= e.getY();
					repaint();
					countDrawBrush++;
				}
			};
			
			mmlDrawBrush = new MouseMotionAdapter()
			{
				public void mouseDragged(MouseEvent e)
				{
					mouseDrawBrushX[countDrawBrush]	= e.getX();
					mouseDrawBrushY[countDrawBrush]	= e.getY();
					repaint();
					countDrawBrush++;
				}
			};
			contentPane.addMouseListener(mlOneDrawBrush);
			contentPane.addMouseMotionListener(mmlDrawBrush);
        }

        //绘制直线
        public void drawLine()
        {
        	//删除监听器
        	contentPane.removeMouseListener(mlOneFillPolygon);
        	contentPane.removeMouseListener(mlOneFillOval);
  			contentPane.removeMouseListener(mlTwoFillOval);
  			contentPane.removeMouseMotionListener(mmlFillOval);
        	contentPane.removeMouseListener(mlOneFillRect);
        	contentPane.removeMouseListener(mlTwoFillRect);
        	contentPane.removeMouseMotionListener(mmlFillRect);
        	contentPane.removeMouseListener(mlOneDrawLine);
        	contentPane.removeMouseListener(mlTwoDrawLine);
        	contentPane.removeMouseListener(mlOneDrawRect);
			contentPane.removeMouseListener(mlTwoDrawRect);
			contentPane.removeMouseMotionListener(mmlDrawRect);
			contentPane.removeMouseListener(mlOneDrawOval);
			contentPane.removeMouseListener(mlTwoDrawOval);
			contentPane.removeMouseMotionListener(mmlDrawOval);
			contentPane.removeMouseListener(mlOneDrawPolygon);
			contentPane.removeMouseListener(mlOneDrawPencil);
			contentPane.removeMouseMotionListener(mmlDrawPencil);
			contentPane.removeMouseListener(mlOneDrawBrush);
			contentPane.removeMouseMotionListener(mmlDrawBrush); 

        	mlOneDrawLine = new MouseAdapter()
        	{
        		public void mousePressed(MouseEvent e)
	        	{
	        		mousePressedDrawLineX[countDrawLine] = e.getX();
	        		mousePressedDrawLineY[countDrawLine] = e.getY();
	        	}
        	};

        	mlTwoDrawLine = new MouseAdapter()
        	{
        		public void mouseReleased(MouseEvent e)
	        	{
	        		mouseReleasedDrawLineX[countDrawLine] = e.getX();
	        		mouseReleasedDrawLineY[countDrawLine] = e.getY();
	        		Graphics g = contentPane.getGraphics();
	        		g.setColor(pColor);
	        		drawLineColor[countDrawLine] = g.getColor();
	        		g.drawLine(mousePressedDrawLineX[countDrawLine], mousePressedDrawLineY[countDrawLine],
	        			mouseReleasedDrawLineX[countDrawLine], mouseReleasedDrawLineY[countDrawLine]);
		    		countDrawLine++;
	        	}
        	};

        	contentPane.addMouseListener(mlOneDrawLine);
        	contentPane.addMouseListener(mlTwoDrawLine);           	        		        
        }

        //开始绘圆
        public void drawOval()
        {
        	contentPane.removeMouseListener(mlOneFillPolygon);
        	contentPane.removeMouseListener(mlOneFillOval);
  			contentPane.removeMouseListener(mlTwoFillOval);
  			contentPane.removeMouseMotionListener(mmlFillOval);
        	contentPane.removeMouseListener(mlOneFillRect);
        	contentPane.removeMouseListener(mlTwoFillRect);
        	contentPane.removeMouseMotionListener(mmlFillRect);
        	contentPane.removeMouseListener(mlOneDrawOval);
			contentPane.removeMouseListener(mlTwoDrawOval);
			contentPane.removeMouseMotionListener(mmlDrawOval);
        	contentPane.removeMouseListener(mlOneDrawLine);
        	contentPane.removeMouseListener(mlTwoDrawLine);
        	contentPane.removeMouseListener(mlOneDrawRect);
			contentPane.removeMouseListener(mlTwoDrawRect);
			contentPane.removeMouseMotionListener(mmlDrawRect);
			contentPane.removeMouseListener(mlOneDrawPolygon);
			contentPane.removeMouseListener(mlOneDrawPencil);
			contentPane.removeMouseMotionListener(mmlDrawPencil);
			contentPane.removeMouseListener(mlOneDrawBrush);
			contentPane.removeMouseMotionListener(mmlDrawBrush); 

        	mlOneDrawOval = new MouseAdapter()
        	{
        		public void mousePressed(MouseEvent e)
	        	{
	        		mousePressedDrawOvalX[countDrawOval] = e.getX();
	        		mousePressedDrawOvalY[countDrawOval] = e.getY();
	        	}
        	};

        	mmlDrawOval = new MouseMotionAdapter()
        	{
        		public void mouseDragged(MouseEvent e)
	        	{
	        		mouseDraggedDrawOvalX = e.getX();
	        		mouseDraggedDrawOvalY = e.getY();
	        	}
        	};

  			mlTwoDrawOval = new MouseAdapter()
  			{
  				public void mouseReleased(MouseEvent e)
	        	{
	        		Graphics g = contentPane.getGraphics();
        			g.setColor(pColor);
        			drawOvalColor[countDrawOval] = g.getColor();
	        		mouseReleasedDrawOvalX[countDrawOval] = e.getX();
	        		mouseReleasedDrawOvalY[countDrawOval] = e.getY();

	        		if (mouseDraggedDrawOvalX > mousePressedDrawOvalX[countDrawOval] && mouseDraggedDrawOvalY > mousePressedDrawOvalY[countDrawOval])
	        		{
	        			g.drawOval(mousePressedDrawOvalX[countDrawOval], mousePressedDrawOvalY[countDrawOval],
	        				mouseDraggedDrawOvalX - mousePressedDrawOvalX[countDrawOval],
	        					mouseDraggedDrawOvalY - mousePressedDrawOvalY[countDrawOval]);
	        			//此处重绘是为了消除圆切线处的BUG, 以下方法一样
	        			g.drawOval(mousePressedDrawOvalX[countDrawOval], mousePressedDrawOvalY[countDrawOval],
	        				mouseDraggedDrawOvalX - mousePressedDrawOvalX[countDrawOval],
	        					mouseDraggedDrawOvalY - mousePressedDrawOvalY[countDrawOval]);
	        		}

	        		if (mouseDraggedDrawOvalX < mousePressedDrawOvalX[countDrawOval] && mouseDraggedDrawOvalY < mousePressedDrawOvalY[countDrawOval])
	        		{
	        			g.drawOval(mouseReleasedDrawOvalX[countDrawOval], mouseReleasedDrawOvalY[countDrawOval],
	        				mousePressedDrawOvalX[countDrawOval] - mouseReleasedDrawOvalX[countDrawOval],
	        					mousePressedDrawOvalY[countDrawOval] - mouseReleasedDrawOvalY[countDrawOval]);
	        			g.drawOval(mouseReleasedDrawOvalX[countDrawOval], mouseReleasedDrawOvalY[countDrawOval],
	        				mousePressedDrawOvalX[countDrawOval] - mouseReleasedDrawOvalX[countDrawOval],
	        					mousePressedDrawOvalY[countDrawOval] - mouseReleasedDrawOvalY[countDrawOval]);
	        		}

	        		if (mouseDraggedDrawOvalX < mousePressedDrawOvalX[countDrawOval] && mouseDraggedDrawOvalY > mousePressedDrawOvalY[countDrawOval])
	        		{
	        			g.drawOval(mouseReleasedDrawOvalX[countDrawOval], mousePressedDrawOvalY[countDrawOval],
	        				mousePressedDrawOvalX[countDrawOval] - mouseReleasedDrawOvalX[countDrawOval],
	        					mouseReleasedDrawOvalY[countDrawOval] - mousePressedDrawOvalY[countDrawOval]);
	        			g.drawOval(mouseReleasedDrawOvalX[countDrawOval], mousePressedDrawOvalY[countDrawOval],
	        				mousePressedDrawOvalX[countDrawOval] - mouseReleasedDrawOvalX[countDrawOval],
	        					mouseReleasedDrawOvalY[countDrawOval] - mousePressedDrawOvalY[countDrawOval]);
	        		}

	        		if (mouseDraggedDrawOvalX > mousePressedDrawOvalX[countDrawOval] && mouseDraggedDrawOvalY < mousePressedDrawOvalY[countDrawOval])
	        		{
	        			g.drawOval(mousePressedDrawOvalX[countDrawOval], mouseReleasedDrawOvalY[countDrawOval],
	        				mouseReleasedDrawOvalX[countDrawOval] - mousePressedDrawOvalX[countDrawOval],
	        					mousePressedDrawOvalY[countDrawOval] - mouseReleasedDrawOvalY[countDrawOval]);
	        			g.drawOval(mousePressedDrawOvalX[countDrawOval], mouseReleasedDrawOvalY[countDrawOval],
	        				mouseReleasedDrawOvalX[countDrawOval] - mousePressedDrawOvalX[countDrawOval],
	        					mousePressedDrawOvalY[countDrawOval] - mouseReleasedDrawOvalY[countDrawOval]);
	        		}
	        		countDrawOval++;
	        	}
  			};
  			
  			contentPane.addMouseListener(mlOneDrawOval);
  			contentPane.addMouseListener(mlTwoDrawOval);
  			contentPane.addMouseMotionListener(mmlDrawOval);
        }

        //开始绘多边形
        public void drawPolygon()
        {
        	contentPane.removeMouseListener(mlOneFillPolygon);
        	contentPane.removeMouseListener(mlOneFillOval);
  			contentPane.removeMouseListener(mlTwoFillOval);
  			contentPane.removeMouseMotionListener(mmlFillOval);
        	contentPane.removeMouseListener(mlOneFillRect);
        	contentPane.removeMouseListener(mlTwoFillRect);
        	contentPane.removeMouseMotionListener(mmlFillRect);
        	contentPane.removeMouseListener(mlOneDrawPolygon);
        	contentPane.removeMouseListener(mlOneDrawLine);
        	contentPane.removeMouseListener(mlTwoDrawLine);
        	contentPane.removeMouseListener(mlOneDrawOval);
			contentPane.removeMouseListener(mlTwoDrawOval);
			contentPane.removeMouseMotionListener(mmlDrawOval);
			contentPane.removeMouseListener(mlOneDrawRect);
        	contentPane.removeMouseListener(mlTwoDrawRect);
        	contentPane.removeMouseListener(mlOneDrawPencil);
			contentPane.removeMouseMotionListener(mmlDrawPencil);
			contentPane.removeMouseListener(mlOneDrawBrush);
			contentPane.removeMouseMotionListener(mmlDrawBrush); 
        	
        	mlOneDrawPolygon = new MouseAdapter()
        	{
        		public void mousePressed(MouseEvent e)
	        	{
	        		mousePressedDrawPolygonX[countDrawPolygon] = e.getX();
	        		mousePressedDrawPolygonY[countDrawPolygon] = e.getY(); 	        		
	        		Graphics g = contentPane.getGraphics();
        			g.setColor(pColor);
        			
	        		if (countDrawPolygon > 0)
		        	{        	        		
		        		g.drawLine(mousePressedDrawPolygonX[countDrawPolygon - 1], mousePressedDrawPolygonY[countDrawPolygon - 1], mousePressedDrawPolygonX[countDrawPolygon], mousePressedDrawPolygonY[countDrawPolygon]);        		
		        	}
		        	countDrawPolygon++;    		        		        		
	        	}	        	
        	};     	
        	contentPane.addMouseListener(mlOneDrawPolygon);
        }

        //开始绘矩形
        public void drawRect()
        {
        	contentPane.removeMouseListener(mlOneFillPolygon);
        	contentPane.removeMouseListener(mlOneFillOval);
  			contentPane.removeMouseListener(mlTwoFillOval);
  			contentPane.removeMouseMotionListener(mmlFillOval);
        	contentPane.removeMouseListener(mlOneFillRect);
        	contentPane.removeMouseListener(mlTwoFillRect);
        	contentPane.removeMouseMotionListener(mmlFillRect);
        	contentPane.removeMouseListener(mlOneDrawRect);
        	contentPane.removeMouseListener(mlTwoDrawRect);
        	contentPane.removeMouseMotionListener(mmlDrawRect);
        	contentPane.removeMouseListener(mlOneDrawLine);
        	contentPane.removeMouseListener(mlTwoDrawLine);
        	contentPane.removeMouseListener(mlOneDrawOval);
			contentPane.removeMouseListener(mlTwoDrawOval);
			contentPane.removeMouseMotionListener(mmlDrawOval);
			contentPane.removeMouseListener(mlOneDrawPolygon);
			contentPane.removeMouseListener(mlOneDrawPencil);
			contentPane.removeMouseMotionListener(mmlDrawPencil);
			contentPane.removeMouseListener(mlOneDrawBrush);
			contentPane.removeMouseMotionListener(mmlDrawBrush); 

        	mlOneDrawRect = new MouseAdapter()
        	{
        		public void mousePressed(MouseEvent e)
	        	{
	        		mousePressedDrawRectX[countDrawRect] = e.getX();
	        		mousePressedDrawRectY[countDrawRect] = e.getY();
	        	}
        	};

        	mmlDrawRect = new MouseMotionAdapter()
        	{
        		public void mouseDragged(MouseEvent e)
	        	{
	        		mouseDraggedDrawRectX = e.getX();
	        		mouseDraggedDrawRectY = e.getY();
	        	}
        	};

        	mlTwoDrawRect = new MouseAdapter()
        	{
        		public void mouseReleased(MouseEvent e)
	        	{
	        		mouseReleasedDrawRectX[countDrawRect] = e.getX();
	        		mouseReleasedDrawRectY[countDrawRect] = e.getY();
	        		Graphics g = contentPane.getGraphics();
	        		g.setColor(pColor);
	        		drawRectColor[countDrawRect] = g.getColor();

		    		if (mouseDraggedDrawRectX > mousePressedDrawRectX[countDrawRect] && mouseDraggedDrawRectY > mousePressedDrawRectY[countDrawRect])
	        		{
	        			g.drawRect(mousePressedDrawRectX[countDrawRect], mousePressedDrawRectY[countDrawRect],
	        				mouseReleasedDrawRectX[countDrawRect] - mousePressedDrawRectX[countDrawRect],
	        					mouseReleasedDrawRectY[countDrawRect] - mousePressedDrawRectY[countDrawRect]);
	        		}

	        		if (mouseDraggedDrawRectX < mousePressedDrawRectX[countDrawRect] && mouseDraggedDrawRectY < mousePressedDrawRectY[countDrawRect])
					{
	        			g.drawRect(mouseReleasedDrawRectX[countDrawRect], mouseReleasedDrawRectY[countDrawRect],
	        				mousePressedDrawRectX[countDrawRect]-mouseReleasedDrawRectX[countDrawRect],
	        					mousePressedDrawRectY[countDrawRect]-mouseReleasedDrawRectY[countDrawRect]);
	        		}

	        		if (mouseDraggedDrawRectX < mousePressedDrawRectX[countDrawRect] && mouseDraggedDrawRectY > mousePressedDrawRectY[countDrawRect])
	        		{
	        			g.drawRect(mouseReleasedDrawRectX[countDrawRect], mousePressedDrawRectY[countDrawRect],
	        				mousePressedDrawRectX[countDrawRect] - mouseReleasedDrawRectX[countDrawRect],
	        					mouseReleasedDrawRectY[countDrawRect] - mousePressedDrawRectY[countDrawRect]);
	        		}

	        		if (mouseDraggedDrawRectX > m

⌨️ 快捷键说明

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