简易画板—画直线version1.0

package drawLine;
/**
 * @author FlyingFire
 * @date:2011-6-1 下午04:44:47

 * @summary:画板窗口最小化以后,所有的直线全部消失
 */
public class DrawUI extends javax.swing.JFrame{
 java.awt.Graphics g;
 UIListener listener;
 public void showUI(){
  this.setTitle("简易画板");
  this.setSize(600,500);
  this.setLocationRelativeTo(null);
  this.setDefaultCloseOperation(3);
  this.setVisible(true);
  g=this.getGraphics();
  listener=new UIListener(g);
  this.addMouseListener(listener);
 }
 public void paint(){
  System.out.println("listener");
  for(int i=0;i<listener.list.size();i++){
   g.drawLine(listener.list.get(i).x1,listener.list.get(i).y1,listener.list.get(i).x2,listener.list.get(i).y2);
  }
 }
public static void main(String a[]){
 DrawUI dui=new DrawUI();
 dui.showUI();
 dui.paint();
}
}

*********************************************************

package drawLine;

public class LinePointers {
 
    public int x1;
    public int y1;
    public int x2;
    public int y2;
 public LinePointers(int x1,int y1,int x2,int y2){
 
  this.x1=x1;
  this.y1=y1;
  this.x2=x2;
  this.y2=y2;
 }
}

****************************************************

package drawLine;

import java.awt.event.MouseEvent;

public class UIListener extends java.awt.event.MouseAdapter{
 java.util.ArrayList<LinePointers> list=new java.util.ArrayList<LinePointers>();
 
 private java.awt.Graphics g;
 private int x1,y1,x2,y2;
 private int countClick=1;
 public UIListener(java.awt.Graphics g){
  this.g=g;
 }
  public void mouseClicked(MouseEvent e) {
   System.out.println("监听了");
   if(countClick==1){
    x1=e.getX();
    y1=e.getY();
    countClick++;
    System.out.println(x1+"x1------y1"+y1);
   }
   else{
    x2=e.getX();
    y2=e.getY();
    System.out.println(x2+"x2------y2"+y2);
    g.drawLine(x1, y1, x2, y2);
    countClick=1;
    LinePointers twoPoints=new LinePointers(x1,y1,x2,y2);
    list.add(twoPoints);
   }
  
  
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值