java 用鼠标点击屏幕随意三个点 画一个三角形 并求出小三角形的周长和...
发布网友
发布时间:2024-10-21 20:07
我来回答
共2个回答
热心网友
时间:2024-11-09 16:36
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class DrawTriangle extends JFrame {
JLabel jl;
public DrawTriangle() {
DrawPanel dp = new DrawPanel();
dp.addMouseListener(dp);
add(dp);
jl = new JLabel("\t");
jl.setBorder(BorderFactory.createTitledBorder("计算结果"));// 设置jl的边框
add(jl, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(300, 200, 500, 500);
setResizable(false);
setTitle("画图 三角形");
setVisible(true);
}
public static void main(String[] args) {
new DrawTriangle();// 实例化窗口
}
class DrawPanel extends JPanel implements MouseListener {
ArrayList<Point> list = new ArrayList<Point>();// 保存点
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// Graphics2D对象才可使用此语句消除锯齿
if (list.size() == 1) {// 1个点的时候画点
g2d.setColor(Color.BLACK);
g2d.drawLine(list.get(0).x, list.get(0).y, list.get(0).x, list.get(0).y);
jl.setText("\t");
}
if (list.size() == 2) {// 2个点的时候画线
g2d.setColor(Color.BLUE);
g2d.drawLine(list.get(0).x, list.get(0).y, list.get(1).x, list.get(1).y);
}
if (list.size() == 3) {// 3个点的时候画三角形
g2d.setColor(Color.RED);
// 可以直接画三条线,我使用了画多边形的办法
int[] xPoints = { list.get(0).x, list.get(1).x, list.get(2).x };
int[] yPoints = { list.get(0).y, list.get(1).y, list.get(2).y };
g2d.drawPolygon(xPoints, yPoints, 3);// x坐标数组,y坐标数组,连线数
double x1 = xPoints[0], y1 = yPoints[0], x2 = xPoints[1], y2 = yPoints[1], x3 = xPoints[2],
y3 = yPoints[2];
// 下面是根据海伦公式求三角形的面积
double s1 = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
double s2 = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
double s3 = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
double p = (s1 + s2 + s3) / 2;
double a = Math.sqrt(p * (p - s1) * (p - s2) * (p - s3));
jl.setText("[取整后的结果] 周长" + Math.round(p * 2) + " 面积:" + Math.round(a));
list.clear();// 清空保存点的集合
}
}
public void mouseClicked(MouseEvent e) {
Point p = new Point(e.getX(), e.getY());
list.add(p);// 点击之后得到点的坐标,并保存到集合
repaint();// 点击后进行重绘
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
}
热心网友
时间:2024-11-09 16:38
public class Test{
static int width = 500,height = 300;
static int x1,x2,x3,y1,y2,y3,count=0;
static int length = 1;
static int xl,yl;
static BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
static Graphics2D g = image.createGraphics();
static JLabel jl;
public static void main(String[] args) {
JFrame jf = new JFrame("三角形");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗体关闭时的操作 退出程序
jf.setSize(width, height);
jf.setLocation(0, 0);
jf.setLayout(new BorderLayout());
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//消除锯齿
g.setBackground(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(Color.red);
jl= new JLabel(new ImageIcon(image));
jf.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(count==0){
g.clearRect(0, 0, width, height);
x1=e.getX()-xl;
y1=e.getY()-yl;
g.drawOval(x1, y1, length, length);
jl.setIcon(new ImageIcon(image));
jl.validate();
}else if(count==1){
x2=e.getX()-xl;
y2=e.getY()-yl;
if(x2==x1&&y2==y1){
return;
}
g.drawOval(x1, y1, length, length);
g.drawOval(x2, y2, length, length);
g.drawLine(x1, y1, x2, y2);
jl.setIcon(new ImageIcon(image));
jl.validate();
}else if(count==2){
x3=e.getX()-xl;
y3=e.getY()-yl;
if((x3==x1&&y3==y1)||(x3==x2&&y3==y2)){
return;
}
g.drawOval(x1, y1, length, length);
g.drawOval(x2, y2, length, length);
g.drawOval(x3, y3, length, length);
g.drawLine(x1, y1, x2, y2);
g.drawLine(x3, y3, x2, y2);
g.drawLine(x3, y3, x1, y1);
jl.setIcon(new ImageIcon(image));
jl.validate();
count=0;
getArea(x1, y1, x2, y2, x3, y3);
return;
}else{
return;
}
count++;
}
});
jf.add(jl);
jf.pack();
xl=jf.getInsets().left;
yl=jf.getInsets().top;
jf.setVisible(true);
}
public static float getArea(int x1,int y1,int x2,int y2,int x3,int y3 ){
float result=0;
double a = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
double b = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
double c = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
double p = (a+b+c)/2;
result = (float) Math.sqrt(p*(p-a)*(p-b)*(p-c));
System.out.println("三角形的面积为:"+result);
System.out.println("三角形的周长为:"+(float)p*2);
JOptionPane.showMessageDialog(null, "三角形的面积为:"+result+" 周长为:"+(float)p*2);
return result;
}
}