画图(点,线,面)
来源:互联网 发布:淘宝客服务要订吗 编辑:程序博客网 时间:2024/06/11 19:14
写一个画图板,在上面画线,点,面
使用工具:eclipse
涉及内容:界面,接口事件
想要在画图板上画线,点,面,我们需要识别鼠标在画图板上的操作
定义DrawListener事件处理类,该类实现MouseListener鼠标事件接口
MouseListener鼠标事件接口其中含有处理对应鼠标动作的方法:
处理鼠标按下:mousePressed
处理鼠标松开:mouseReleased
处理鼠标点击:mouseClicked
处理鼠标进入:mouseEntered
处理鼠标离开:mouseExited
DrawListener继承MouseListener,,虽然只需要要用到按下和松开的方法,但是却必须重写MouseListener的全部抽象方法
以按下为例,按下点的方法中的参数MouseEvent e记录了鼠标操作的信息,包括操作时鼠标的坐标
画一条线,需要得到线的两边点的坐标,然后调用画图板画笔的drawLine方法
//x1,y1为按下点坐标int x1,y1;//x2,y2为释放点坐标int x2,y2;public void mousePressed(MouseEvent e){ //e.getX()和e.getY()返回坐标 int x1=e.getX(); int y1=e.getY();}public void mouseReleased(MouseEvent e){ int x2=e.getX(); int y2=e.getY();}//drawLine参数是两个点的坐标g.drawline(x1,y1,x2,y2);
这样画好一条线
想要画一个点则只需要那个点的坐标,并使用drawLine,传入的两个参数就可以,在两个相同位置之间画一条线,就是画一个点
画图形比如圆,矩形,则要调用画图板画笔的其他方法
圆:drawOval(x, y, width, height),此圆为以(x,y)为左上角顶点,以width为宽,以height为高的 矩形的内切圆
矩形:drawRect(x, y, width, height),以(x,y)为左上角顶点,以width为宽,以height为高的 矩形
附上代码,这个代码可以选择需要画的类型,另外,没有内置函数可以画任意多边形,需要自己写,里面有写好的任意多边形代码,需要用到鼠标点击mouseClicked,效果和电脑自带的画图板效果一样,逻辑不复杂,可以自己尝试写写,其中接口ActionListener请见《Java接口事件》
import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Graphics;import javax.swing.JButton;import javax.swing.JFrame;public class DrawFrame extends JFrame { public static void main(String[] args) { // TODO Auto-generated method stub DrawFrame dr=new DrawFrame(); dr.InitUI(); } public void InitUI() { this.setTitle("Draw"); this.setSize(700, 500); this.setDefaultCloseOperation(3); this.setLocationRelativeTo(null); FlowLayout f1=new FlowLayout(FlowLayout.LEFT); this.setLayout(f1); JButton zhixian=new JButton("直线"); zhixian.setPreferredSize(new Dimension(100,30)); this.add(zhixian); JButton juxing=new JButton("矩形"); juxing.setPreferredSize(new Dimension(100,30)); this.add(juxing); JButton yuan=new JButton("圆"); yuan.setPreferredSize(new Dimension(100,30)); this.add(yuan); JButton renyi=new JButton("任意多边形"); renyi.setPreferredSize(new Dimension(100,30)); this.add(renyi); this.setVisible(true); DrawListener d=new DrawListener(); ActListener ac=new ActListener(); ac.getDraw(d); zhixian.addActionListener(ac); juxing.addActionListener(ac); yuan.addActionListener(ac); renyi.addActionListener(ac); Graphics g=this.getGraphics(); d.getG(g); this.addMouseListener(d); }}import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class ActListener implements ActionListener { public DrawListener dr; public void getDraw(DrawListener d) { dr=d; } public void actionPerformed(ActionEvent e) { if(e.getActionCommand()=="直线") { dr.getnum(1); } if(e.getActionCommand()=="矩形") { dr.getnum(2); } if(e.getActionCommand()=="圆") { dr.getnum(3); } if(e.getActionCommand()=="任意多边形") { dr.getnum(4); } }}import java.awt.Graphics;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;public class DrawListener implements MouseListener{ private int x0,y0,x1,y1,x2,y2,x3,y3,x4,y4; private int count=0; private int num=1; private Graphics g; public void getnum(int a) { num=a; } public void getG(Graphics gr) { g=gr; } public void mouseClicked(MouseEvent e) { if(num==4) { if(count>=3) { if(x3==x1&&y3==y1&&x4==x2&&y4==y2) { g.drawLine(x0, y0, x2, y2); count=0; } } } } public void mousePressed(MouseEvent e) { x1=e.getX(); y1=e.getY(); if(num==4) { count++; x4=x2; y4=y2; if(count==1) { x0=x1; y0=y1; } else if(count==3) { g.drawLine(x1, y1, x2, y2); } else { g.drawLine(x1, y1, x3, y3); } } } public void mouseReleased(MouseEvent e) { x2=e.getX(); y2=e.getY(); if(num==1) { g.drawLine(x1, y1, x2, y2); } if(num==2) { g.drawRect(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2)); } if(num==3) { g.drawOval(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2)); } if(num==4) { count++; x3=x1; y3=y1; if(count==2) { g.drawLine(x1, y1, x2, y2); } } } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){}}
效果
- 画图(点,线,面)
- 创建shape文件(点、线、面)
- 点、 线、 面
- C# 点 线 面
- 绘制点、线、面
- 【Python】Matplotlib画图(七)——线的颜色、点的形状
- 【Python】Matplotlib画图(七)——线的颜色、点的形状
- matlab 描点画图
- python 读点画图
- gnuplot下画图以及设置点、线基础
- 入门:画点、线、面
- AE绘制点、线、面
- OpenGL-绘制点、线、面
- JavaScript趣题:点、线、面
- matlab描点连线画图
- Dlib提取人脸特征点(68点,opencv画图)
- Dlib提取人脸特征点(68点,opencv画图)
- ArcMap获取要素(点、线、面)外包络面(含凹面)的一个方法
- ubuntu安装gcc
- 隐式转换与隐式参数2
- Android NDK开发扫盲及最新CMake的编译使用
- Zookeeper简介和部署
- 35.Linux-分析并制作环形缓冲区
- 画图(点,线,面)
- JDBC之数据库连接(proxool)
- JAVA之 for和foreach循环
- [BZOJ3669]-[Noi2014]魔法森林-LCT+并查集
- maven阿里巴巴镜像仓库配置
- 【Linux】rpm软件包管理
- HttpServletRequest
- 常见CTF源码泄露途径
- 13.将字符串中的小写字母转换成大写字母