问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

请教如何在java画图中不覆盖原来的画图???

发布网友 发布时间:2023-10-26 16:30

我来回答

1个回答

热心网友 时间:2024-11-25 09:56

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("矩形"); 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; } }
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
企业培训学到了什么 培训感悟简短 有关培训的感悟 通过培训学到什么 培训你学到了什么 领导问培训学到什么怎么回复 Linux系统安装FTP服务器 Linux系统的网络文件共享 建筑的七盏明灯的内容简介 面向对象设计七大原则 简单说 交互设计七大定律 ...游戏我吼他以后他不理我了。几天没有理我是什么意思啊啊?_百度... 物理3-3中,温度改变,考虑大气压强改变吗2 个人买卖工商局怎么处罚 一个手机号怎么申请第二个 经常脱发,失眠,吃什么能调理2 如何从本溪站前到本溪水洞?2 怀小宝宝了,不知道该不该要。我还没做好生孩子的准备呢,怎么办... 婆婆侮辱我、做了许多欺负我的事情,可是还老往我家跑,我该怎么...16 到杭州大厦(环城北路)要怎么坐公交车 威纶触摸屏离线模拟功能键总出现请确认操作是怎乡回事?怎样解决...2 环球是干什么用的 环球有什么用途 威纶触摸屏离线模拟时提示重新编译工程是什么意思 内蒙古师范大学音乐专业考研要求6 2022内蒙古考研报考指南-专科生可以考研吗 可以考985、... 消失模浇注过程应注意的问题有哪些?10 高中物理3-3的问题1 小学六年级上册数学应用题如何分辨乘除法?有窍门没有?急快!!! 本溪火车站到本溪水洞怎么走?怎么样到本溪水洞? 本溪火车站怎么去本溪水洞7 ...建设用地标准》,居住用地占城市建设用地的比例是( )。 我的手机是波导X1,支持JAVA,但是是个横屏的,向大家请教... 戒酒几年了现在自酒闻到问都难受可是偶尔想喝一点红酒或啤酒还能喝吗... 戒酒十二天了晚上喝酒可以吗 初入原油市场怎么操作才能盈利? 失眠多梦脱发怎么办38 一个怎么会同时出现两个不同的头像,请问是怎么设置的? 一个怎么会同时出现两个不同的头像,请问是怎么设置的? 一个怎么会同时出现两个不同的头像,请问是怎么设置的? 六宫格做不出来,有人会解吗?说是4岁孩子的难度,动员了全家都... 一个怎么换两张头像? 现在大四马上要毕业了,毕业设计做不出来怎么办,老师变态什么不...3 我用霍尔传感器测小马达的速度,显示出来的速度变化波动较大,请问如何... 金蝶凭证纸在那买比较好?4 爱的理想生活演员表剧情介绍  盆地盖层火山岩岩浆系列及岩浆成因 难道我不要你不读书吗改为陈述句 请问刘易思的二元经济理论是什么?谢谢 难道你不在这所学校读书吗 改成陈述句 时序图里面T0的t1是不是5s,德塔t又是多少呢?求大家指教... 为什么孟买降水量较多的是从6月至9月?