-
目的
-
绘制一个画图界面,设置两种按钮,分别是绘图按钮与颜色按钮
-
实现绘制不同颜色的各种图形
-
DrawUI .java
import javax.swing.*;
import java.awt.*;
public class DrawUI {
DrawListen dl = new DrawListen();
//创建监听器对象
String[] btnstrs ={"直线","矩形","圆形","点击三角形","等腰三角形","多边形","实心矩形","实心圆"};
//定义一个图形数组
String[] colorstrs ={"红","橘","黄","绿","蓝","白","紫","黑"};
//定义一个颜色数组
public void initUI() {
JFrame jf = new JFrame();
//创建窗体
jf.setTitle("绘制图形");//标题
jf.setSize(1000, 1000);//大小
jf.setDefaultCloseOperation(3);//操作选项
jf.setLayout(new FlowLayout());//流式布局
for(int i=0;i< btnstrs.length;i++){
//循环执行“按钮字符次数”次
JButton btn=new JButton();
//创建按钮对象
btn.setText(btnstrs[i]);
//按钮字符数组的字符-赋值给按钮
btn.setActionCommand("shapeOP");
//按钮设置命令为绘制图形操作
jf.add(btn);
//窗体添加按钮
btn.addActionListener(dl);
//按钮添加动作监听器
}
for(int i=0;i< colorstrs.length;i++){
//循环执行“颜色字符次数”次
JButton btn =new JButton();
//创建按钮对象
btn.setText(colorstrs[i]);
//颜色字符数组的颜色字符-赋值给按钮
btn.setActionCommand("colorOP");
//按钮设置命令为颜色操作
jf.add(btn);
//窗体添加按钮
btn.addActionListener(dl);
//按钮添加动作监听器
}
jf.setVisible(true);//可视化
jf.addMouseListener(dl);
//窗体添加鼠标监听器
dl.setGraphics(jf.getGraphics());
//画布与监听器绑定
}
public static void main(String[]args){
DrawUI drawUI=new DrawUI();
drawUI.initUI();
}
}
- DrawListen.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class DrawListen implements MouseListener,ActionListener {
//画画监听器类实现动作和鼠标监听器接口
//一个类可以实现多个接口
private int x1, y1, x2, y2;
private Graphics g;
//属性封装
public void setGraphics(Graphics g) {
this.g=g;
}
//设置set方法
private String btnStr;
//封装按钮字符属性
public void actionPerformed(ActionEvent e){
System.out.println("按钮被点击了");
String action=e.getActionCommand();
//按钮命令转化为动作属性
if(action.equals("shapeOP")){
JButton btn = (JButton) e.getSource();
//按钮获取来源,与DrawUI界面联系起来
btnStr = btn.getText();}
else if
(action.equals("colorOP")) {
JButton btn =(JButton) e.getSource();
//
String colorstr = btn.getText();
switch (colorstr){
case"红":g.setColor(Color.red);break;
case"橘":g.setColor(Color.orange);break;
case"黄":g.setColor(Color.yellow);break;
case"绿":g.setColor(Color.green);break;
case"蓝":g.setColor(Color.blue);break;
case"白":g.setColor(Color.white);break;
case"紫":g.setColor(Color.magenta);break;
case"黑":g.setColor(Color.BLACK);break;
}
}
}
int count=0;
int x3,y3,x4,y4,x5,y5;
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("点击");
int x=e.getX();
int y=e.getY();
g.fillOval(x-2,y-2,4,4);
if(btnStr.equals("点击三角形")){
if(count==0){x3=x;y3=y;count++;}
else if (count==1) {x4=x;y4=y;g.drawLine(x3,y3,x4,y4);count++;}
else if (count==2) {x5=x;y5=y;g.drawLine(x3,y3,x5,y5);g.drawLine(x4,y4,x5,y5);count=0;}
} else if (btnStr.equals("多边形")) { }
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("按下");
x1=e.getX();
y1=e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("释放");
x2=e.getX();
y2=e.getY();
if(btnStr.equals("直线")){
g.drawLine(x1,y1,x2,y2);
} else if (btnStr.equals("矩形")) {
g.drawLine(x1,y1,x2,y1);
g.drawLine(x1,y1,x1,y2);
g.drawLine(x2,y1,x2,y2);
g.drawLine(x1,y2,x2,y2);
} else if (btnStr.equals("等腰三角形")) {
g.drawLine((x1+x2)/2,y1,x1,y2);
g.drawLine((x1+x2)/2,y1,x2,y2);
g.drawLine(x1,y2,x2,y2);
} else if (btnStr.equals("圆形")) {
g.fillOval(x2-x1,y2-y1,100,100);
} else if (btnStr.equals("多边形")) {
g.drawLine(x1,y1,(x1+x2)/2,(3*y1-y2)/2);
g.drawLine((x1+x2)/2,(3*y1-y2)/2,x2,y1);
g.drawLine(x2,y1,x2,y2);
g.drawLine(x2,y2,(x1+x2)/2,(3*y2-y1)/2);
g.drawLine((x1+x2)/2,(3*y2-y1)/2,x1,y2);
g.drawLine(x1,y2,x1,y1);
}
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("进入");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("退出");
}
}