①使用鼠标随意画线②画直线(显示直线轨迹)③画直线(不显示直线轨迹)
Posted jianqiao123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了①使用鼠标随意画线②画直线(显示直线轨迹)③画直线(不显示直线轨迹)相关的知识,希望对你有一定的参考价值。
①PaintJFrame类
package drawLine; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PaintJFrame extends JFrame { public PaintJFrame() { super("使用鼠标随意画线"); this.setBounds(400,300,400,300); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.getContentPane().add(new PaintCanvas()); //创建自定义画布组件 this.setVisible(true); } class PaintCanvas extends Canvas implements MouseListener, MouseMotionListener //画布组件 { Point start, end; public PaintCanvas() { start=end=null; this.addMouseMotionListener(this); this.addMouseListener(this); } public void mousePressed(MouseEvent ev) //按下鼠标事件处理方法,以下实现MouseListener接口 { end = new Point(ev.getX(), ev.getY()); } public void mouseReleased(MouseEvent ev) {} //释放鼠标 public void mouseClicked(MouseEvent ev) {} //单击鼠标 public void mouseEntered(MouseEvent ev) {} //鼠标进入 public void mouseExited(MouseEvent ev) {} //鼠标离开 public void mouseMoved(MouseEvent ev){} //移动鼠标,实现MouseMotionListener接口 public void mouseDragged(MouseEvent ev) //拖动鼠标,实现MouseMotionListener接口 { start = end; end = new Point(ev.getX(), ev.getY()); repaint(); } public void paint(Graphics g) //在Canvas上作图 { g.setColor(Color.blue); g.fillOval(10, 10, 20,30); g.drawOval(10, 40, 20, 30); if (start!=null && end!=null) { g.setColor(Color.blue); //设置画线颜色 g.drawLine(start.x, start.y, end.x, end.y);//画直线 } } public void update(Graphics g) //更新组件 { paint(g); } }//PaintCanvas类结束 public static void main(String arg[]) { new PaintJFrame(); } }
②PaintLineJFrame类
package drawLine; // 使用鼠标画直线 //显示直线轨迹 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PaintLineJFrame extends JFrame { public PaintLineJFrame() { super("使用鼠标画直线"); this.setBounds(400,300,400,300); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.getContentPane().add(new PaintLineCanvas()); //创建自定义画布组件 this.setVisible(true); } //画布内部类,响应鼠标事件和鼠标移动事件 class PaintLineCanvas extends Canvas implements MouseListener, MouseMotionListener { Point start, end, lastend; public PaintLineCanvas() { start=end=lastend=null; this.addMouseMotionListener(this); this.addMouseListener(this); } public void mousePressed(MouseEvent ev) //按下鼠标事件处理方法,以下实现MouseListener接口 { start = new Point(ev.getX(), ev.getY()); } public void mouseReleased(MouseEvent ev) //释放鼠标 { end = new Point(ev.getX(), ev.getY()); this.repaint(); } public void mouseClicked(MouseEvent ev) {} //单击鼠标 public void mouseEntered(MouseEvent ev) {} //鼠标进入 public void mouseExited(MouseEvent ev) {} //鼠标离开 public void mouseMoved(MouseEvent ev){} //移动鼠标,实现MouseMotionListener接口 public void mouseDragged(MouseEvent ev) //拖动鼠标,实现MouseMotionListener接口 { lastend = end; end = new Point(ev.getX(), ev.getY()); this.repaint(); //调用paint(Graphics)方法,重画 } public void paint(Graphics g) //在Canvas上作图 { if (start!=null && lastend!=null) { //?? g.setXORMode(getBackground()); g.setColor(this.getBackground()); g.drawLine(start.x,start.y,lastend.x,lastend.y);//用背景色画线即擦除原线 // g.drawOval(start.x, start.y, lastend.x, lastend.y); //画椭圆 g.setColor(Color.blue); //设置画线颜色 g.drawLine(start.x, start.y, end.x, end.y); //画直线 // g.drawOval(start.x, start.y, end.x, end.y); //画椭圆 // g.drawRect(start.x, start.y, end.x, end.y); //画圆角矩形 } } public void update(Graphics g) //更新组件 { this.paint(g); } }//PaintLineCanvas类结束 public static void main(String arg[]) { new PaintLineJFrame(); } }
③PaintLineJFrame1类
package drawLine; // 使用鼠标画直线。 //只响应鼠标事件,没有响应鼠标拖动事件,松开鼠标时才画,没有显示直线轨迹。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PaintLineJFrame1 extends JFrame { public PaintLineJFrame1() { super("使用鼠标画直线"); this.setBounds(400,300,400,300); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.getContentPane().add(new PaintLineCanvas()); //创建自定义画布组件 this.setVisible(true); } class PaintLineCanvas extends Canvas implements MouseListener //画布 { Point start, end; public PaintLineCanvas() { start=end=null; this.addMouseListener(this); } public void mousePressed(MouseEvent ev) //按下鼠标事件处理方法,以下实现MouseListener接口 { start = new Point(ev.getX(), ev.getY()); } public void mouseReleased(MouseEvent ev) //释放鼠标 { end = new Point(ev.getX(), ev.getY()); repaint(); } public void mouseClicked(MouseEvent ev) {} //单击鼠标 public void mouseEntered(MouseEvent ev) {} //鼠标进入 public void mouseExited(MouseEvent ev) {} //鼠标离开 public void paint(Graphics g) //在Canvas上作图 { System.out.println("paint(Graphics g)"); if (start!=null && end!=null) { g.setColor(Color.blue); //设置画线颜色 g.drawLine(start.x, start.y, end.x, end.y); //画直线 } } public void update(Graphics g) //更新组件时,自动执行 { System.out.println("update(Graphics g)"); paint(g); } }//PaintLineCanvas类结束 public static void main(String arg[]) { new PaintLineJFrame1(); } }
以上是关于①使用鼠标随意画线②画直线(显示直线轨迹)③画直线(不显示直线轨迹)的主要内容,如果未能解决你的问题,请参考以下文章