`

java applet例子---画曲线

阅读更多
applet是java小应用程序,可以继承Applet也可继承JApplet,前者属于awt包中,后者属于swing包
package applet;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class DrawCurveApplet extends Applet implements MouseMotionListener,MouseListener{

	private int startX = 0;
	private int startY = 0;
	@Override
	public void mouseDragged(MouseEvent e) { //MouseMotionListener
//		鼠标处于拖拽状态时绘制曲线
		Graphics g = this.getGraphics();
		
		g.setColor(Color.red);
		g.drawLine(startX, startY, e.getX(), e.getY());
		startX = e.getX();
		startY = e.getY();
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {//MouseMotionListener
		
	}
	
	public void init(){
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}

	@Override
	public void mouseClicked(MouseEvent arg0) { //MouseListener
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) { //MouseListener
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) { //MouseListener
		
	}

	@Override
	public void mousePressed(MouseEvent e) { //MouseListener
//		记录鼠标按下的起始点的坐标
		startX = e.getX();
		startY = e.getY();
	}

	@Override
	public void mouseReleased(MouseEvent arg0) { //MouseListener
		
	}

}

执行结果,在applet界面上用鼠标拖拽,即可画出曲线。如下图


  • 大小: 8.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics