JAVA作业 窗口响应鼠标事件:鼠标在画布上画圆

题目:生成一个窗口响应鼠标事件,可以拖住鼠标在画布上画出蓝色的圆。鼠标按下和弹起的位置为圆的直径。


思想:

1、编写框架类与画布类即可实现画板系列问题;

2、编写Circle类,用于描述圆对象,因此属性为int x1, y1, x2, y2表示鼠标按下和弹起的位置,Color color表示圆的颜色;

3、DrawFrame类负责提供视图,用户可通过此类GUI看到画板。

4、DrawTable类负责实现画布,用于监控用户鼠标操作,存储用户画出的圆并时刻刷新显示;属性为int x1, y1, x2, y2表示鼠标按下和弹起的位置,Vector<Circle> vectorCircle 用于存储用户所有画过的圆;

源代码:

Circle.java

package drawCircle;

import java.awt.Color;
import java.awt.Graphics;

/**
* @author TX
* @version 创建时间:2016年12月3日 下午2:54:54
* 类说明:圆类
*/
public class Circle {
	int x1, y1, x2, y2;
	Color color = Color.blue;
	public Circle(int x1, int y1, int x2, int y2)
	{
		this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2;
	}
    void draw(Graphics g){
    	        g.setColor(color);
		g.fillOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
	}
}
DrawFrame.java

package drawCircle;

import javax.swing.JFrame;

/**
* @author TX
* @version 创建时间:2016年12月3日 下午3:10:40
* 类说明:JFrame框架类
*/
public class DrawFrame extends JFrame{
	private DrawTable drawTable;
	public DrawFrame(String s)
	{
		super(s);
		drawTable = new DrawTable();
		this.add(drawTable);
	}
	
	public static void main(String[] args) {
		DrawFrame drawCir = new DrawFrame("画圆");
		drawCir.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		drawCir.setSize(550, 450);
		drawCir.setLocation(400, 100);
		drawCir.setVisible(true);
	}
}
DrawTable.java
package drawCircle;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import javax.swing.JPanel;
/**
* @author TX
* @version 创建时间:2016年12月3日 下午2:30:24
* 类说明:JPanel画板类
*/
public class DrawTable  extends JPanel{

	int x1, y1, x2, y2;
	Vector<Circle> vectorCircle = new Vector<Circle>();
	public DrawTable()
	{
		JPanel panel = new JPanel();
		addMouseListener(new MouseListener()
		{
			public void mousePressed(MouseEvent e)
			{
				x1 = e.getX();
				y1 = e.getY();
				Circle c = new Circle(x1,y1,x1,y1);
				vectorCircle.add(c);
			}//当用户按下鼠标按钮时发生
			public void mouseReleased(MouseEvent e)
			{
			}//当用户松开鼠标按钮时发生
			
			public void mouseClicked(MouseEvent e) {
			}
			
			public void mouseEntered(MouseEvent e) {
			}
			
			public void mouseExited(MouseEvent e) {
			}
		}
        );
		addMouseMotionListener(new MouseMotionListener()
		{
			public void mouseMoved(MouseEvent e)
			{
			}//当用户按下鼠标按钮并在松开之前进行移动时发生

			public void mouseDragged(MouseEvent e)
			{
				vectorCircle.get(vectorCircle.size()-1).x2 = e.getX();
				vectorCircle.get(vectorCircle.size()-1).y2 = e.getY();
		    	repaint();
			}//当鼠标在组件上移动而 不时拖动时发生
		}
		);
	}
	protected void paintComponent(Graphics g){
		g.clearRect(0, 0, getWidth(), getHeight());
		for(int i = 0; i < vectorCircle.size(); i++)	
		{
			vectorCircle.get(i).draw(g);
	    }
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值