java画板中画直线_画图板(画直线)

1、创建一个画图板(界面)

public class DrawUI {

//显示界面的方法

public void show(){

//创建界面对象

JFrame jframe = new JFrame();

//设置界面的尺寸、位置居中、关闭、标题

jframe.setSize(1000, 900);

jframe.setLocationRelativeTo(null);

jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jframe.setTitle("画图板v2.0");

//设置界面可见

jframe.setVisible(true);

}

//主方法

public static void main(String[] args){

DrawUI UI = new DrawUI();

UI.show();

}

}

2、创建监听器类

public class Listener implements MouseListener{

/**

* Invoked when the mouse button has been clicked (pressed

* and released) on a component.

*/

//鼠标点击

public void mouseClicked(MouseEvent e){}

/**

* Invoked when a mouse button has been pressed on a component.

*/

// 鼠标按下

public void mousePressed(MouseEvent e){}

/**

* Invoked when a mouse button has been released on a component.

*/

//鼠标松开

public void mouseReleased(MouseEvent e){}

/**

* Invoked when the mouse enters a component.

*/

// 进入

public void mouseEntered(MouseEvent e){}

/**

* Invoked when the mouse exits a component.

*/

// 退出 荐﵄Ɂ .RichEdit20 W 鑓�cዀ괋翸 Ɂ

public void mouseExited(MouseEvent e){}

}

3、在画图板上添加监听器,并实现画出直线。

(1)在画图板中创建监听器并添加监听器(写在画图板“可见”之前 ):建立画图板与监听器之间的联系。

//创建监听器

Listener draw = new Listener();

//添加监听器

jframe.addMouseListener(draw);

然后转到监听器类中输入代码

(2)实现画图 实现监听器在画图板上画图(在此引入Graphics:用于画图工具)

<1>在创建的监听器类中定义我们需要画的直线(Graphics),并画出它(这可以理解为在告诉监听器要画什么)

//记录坐标:因为两点确定一直线,所以定义两起始点的坐标即可

int x1,y1,x2,y2;

//定义Graphics

Graphics graph1;

//设置画图方法:graph1为我们要画的直线,graph2为鼠标要画的图,要使鼠标画的图是直线。

public void setGraphics(Graphics graph2){

graph1 = graph2;

<2>在画图板中获取监听器中要画的直线

在画图板中输入一下代码(输在画图板“可见”之后)

//获取Graphics

Graphics graph = jframe.getGraphics();

//给监听器的画布对象赋值

draw.setGraphics(graph);

(3)鼠标画出直线

在鼠标按下事件中输入起点坐标

public void mousePressed(MouseEvent e){

//获取按下信息

x1=e.getX();

y1=e.getY();

}

在鼠标松开事件中输入终点坐标,并画出直线

public void mouseReleased(MouseEvent e){

//获取松开信息

x2=e.getX();

y2=e.getY();

//画线

graph1.drawLine(x1,y1,x2,y2);

}

完整:

界面

package Draw;

import java.awt.Graphics;

import javax.swing.JFrame;

public class DrawUI {

public void show(){

JFrame jframe = new JFrame();

jframe.setSize(1000, 900);

jframe.setLocationRelativeTo(null);

jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jframe.setTitle("画图板v2.0");

//创建监听器

Listener draw = new Listener();

//添加监听器

jframe.addMouseListener(draw);

jframe.setVisible(true);

//获取Graphics

Graphics graph = jframe.getGraphics();

//给监听器的画布对象赋值

draw.setGraphics(graph);

}

//主方法

public static void main(String[] args){

DrawUI UI = new DrawUI();

UI.show();

}

}

监听器

package Draw;

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

public class Listener implements MouseListener{

//记录坐标

int x1,y1,x2,y2;

//定义Graphics

Graphics graph1;

//设置画图方法

public void setGraphics(Graphics graph2){

graph1 = graph2;

}

/**

* Invoked when the mouse button has been clicked (pressed

* and released) on a component.

*/

public void mouseClicked(MouseEvent e){}

/**

* Invoked when a mouse button has been pressed on a component.

*/

public void mousePressed(MouseEvent e){

//获取按下信息

x1=e.getX();

y1=e.getY();

}

/**

* Invoked when a mouse button has been released on a component.

*/

public void mouseReleased(MouseEvent e){

//获取松开信息

x2=e.getX();

y2=e.getY();

//画线

graph1.drawLine(x1,y1,x2,y2);

}

/**

* Invoked when the mouse enters a component.

*/

public void mouseEntered(MouseEvent e){}

/**

* Invoked when the mouse exits a component.

*/

public void mouseExited(MouseEvent e){}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值