资讯动态

Java笔记:绘制简易图形,鼠标监听器,窗口坐标系与标志位

发布时间:2026/8/8 8:58:58 来源:尧图企业网站定制
概述本文旨在记录Java代码初学者的学习历程并表达本人对代码的理解。本文的内容包括画笔的简单使用方法鼠标监听器的使用方法Java窗口的坐标系标志位。1.画笔的使用方法我们会实现在一个窗口里画出简单图案可以通过按钮选择画出的图案并且可以把线条颜色改为红色的需求。首先定义窗体并设置其属性这里不再具体说明。随后我们需要创建画笔对象。需要注意画笔要在哪里画这个画笔就要从哪个组件取得。并且画笔必须在设置窗体可见后。代码如下import java.awt.*; import javax.swing.*; public class DrawSoftWare { public void drawer(){ JFrame jframenew JFrame(); jframe.setSize(1200,900); jframe.setTitle(Draw SoftWare); jframe.setDefaultCloseOperation(3); jframe.setLocationRelativeTo(null); jframe.setVisible(true); Graphics gjframe.getGraphics();//本句即是画笔的定义句 } public static void main(String[] args){ DrawSoftWare dnew 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 jframenew JFrame(); jframe.setSize(600,600); jframe.setTitle(Draw SoftWare); jframe.setDefaultCloseOperation(3); jframe.setLocationRelativeTo(null); jframe.setVisible(true); FlowLayout flowLayoutnew FlowLayout(); jframe.setLayout(flowLayout); JButton jbuttonnew JButton(直线); JButton jbutton2new JButton(矩形); JButton jbutton3new JButton(等腰三角形); JButton jbutton4new JButton(任意三角形); JButton jbutton5new JButton(多边形); JButton Red new JButton(红色); jframe.add(jbutton); jframe.add(jbutton2); jframe.add(jbutton3); jframe.add(jbutton4); jframe.add(jbutton5); jframe.add(Red); Graphics gjframe.getGraphics(); DrawListener drawListenernew DrawListener(); jframe.addMouseListener(drawListener); drawListener.grg; 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 dnew 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) { x1e.getX(); y1e.getY(); } public void mouseReleased(MouseEvent e) { x2e.getX(); y2e.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) { colore.getActionCommand(); if(!color.equals(红色)){ Command e.getActionCommand(); } flag true; System.out.println(Command); System.out.println(color); } public void mousePressed(MouseEvent e){ if(flagtrue){ x1e.getX(); y1e.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(多边形)flagtrue) { gr.drawLine(x1, y1, x2, y2); flag false; } } public void mouseClicked(MouseEvent e){ x3e.getX(); y3e.getY(); if(Command.equals(任意三角形)){ gr.drawLine(x1, y1, x3, y3); gr.drawLine(x2, y2, x3, y3); flagtrue; } if(Command.equals(多边形)){ gr.drawLine(x2, y2, x3, y3); x2x3; y2y3; int timese.getClickCount(); if (times2){ gr.drawLine(x1, y1, x3, y3); flagtrue; } } } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } }其中!字符串1.equals(字符串2)表示字符串1不等于字符串2。

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价