展开全部

import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionListener; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; //import javax.swing.Component;publicclass ShapeMain extends JFrame implements ActionListener,MouseListener,MouseMotionListener{ int x,y,x1,y1,x2,y2,width,height; boolean isFirstPoint = true; //初始化开始画的是线int drawType = PaintingGround.LINE; //初始化开始不是填充boolean isFill = false; //添加控件 ButtonGroup btg = new ButtonGroup(); Button btLine = new Button("线"); Button btRectangle = new Button("矩形32313133353236313431303231363533e58685e5aeb931333332636435"); Button btRound = new Button("圆"); Button btEllipse = new Button("椭圆"); Button tbFillState = new Button("填充"); Button button3 = new Button("文本操作"); Button button2 = new Button("清除"); Button button1 = new Button("选择颜色"); Panel buttonPanel = new Panel(); PaintingGround paintingGround = new PaintingGround(); //Main Methodpublicstaticvoid main(String[] args) { //设置显示外观try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception e) { e.printStackTrace(); } new ShapeMain(); } //构造函数public ShapeMain() { //控件添加到控件组中 // btg.add(btLine); // btg.add(btRectangle); // btg.add(btRound); // btg.add(btEllipse); buttonPanel.add(btLine); buttonPanel.add(btRectangle); buttonPanel.add(btRound); buttonPanel.add(btEllipse); buttonPanel.add(tbFillState); //设置容器及容器的整体布局 Container cp = this; cp.setLayout(new BorderLayout()); cp.add(BorderLayout.NORTH,buttonPanel); cp.add(BorderLayout.CENTER,paintingGround); //cp.add(BorderLayout.SOUTH,jf); //jf.setJMenuBar(mb); setLocation(300,150); setSize(600,480); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); //添加鼠标触发事件 paintingGround.addMouseListener(new MouseAdapter() { publicvoid mouseReleased(MouseEvent evn) { isFirstPoint = true; } }); //对鼠标的输入进行判断并调用画图程序 paintingGround.addMouseMotionListener(new MouseMotionAdapter() { publicvoid mouseDragged(MouseEvent evn) { if(isFirstPoint) { x1 = evn.getX(); y1 = evn.getY(); isFirstPoint = false; } else { x2 = evn.getX(); y2 = evn.getY(); switch(drawType) { case PaintingGround.LINE: //画线paintingGround.drawLine(x1,y1,x2,y2); break; case PaintingGround.RECTANGLE: //画矫形 paintingGround.drawRect(x1,y1,x2-x1,y2-y1); break; case PaintingGround.ROUND: //画圆//两点距离公式int size = Math.abs((int)Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))); paintingGround.drawRound(x1,y1,size); break; case PaintingGround.ELLIPSE: //画椭圆 paintingGround.drawEllipse(x1,y1,x2-x1,y2-y1); break; default: break; } } } }); //各个控件的触发事件 btLine.addActionListener(new ActionListener(){ publicvoid actionPerformed(ActionEvent evn) { drawType = PaintingGround.LINE; } }); btRectangle.addActionListener(new ActionListener(){ publicvoid actionPerformed(ActionEvent evn) { drawType = PaintingGround.RECTANGLE; } }); btRound.addActionListener(new ActionListener(){ publicvoid actionPerformed(ActionEvent evn) { drawType = PaintingGround.ROUND; } }); btEllipse.addActionListener(new ActionListener(){ publicvoid actionPerformed(ActionEvent evn) { drawType = PaintingGround.ELLIPSE; } }); tbFillState.addActionListener(new ActionListener(){ publicvoid actionPerformed(ActionEvent evn) { isFill = tbFillState.isShowing(); paintingGround.setFillState(isFill); } }); } publicvoid actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } publicvoid mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } publicvoid mousePressed(MouseEvent e) { // TODO Auto-generated method stub } publicvoid mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } publicvoid mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } publicvoid mouseExited(MouseEvent e) { // TODO Auto-generated method stub } publicvoid mouseDragged(MouseEvent e) { // TODO Auto-generated method stub } publicvoid mouseMoved(MouseEvent e) { // TODO Auto-generated method stub } } class PaintingGround extends JPanel { publicstaticfinalint LINE = 1; publicstaticfinalint RECTANGLE = 2; publicstaticfinalint ROUND = 3; publicstaticfinalint ELLIPSE = 4; privateint x,y; privateint x1,y1,x2,y2; privateint width, height,size; privateint drawType = 0; privateboolean isFill = false; //构造函数public PaintingGround() { setBackground(Color.white); } //判断是用实心还是空心的,publicvoid paint(Graphics g) { //super.paintComponent(g); //super.paint(g);super.paint(g); g.setColor(Color.black); if(isFill) { switch(drawType) { case LINE: g.drawLine(x1,y1,x2,y2); break; case RECTANGLE: g.fillRect(x,y,width,height); break; case ROUND: g.fillOval(x,y,size,size); break; case ELLIPSE: g.fillOval(x,y,width,height); break; default: break; } } else { switch(drawType) { case LINE: g.drawLine(x1,y1,x2,y2); break; case RECTANGLE: g.drawRect(x,y,width,height); break; case ROUND: g.drawOval(x,y,size,size); break; case ELLIPSE: g.drawOval(x,y,width,height); break; default: break; } } } publicvoid drawLine(int x1, int y1, int x2,int y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; drawType = LINE; //jim isFill = false; //jimrepaint(); } //具体的实现方式publicvoid drawRect(int x,int y,int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; drawType = RECTANGLE; repaint(); } publicvoid drawRound(int x,int y,int size) { this.x = x; this.y = y; this.size = size; drawType = ROUND; repaint(); } publicvoid drawEllipse(int x,int y,int width,int height) { this.x = x; this.y = y; this.width = width; this.height = height; drawType = ELLIPSE; repaint(); } publicvoid setFillState(boolean isFill) { this.isFill = isFill; } }

