package lcr0330.v3;
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 DrawListener implements MouseListener , ActionListener {
String ae = "";
int j = 0;
int k = 0;
int cout = 0;
Graphics g;
int x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6;
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (ae.equals("三角形")) {//三角形
if (j == 0) {
x1 = x;
y1 = y;
//System.out.println("点击 x1:"+x1+" j:"+j);
j++;
} else if (j == 1) {
x2 = x;
y2 = y;
g.drawLine(x1, y1, x2, y2);
//System.out.println("点击 x1:"+x1+" x2:"+x2+" j:"+j);
j++;
} else if (j == 2) {
x3 = x;
y3 = y;
g.drawLine(x3, y3, x2, y2);
g.drawLine(x1, y1, x3, y3);
x3 = 0;
y3 = 0;
//System.out.println("点击 x1:"+x1+" x2:"+x2+" x3:"+x3+" j:"+j);
j = 0;
}
} else if (ae.equals("多边形")) {//多边形
if(cout == 0){
x4 = x;
y4 = y;
cout++;
}else if (cout == 1){
x5 = x;
y5 = y;
g.drawLine(x5, y5, x4, y4);
cout++;
} else if (cout == 2) {
x6 = x;
y6 = y;
if(Math.sqrt(Math.pow(x6-x4,2)+Math.pow(y6-y4,2)) > 50) {
g.drawLine(x6, y6, x5, y5);
x5 = x6;
y5 = y6;
cout = 2;
} else {
g.drawLine(x5,y5,x4,y4);
cout = 0;
}
}
}
}
@Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (ae.equals("直线") || ae.equals("矩形") || ae.equals("等腰三角形")|| ae.equals("圆形")) {
x1 = x;
y1 = y;
}
}
@Override
public void mouseReleased(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if(ae.equals("直线")){
x2 = x;
y2 = y;
g.drawLine(x1,y1,x2,y2);
} else if (ae.equals("矩形")) {
x2 = x;
y2 = y;
g.drawLine(x1,y1,x2,y1);
g.drawLine(x2,y1,x2,y2);
g.drawLine(x2,y2,x1,y2);
g.drawLine(x1,y2,x1,y1);
} else if (ae.equals("等腰三角形")) {
x2 = x;
y2 = y;
int midX = (x1 + x2) / 2;
int midY = (y1 + y2) / 2;
// 计算向量
int vectorX = x2 - x1;
int vectorY = y2 - y1;
// 旋转向量
int rotatedVectorX = -vectorY;
int rotatedVectorY = vectorX;
// 计算另一个顶点坐标
int x3 = midX + rotatedVectorX;
int y3 = midY + rotatedVectorY;
g.drawLine(x1,y1,x2,y2);
g.drawLine(x1,y1,x3,y3);
g.drawLine(x3,y3,x2,y2);
} else if (ae.equals("圆形")) {
x2 = x;
y2 = y;
int midX = (x1 + x2) / 2;
int midY = (y1 + y2) / 2;
int d = (int)Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y2-y1,2));
int x3 = (x1+x2)/2 - d/2;
int y3 = (y1+y2)/2 - d/2;
int x4 = (x1+x2)/2 + d/2;
int y4 = (y1+y2)/2 + d/2;
for( int i = x3;i < x4;i++){
for (int j = y3;j < y4;j++){
Double h = Math.sqrt(Math.pow(i-midX,2)+Math.pow(j-midY,2));
if(h < d/2+2 && h > d/2-2){
g.drawLine(i,j,i,j);
}
}
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
ae = e.getActionCommand();
}
}
三角形代码逻辑
画三角形中使用三个点(x1,y1)(x2,y2)(x3,y3)来进行,定义了一个j来代表第几个点,初始为0。点击一次后获得坐标,通过if语句判断j的值来讲获取的坐标传值给第几个点的坐标,在j=0时坐标传值给x1,y1,j++;后点击第二次通过if语句判断在j=1时传值给x2,y2并且画出第一个点与第二个点,j++;第三次点击后通过if语句判断在j=2时传值给x3,y3,画出第三个点与第二个点的连线以及第三个点与第一个点的连线。完成三角形的绘制,后将j清零开始下一个三角形的绘制。
多边形代码逻辑
画多边形中也使用三个点x4,y4,x5,y5,x6,y6,与三角形类似,当我们点击第三个点时,此时有一个点对于后面的绘制没有用处,第一个点(x4,y4)需要我们与最后一个点进行判断,当我们达到第一个点的范围内是连接此两点。而第三个点(x6,x6)的坐标需要与下一个进行连接,因此第二个点(x5,y5)无用,我们可以将第三个点的坐标传给第二点保存,第三个点接着记录下一个坐标,直到达到第一个点判定的范围后结束。
第一次:
if(cout == 0){
x4 = x;
y4 = y;
cout++;
}
第二次:前两次与画三角形无异
else if (cout == 1){
x5 = x;
y5 = y;
g.drawLine(x5, y5, x4, y4);
cout++;
}
第三次:用if进行判断到x6,y6的坐标与第一个点的坐标的距离小于某个值时连接x5,x6与第一个点
当大于时继续连接x6,y6与x5,y5并把当前值传给x5与y5后继续让cout=2进行这一步直到小于某个值收尾相连并且清零cout。
else if (cout == 2) {
x6 = x;
y6 = y;
if(Math.sqrt(Math.pow(x6-x4,2)+Math.pow(y6-y4,2)) > 50) {
g.drawLine(x6, y6, x5, y5);
x5 = x6;
y5 = y6;
cout = 2;
} else {
g.drawLine(x5,y5,x4,y4);
cout = 0;
}
}
注:使用if elseif来处理多条件的区间性判定进行其中一项后不会接着进行。