java画图图形代码_drawshapeframe.java 源代码在线查看 - 自己写的一个Java画图板程序 支持鼠标拖动图形 选中图形 改变颜色等 推荐使用Eclipse来运行 资源下载 虫虫电...

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 stubif ((file.getName()).endsWith(".sh")) {return true;}else {return false;}}public String getDescription() {// TODO Auto-generated method stubreturn ".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 stubif ((file.getName()).endsWith(".sh")) {return true;}else {return false;}}public String getDescription() {// TODO Auto-generated method stubreturn ".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();}}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值