展开全部

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. Linux下动态库使用小结
  2. Promise.all 处理error
  3. 转行人工智能,不得不温故的数学基础知识
  4. Windows下学习Objective-C 2.0
  5. iso qemu 安装ubuntu_qemu 安装 ubuntu-server 虚拟机
  6. 浅谈一下session问题
  7. 新版本eclipse Neon 4.6.1,登录git报401 没有权限
  8. 先验概率、后验概率、贝叶斯公式的通俗解释
  9. OpenWrt官方入门手册
  10. Windows10 地平线4支持PS4手柄有线连接
  11. centos双网卡不能同时工作解决
  12. 计算机网络识别慢,win7系统开机后识别网络速度特别慢的解决方法
  13. 【Java每日面试题】大厂是如何设计秒杀系统的,渣本Java开发小伙如何一步步成为架构师
  14. ReactNative出现诸如以上的错误; ** is not defined.都是没有导入相应的库.
  15. 解决gcc -m32报错fatal error: sys/cdefs.h: No such file or directory
  16. 教程篇(5.4) 03. FortiManager 设备注册 ❀ Fortinet 网络安全专家 NSE5
  17. 最简解决方案--安装ubuntu 遇到32位 EFI(UEFI) /EFI/BOOT/bootia32.efi unavilable
  18. 用yolov5做人脸检测
  19. 瞧不起,与 “瞧不起”
  20. C++之命令(Command)模式

热门文章

  1. 【python】算法引入及算法特性和时间复杂度
  2. 多级cache之间的替换(缓存)策略
  3. [待]-optee的native_intr_handler中断处理流程
  4. [crypto]-53-openssl命令行的使用(aes/rsa签名校验/rsa加密解密/hmac)
  5. 广西中专机器人应用与维护_我校2018级工业机器人应用与维护专业跟岗实习
  6. 2020-10-25(个人int误区)
  7. 【技术分享】如何解锁高通骁龙660上的安卓引导加载程序
  8. 4.API的调用过程(系统服务表)
  9. Javascript基本概念之数据类型
  10. 5、CSS 外边距合并