JavaSE series code 62: manual painting program

The cost of thread based is less. In multitasking, each process needs to allocate its own independent address space. Multiple threads can share the same address space and share the same process together. The cost of inter process call is lower than that of inter thread communication. The cost of inter thread switch is lower than that of inter process switch

import java.awt.*; 
import java.awt.event.*; 
public class Javase_62 extends Frame implements MouseMotionListener
{
  static int x1,y1,x2,y2;
  public static void main(String[] args)
  {
    Javase_62 frm=new Javase_62 ();
    frm.setTitle("交互式绘图");
    frm.setBounds(10,10,250,200);
    frm.addMouseMotionListener(frm);             //设置监听者
    frm.addMouseListener(new MyMouseList());     //设置监听者
    frm.setVisible(true);
  }
  //定义静态内部类MyMouseList,并继承自MouseAdapter
  static class MyMouseList extends MouseAdapter
  {
    public void mousePressed(MouseEvent e)
    {
      x1= e.getX();   //取得鼠标按下时的x坐标,作为起点的x坐标
      y1= e.getY();   //取得鼠标按下时的y坐标,作为起点的y坐标
    }
  }
  public void mouseDragged(MouseEvent e) //用鼠标拖动事件源的处理操作
  {
    x2= e.getX();      //取得拖动鼠标时的x坐标
    y2= e.getY();      //取得拖动鼠标时的x坐标
    Graphics g=getGraphics();
    g.drawLine(x1,y1,x2,y2);   //以(x1,y1)为起点,(x2,y2)为终点画线
    x1=x2;       //更新绘线起点的x坐标
    y1=y2;       //更新绘线起点的y坐标
  }
  public void mouseMoved(MouseEvent e){}
}

//app14_3.java       用鼠标拖动来绘画椭圆
import java.awt.*;
import java.awt.event.*;
public class app14_3 extends Frame implements MouseMotionListener, MouseListener
{
  static app14_3 frm=new app14_3();
  int px1,py1,px2,py2,status=0;
  int rpx1,rpy1,rpx2,rpy2;
  public static void main(String[] args)
  {
    frm.setTitle("鼠标拖动画椭圆");
    frm.setSize(250,230);
    frm.addMouseMotionListener(frm);       //设置监听者
    frm.addMouseListener(frm);             //设置监听者
    frm.setVisible(true);
  }
  public void mouseMoved(MouseEvent e)
  {
    px1=e.getX();
    py1=e.getY();
    status=0;
  }
  public void mouseDragged(MouseEvent e)     //用鼠标拖动来画椭圆
  {
    Graphics g=getGraphics();
    g.setColor(Color. yellow);       //设置当前绘图颜色为黄色
    g.setXORMode(Color.black);    //设置以异或模式作图
    if (status==1) g.drawOval (px1,py1,px2,py2);   //判断是否为新画的椭圆
    else {
      px1=e.getX();
      py1=e.getY();
      status=1;
    }
    px2=Math.abs(e.getX()-px1);    //计算长径
    py2= Math.abs(e.getY()-py1);    //计算宽径
    g.drawOval (px1,py1,px2,py2);    //画椭圆
    rpx1=px1;rpy1=py1;rpx2=px2;rpy2=py2;  //保存坐标位置
  }
  public void mouseReleased(MouseEvent e)
  {
    Graphics g=getGraphics();
    g.setColor(Color.red);
    g.drawOval (rpx1,rpy1,rpx2,rpy2);
  }
  public void mousePressed(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mouseClicked(MouseEvent e) {}
}
Published 73 original articles · praised 189 · 10,000+ views

Guess you like

Origin blog.csdn.net/blog_programb/article/details/105566690