本回答被提问者采纳

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

java 画图覆盖_请教如何在java画图中不覆盖原来的画图???相关推荐

  1. jsp获取java数组长度_数组 – 如何在java jsp中获取数组列表大小?

    我有一个表单要求用户输入ID.此表单发送到一个servlet,该servlet检查数据库以查看用户是否存在.如果用户存在,那么它会发回我们的订购项目.有序项目作为数组列表返回.然后这个数组列表将重定向 ...

  2. java jsonarray 追加_我们如何在Java中将JSONArray添加到JSONObject?

    该JSON是用于交换数据的基于文本的格式.它是轻量级的组件,与语言无关.我们还可以将JSONArray添加到JSONObject.我们需要首先将一些项目添加到ArrayList中,并将此列表传递给JS ...

  3. java 初始化参数_我们如何在Java中的对象参数中初始化数组?

    您可以使用构造函数或使用setter方法来初始化与其他任何值一样在类内部声明的数组变量. 示例 在下面的Java示例中,我们声明一个数组类型的实例变量,并从构造函数中对其进行初始化.public cl ...

  4. 从事java的年龄_请教前辈们:JAVA的职业有年龄限制吗

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 顺便把文章也贴在这里: 来C语言吧潜水很久,冒个泡,给初学者几个建议 1. 如果你不是计算机,电子,自动化等"计算机相关专业",除非你 ...

  5. java 服务器发布_我如何在java中发布到服务器?

    我想通过流向服务器发送一个字符串"hello world". 到目前为止我做了: private void PostData() { HttpURLConnection urlCo ...

  6. java 中覆 写tostring_如何在Java中正确覆盖toString()?

    如何在Java中正确覆盖toString()? 听起来有点愚蠢,但我需要帮助我的toString()方法,这是非常irking. 我尝试在网上查找,因为toString是搞砸了,"没有找到K ...

  7. 编写一个java程序_鼠标在java窗口上的坐标_请问如何在Java中获取窗口外部的鼠标单击坐标...

    尽管可能,但可能会受到限制: 为焦点事件添加一个AWTEventListener.只要您的应用在单击按钮之前就具有焦点,就会收到焦点丢失事件.然后查询指针位置. 限制是,当然,您的应用程序失去了焦点. ...

  8. java future用法_纯干货:Java学习过程中的21个知识点和技术点

    我们在Java学习过程中要学会抓重点,善于总结,Java学习过程中常见的21个知识点和技术点你知道吗?下面和千锋广州小编一起来看看吧! 1. JVM相关 对于刚刚接触Java的人来说,JVM相关的知识 ...

  9. java python算法_用Python,Java和C ++示例解释的排序算法

    java python算法 什么是排序算法? (What is a Sorting Algorithm?) Sorting algorithms are a set of instructions t ...

最新文章

  1. 半连接队列 全连接队列??
  2. SpendidCRM:在admin-layout中修改了视图上的字段后,用MyGeneration生成修改脚本
  3. mysql 进阶_mysql进阶知识
  4. python中网页关闭再打开_Python Selenium 之关闭窗口close与quit的方法
  5. 私有云办公平台大规模集群/企业级集群/小型工作室集群解决方案:NextCloud集群部署方案--NextCloud集群架构设计
  6. python中文转拼音不用第三方库_又一个奇葩要求,Python是如何将“中文”转“拼音”的?...
  7. es 使用ik停词_ES使用IK分词器
  8. 调用ICodeCompiler来计算字符串表达式
  9. 库伦法-锂电池容量计量
  10. 推荐5款免费网盘,空间大不限速!
  11. initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
  12. 入职填写的工作经历时间虚假,公司以此来辞退我怎么办?
  13. 【秋招】拼多多_数据分析岗_面试题整理
  14. 从未相识——只愿下一世安好
  15. sql语句实现查询实例
  16. Apollo 3.0来了!百度自动驾驶硬件系统全解读
  17. 通过AI,领略皮影戏艺术 | MixLab人工智能
  18. js从服务器获取word文档,javascript - 使用Office.js API将Word文档(.docx)保存到后端服务器 - 堆栈内存溢出...
  19. win7 喇叭图标不见了
  20. 基于EFR32的Zigbee开发-介绍

热门文章

  1. Linux Kernel中local_irq_enable()和local_irq_disable()的实现
  2. 用Go语言建立一个简单的区块链part5:地址
  3. linuxdeployqt打包
  4. 配置SQL Server数据库连接
  5. 16、Java Swing JProgressBar:进度条组件
  6. jQuery表格的行编辑
  7. Nginx的平滑重启和升级
  8. ajax请求挂起_Ajax请求内部循环挂起浏览器?
  9. python如何进入文件夹_python之文件的读写和文件目录以及文件夹的操作实现代码...
  10. 【数据结构-排序】3.图解选择排序两种实现(简单选择排序/堆排序)