package com.cbs;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* Draw类,用于界面的初始化
*
* @author CBS
*
*/
public class Draw {
public static void main(String[] args) {
Draw t = new Draw();
t.showUI();
}
// 界面初始化方法
public void showUI() {
JFrame jf = new JFrame();
jf.setTitle("画图");
jf.setSize(700, 700);
jf.setDefaultCloseOperation(3);
jf.setLocationRelativeTo(null);
FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
jf.setLayout(layout);
JButton drawLine = new JButton("画直线");
jf.add(drawLine);
JButton drawOval = new JButton("画椭圆");
jf.add(drawOval);
JButton drawArc = new JButton("画曲线");
jf.add(drawArc);
JButton drawPolygon = new JButton("三角形");
jf.add(drawPolygon);
JButton jb1 = new JButton();
jb1.setBackground(Color.RED);
jf.add(jb1);
jb1.setPreferredSize(new Dimension(30, 30));
JButton jb2 = new JButton();
jb2.setBackground(Color.GREEN);
jf.add(jb2);
jb2.setPreferredSize(new Dimension(30, 30));
jf.setVisible(true);
Graphics g = jf.getGraphics();// 获取当前的画笔
DrawListener dl = new DrawListener(g);// 实例化DrawListener类的对象
jf.addMouseListener(dl);// 为窗体添加鼠标事件监听方法
jf.addMouseMotionListener(dl);// 为窗体添加鼠标移动事件监听方法
// 为按钮添加动作监听
drawLine.addActionListener(dl);
drawOval.addActionListener(dl);
jb1.addActionListener(dl);
jb2.addActionListener(dl);
drawArc.addActionListener(dl);
drawPolygon.addActionListener(dl);
}
}
package com.cbs;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
/**
* 事件处理类
*
* @author CBS
*
*/
public class DrawListener implements MouseListener, MouseMotionListener,
ActionListener {
private int x1, y1, x2, y2;// 记录两次鼠标的点击坐标
private Graphics g;// 从界面获取画布对象
private String str;// 记录当前按钮的信息,区分不同的按钮
private Color color;// 记录画笔的颜色信息
private int f = 1;// 控制变量,用于更新坐标
public DrawListener(Graphics g) {
this.g = g;
}
//鼠标按下时的处理方法
public void mousePressed(MouseEvent e) {
// 记录第一次点击的位置;由对象e得到
if (f == 1) {
x1 = e.getX();
y1 = e.getY();
}
}
//鼠标点击时的处理方法
public void mouseClicked(MouseEvent e) {
if ("三角形".equals(str)) {
System.out.println("sanjaioxing");
int x, y;
x = e.getX();
y = e.getY();
g.setColor(color);
g.drawLine(x, y, x1, y1);
g.drawLine(x2, y2, x, y);
f = 1;
}
}
// 鼠标释放时的处理方法
public void mouseReleased(MouseEvent e) {
// 记录鼠标释放时的坐标
if (f == 1) {
x2 = e.getX();
y2 = e.getY();
}
// 两个坐标的得到了,可以用于直线的绘制,调用画布对象g方法,在界面上面画出直线
if ("画直线".equals(str)) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
if ("画椭圆".equals(str)) {
g.setColor(color);
g.drawOval(x1, y1, x2, y2);
}
if ("三角形".equals(str) && f == 1) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
f++;
}
}
// 鼠标进入时的处理方法
public void mouseEntered(MouseEvent e) {
}
// 鼠标退出时的处理方法
public void mouseExited(MouseEvent e) {
}
public void actionPerformed(ActionEvent e) {
if ("".equals(e.getActionCommand())) {
JButton jb = (JButton) e.getSource();
color = jb.getBackground();
} else {
str = e.getActionCommand();
}
}
// 鼠标拖动时的处理方法
public void mouseDragged(MouseEvent e) {
if ("画曲线".equals(str)) {
int x, y;
x = e.getX();
y = e.getY();
g.setColor(color);
g.drawLine(x1, y1, x, y);
x1 = x;
y1 = y;
}
}
// 鼠标释放时的移动方法
public void mouseMoved(MouseEvent e) {
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有