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

📄 drawshapeframe.java

📁 自己写的一个Java画图板程序 支持鼠标拖动图形 选中图形 改变颜色等 推荐使用Eclipse来运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			shape.setIsSelected(false);

		}
		
		drawShapePanel.mouseEndPoint.x = e.getX();
		drawShapePanel.mouseEndPoint.y = e.getY();
		
		
		
		
		if(drawShapePanel.isDrawLineOn)
		{
			DrawShapeCommand drawShapeCommand = new DrawShapeCommand(drawShapePanel);
			drawShapeCommand.setDrawShapeFrame(this);
			
			
			
			
			
			
			
			
			
			MyLine myLine = new MyLine(drawShapePanel.mouseOriginalPoint.x, drawShapePanel.mouseOriginalPoint.y, drawShapePanel.mouseEndPoint.x, drawShapePanel.mouseEndPoint.y);
			
			myLine.setBorderColor(drawShapePanel.currentColor);
			myLine.setFillColor(drawShapePanel.currentColor);
			
			drawShapeCommand.setShape(myLine);
			
			
			drawShapeCommand.execute();
			
		}
		
		
		else if(drawShapePanel.isDrawRectangleOn)
		{
			DrawShapeCommand drawShapeCommand = new DrawShapeCommand(drawShapePanel);
			drawShapeCommand.setDrawShapeFrame(this);
			
			
			
			
			
			SuitableStartPoint suitableStartPoint =  new SuitableStartPoint();
			Point p = suitableStartPoint.getSuitableStartPoint(drawShapePanel.mouseOriginalPoint, drawShapePanel.mouseEndPoint);
			
			
			int width = drawShapePanel.mouseOriginalPoint.x - drawShapePanel.mouseEndPoint.x;
			int widthAbs = Math.abs(width);
			
			int height = drawShapePanel.mouseOriginalPoint.y - drawShapePanel.mouseEndPoint.y;
			int heightAbs = Math.abs(height);
			
			
			
			
			MyRectangle myRectangle = new MyRectangle(p.x, p.y, p.x + widthAbs, p.y + heightAbs);
			
			myRectangle.setBorderColor(drawShapePanel.currentColor);
			myRectangle.setFillColor(drawShapePanel.currentColor);
			
			drawShapeCommand.setShape(myRectangle);
			
			
			drawShapeCommand.execute();
			
			
		}
		
		
		
		else if(drawShapePanel.isDrawOvalOn)
		{
			DrawShapeCommand drawShapeCommand = new DrawShapeCommand(drawShapePanel);
			drawShapeCommand.setDrawShapeFrame(this);
			
			
			
			
			
			SuitableStartPoint suitableStartPoint =  new SuitableStartPoint();
			Point p = suitableStartPoint.getSuitableStartPoint(drawShapePanel.mouseOriginalPoint, drawShapePanel.mouseEndPoint);
			
			
			int width = drawShapePanel.mouseOriginalPoint.x - drawShapePanel.mouseEndPoint.x;
			int widthAbs = Math.abs(width);
			
			int height = drawShapePanel.mouseOriginalPoint.y - drawShapePanel.mouseEndPoint.y;
			int heightAbs = Math.abs(height);
			
			
			
			
			MyOval myOval = new MyOval(p.x, p.y, p.x + widthAbs, p.y + heightAbs);
			
			myOval.setBorderColor(drawShapePanel.currentColor);
			myOval.setFillColor(drawShapePanel.currentColor);
			
			drawShapeCommand.setShape(myOval);
			
			
			drawShapeCommand.execute();
			
			
		}
		
		
			
		
		
		repaint();
		
		
	}

	//鼠标拖动时,记录鼠标终点mouseEndPoint
	
	//如果图形被选中且处于isMoving状态,则调用图形的move方法
	
	public void mouseDragged(MouseEvent e) {
		
		drawShapePanel.mouseEndPoint.x = e.getX();
		drawShapePanel.mouseEndPoint.y = e.getY();
		
	
		for (Shape shape : drawShapePanel.shapeArrayList) {

			if (shape.getIsSelected() == true && drawShapePanel.isMoving) {
				shape.move(drawShapePanel.mouseOriginalPoint, drawShapePanel.mouseEndPoint);

				
			}
		
		}
		
		repaint();
		
	}

	public void mouseMoved(MouseEvent arg0) {

	}

	
	
	//处理按钮响应
	public void actionPerformed(ActionEvent e) {
	
		//保存文件
		if (e.getSource() == saveFile) {
			JFileChooser fileChooser = new JFileChooser();

			fileChooser.setAcceptAllFileFilterUsed(false);

			fileChooser.setFileFilter(new FileFilter() {

				public boolean accept(File file) {
					// TODO Auto-generated method stub

					if ((file.getName()).endsWith(".sh")) {
						return true;
					}

					else {
						return false;
					}

				}

				public String getDescription() {
					// TODO Auto-generated method stub
					return ".sh";
				}

			});

			int returnValue = fileChooser.showSaveDialog(new JFrame());

			if (returnValue == JFileChooser.APPROVE_OPTION) {
				String destinationFile = (fileChooser.getSelectedFile())
						.getPath();

				OutputShape outputShape = new OutputShape();

				outputShape.output(destinationFile,
						drawShapePanel.shapeArrayList);

			}
		}
		
		
		//读取文件
		else if (e.getSource() == loadFile) {
			JFileChooser fileChooser = new JFileChooser();

			fileChooser.setAcceptAllFileFilterUsed(false);

			fileChooser.setFileFilter(new FileFilter() {

				public boolean accept(File file) {
					// TODO Auto-generated method stub

					if ((file.getName()).endsWith(".sh")) {
						return true;
					}

					else {
						return false;
					}

				}

				public String getDescription() {
					// TODO Auto-generated method stub
					return ".sh";
				}

			});

			int returnValue = fileChooser.showOpenDialog(new JFrame());

			if (returnValue == JFileChooser.APPROVE_OPTION) {
				String sourceFile = (fileChooser.getSelectedFile()).getPath();

				InputShape inputShape = new InputShape();

				(drawShapePanel.shapeArrayList).clear();

				inputShape.input(sourceFile, drawShapePanel.shapeArrayList);

				drawShapePanel.repaint();

			}
			
			
		}
		
		//打开选择工具
		else if(e.getSource() == selectToolButton)
		{
			drawShapePanel.changeMode(0);
			
		
			
			
		}
		
		//进行组合
		else if(e.getSource() == compositeButton)
		{
			
			drawShapePanel.compositeShape();
			repaint();
			
		}
		
		//进行分离
		else if(e.getSource() == apartButton)
		{
			drawShapePanel.apartShape();
			repaint();
			
		}
		
		//进入画直线模式
		else if(e.getSource() == drawLineButton)
		{
			
			drawShapePanel.changeMode(1);
			
			
			
			
		}
		
		//进入画矩形模式
		else if(e.getSource() == drawRectangleButton)
		{
			drawShapePanel.changeMode(2);
			
			
			
			
		}
		
		//进入画椭圆模式
		else if(e.getSource() == drawOvalButton)
		{
			drawShapePanel.changeMode(3);
			
			
			
		}
		
		//进入选择边框颜色模式
		else if(e.getSource() == borderColorButton)
		{
			drawShapePanel.changeMode(4);
			
			
			
			
		}
		
		//进入选择填充颜色模式
		else if(e.getSource() == fillColorButton)
		{
			drawShapePanel.changeMode(5);
			
			
			
		}
		
		
		
		
		//选择当前绘制颜色
		else if(e.getSource() == colorButton)
		{
			
			Color colorChooser = JColorChooser.showDialog(this, "Color", Color.BLUE);
			
			if(colorChooser != null)
			{
				drawShapePanel.currentColor = colorChooser;
					
				colorButton.setBackground(drawShapePanel.currentColor);
				
				
			}
			
			
			
			
		}
		
		
		
		
		//进行undo操作
		else if(e.getSource() == undoButton || e.getSource() == undo)
		{
			DrawShapeCommand drawRectangleCommand = new DrawShapeCommand(drawShapePanel);
			drawRectangleCommand.setDrawShapeFrame(this);
			drawRectangleCommand.undo();
		}
		
		//进行redo操作
		else if(e.getSource() == redoButton || e.getSource() == redo)
		{
			DrawShapeCommand drawRectangleCommand = new DrawShapeCommand(drawShapePanel);
			drawRectangleCommand.setDrawShapeFrame(this);
			drawRectangleCommand.redo();
		}

	}

}

⌨️ 快捷键说明

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