JavaSE系列代码63:小程序的应用

In Java, an application can contain multiple threads. Each thread performs a specific task, and can execute concurrently with other threads. Multithreading minimizes the idle time of the system, improves CPU utilization. Multithreading programming environment hides the fact that CPU switches between tasks with a convenient model

import java.awt.*; 
import java.awt.event.*; 
import java.applet.Applet;
public class Javase_63 extends Applet implements MouseListener
{
  int x1,y1,x2,y2,width,height;
  boolean flag=false;
  public void init()     //初始化方法
  {
    this.addMouseListener(this); //设置小程序本身为自己的监听者
  }
  public void mousePressed(MouseEvent e)
  {
    flag=true;
    x1= e.getX();
    y1= e.getY();
  }
  public void mouseReleased(MouseEvent e) //松开鼠标事件源的处理操作
  {
    x2= e.getX();      //取得拖动鼠标时的x坐标
    y2= e.getY();      //取得拖动鼠标时的y坐标
    repaint();
  }
  public void paint(Graphics g)
  {
    if(flag)
    {
      width=Math.abs(x2-x1);
      height=Math.abs(y2-y1);
      if (x1>x2 && y1>y2)    //取得矩形框左上角的坐标(x1,y1)
      { x1=x2;  y1=y2; }
      else if(x1>x2 && y1<y2) x1=x2;
      else if(x1<x2 && y1>y2) y1=y2;
      g.drawRect(x1,y1,width,height);  //画矩形
    }
  }
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mouseClicked(MouseEvent e) {}
}

<Javase_63.html>
<html>
<applet
  code="Javase_63.class"
  height=150
  width=300
  alt="很抱歉,您的浏览器不支持Java applet。"
  align="Middle"
  vspace="25">
</applet>
</html>
发布了73 篇原创文章 · 获赞 189 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/blog_programb/article/details/105567974