Java笔记:绘制简易图形,鼠标监听器,窗口坐标系与标志位
概述
本文旨在记录Java代码初学者的学习历程并表达本人对代码的理解。
本文的内容包括:画笔的简单使用方法,鼠标监听器的使用方法,Java窗口的坐标系,标志位。
1.画笔的使用方法
我们会实现在一个窗口里画出简单图案,可以通过按钮选择画出的图案,并且可以把线条颜色改为红色的需求。首先定义窗体并设置其属性,这里不再具体说明。随后,我们需要创建画笔对象。需要注意,画笔要在哪里画,这个画笔就要从哪个组件取得。并且,画笔必须在设置窗体可见后。代码如下:
import java.awt.*; import javax.swing.*; public class DrawSoftWare { public void drawer(){ JFrame jframe=new JFrame(); jframe.setSize(1200,900); jframe.setTitle("Draw SoftWare"); jframe.setDefaultCloseOperation(3); jframe.setLocationRelativeTo(null); jframe.setVisible(true); Graphics g=jframe.getGraphics();//本句即是画笔的定义句 } public static void main(String[] args){ DrawSoftWare d=new DrawSoftWare(); d.drawer(); } }要实现画图效果,我们还需要鼠标监听器(MouseListener),它的数据类型同样是接口,我们可以参考前面的接口使用方法。
import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class DrawListener implements MouseListener { public void mouseClicked (MouseEvent e){ } public void mousePressed(MouseEvent e){ } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } }可见,鼠标监听器中有5个方法体。这里我们需要用到前三个。第一个指的是鼠标点击时触发(鼠标不移动),第二个是鼠标按下时触发,第三个是鼠标松开时触发。需注意,鼠标点击时也会触发一次按下和松开。由于这是两个不同的类,因此我们需要一个全局变量来引用传递上一个窗口的画笔。
public class DrawSoftWare { public void drawer(){ JFrame jframe=new JFrame(); jframe.setSize(600,600); jframe.setTitle("Draw SoftWare"); jframe.setDefaultCloseOperation(3); jframe.setLocationRelativeTo(null); jframe.setVisible(true); FlowLayout flowLayout=new FlowLayout(); jframe.setLayout(flowLayout); JButton jbutton=new JButton("直线"); JButton jbutton2=new JButton("矩形"); JButton jbutton3=new JButton("等腰三角形"); JButton jbutton4=new JButton("任意三角形"); JButton jbutton5=new JButton("多边形"); JButton Red =new JButton("红色"); jframe.add(jbutton); jframe.add(jbutton2); jframe.add(jbutton3); jframe.add(jbutton4); jframe.add(jbutton5); jframe.add(Red); Graphics g=jframe.getGraphics(); DrawListener drawListener=new DrawListener(); jframe.addMouseListener(drawListener); drawListener.gr=g; jbutton.addActionListener(drawListener); jbutton2.addActionListener(drawListener); jbutton3.addActionListener(drawListener); jbutton4.addActionListener(drawListener); jbutton5.addActionListener(drawListener); Red.addActionListener(drawListener); } public static void main(String[] args){ DrawSoftWare d=new DrawSoftWare(); d.drawer(); } }完成了监听器的设置,Graphics g的引用传递和按钮的设置后,类DrawSoftWare就完成了。
2.Java窗口的坐标系
绘制直线的代码长这样:
gr.drawLine(x1,y1,x2,y2)其中,x1和y1分别是起点的横坐标和纵坐标,x2和y2则是终点的。下面,我们介绍一下Java窗口的坐标。
Java的窗口,其坐标原点在窗口左上角,x从0开始从左往右递增,y从0开始从上往下递增,因此不存在负数坐标。
要绘制直线,我们需要提取鼠标按下和释放所在的坐标,然后在这两点之间连一条线即可。代码如下:
public void mousePressed(MouseEvent e) { x1=e.getX(); y1=e.getY(); } public void mouseReleased(MouseEvent e) { x2=e.getX(); y2=e.getY(); gr.drawLine(x1, y1, x2, y2); }x1即是起点的x坐标,y1即是起点的y坐标。在鼠标释放后完成直线的绘制。
3.标志位
标志位,简单地说就是控制代码是否执行的量。常用的类型有整型,布尔型和字符串。我们要让不同按钮对应不同的代码段落,因此需要标志位。
这里我们使用按钮上的文本作为判断依据。我们会设置一个全局变量Command来保存按钮上的文本,以它为标志位。同时,前面提到鼠标点击时也会触发按下和释放,这会导致x1,x2,x3变为相同量,y1,y2,y3也是这样,所以,我们会设置一个布尔型的全局变量flag来控制。分析逻辑后,最终事件处理类如下:
import java.awt.*; import java.awt.event.*; public class DrawListener implements MouseListener, ActionListener { public int x1,y1,x2,y2,x3,y3; public static Graphics gr ; public String Command; public String color; public boolean flag; public void actionPerformed(ActionEvent e) { color=e.getActionCommand(); if(!color.equals("红色")){ Command = e.getActionCommand(); } flag = true; System.out.println(Command); System.out.println(color); } public void mousePressed(MouseEvent e){ if(flag==true){ x1=e.getX(); y1=e.getY(); } } public void mouseReleased(MouseEvent e) { if(color.equals("红色")){ gr.setColor(Color.RED); } if (flag == true) { x2 = e.getX(); y2 = e.getY(); } if (Command.equals("直线")) { gr.drawLine(x1, y1, x2, y2);//直线 } if (Command.equals("矩形")) { int wid = Math.abs(x2 - x1); int hei = Math.abs(y2 - y1); int xMin = Math.min(x1, x2); int yMin = Math.min(y1, y2); gr.drawRect(xMin, yMin, wid, hei);//矩形 } if (Command.equals("等腰三角形")) { gr.drawLine(x1, y1, x2, y1); int xMid = Math.abs((x2 + x1) / 2); gr.drawLine(x1, y1, xMid, y2); gr.drawLine(x2, y1, xMid, y2); } if (Command.equals("任意三角形")) { gr.drawLine(x1, y1, x2, y2); flag = false; } if (Command.equals("多边形")&&flag==true) { gr.drawLine(x1, y1, x2, y2); flag = false; } } public void mouseClicked(MouseEvent e){ x3=e.getX(); y3=e.getY(); if(Command.equals("任意三角形")){ gr.drawLine(x1, y1, x3, y3); gr.drawLine(x2, y2, x3, y3); flag=true; } if(Command.equals("多边形")){ gr.drawLine(x2, y2, x3, y3); x2=x3; y2=y3; int times=e.getClickCount(); if (times==2){ gr.drawLine(x1, y1, x3, y3); flag=true; } } } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } }其中,!字符串1.equals(字符串2)表示字符串1不等于字符串2。
