java panel 坐标 绘图

设计一个Swing应用程序,在主窗体中包含3个文本框,用于输入3个坐标(格式为x,y并且x及y的范围在[0,99]),还包含一个按钮“确定”及一个JPanel。点击按钮,... 设计一个Swing应用程序,在主窗体中包含3个文本框,用于输入3个坐标(格式为x,y并且x及y的范围在[0,99]),还包含一个按钮“确定”及一个JPanel。点击按钮,程序首先判断输入的3个坐标是一个点还是一条直线或者是一个三角形,将判断结果显示在一个标签中,然后绘制相应的图形在JPanel。 展开
 我来答
匿名用户
2013-09-16
展开全部
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.util.regex.Pattern;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
public class Test extends JFrame implements ActionListener { private JLabel lblFirstPoint = new JLabel("第一个点(x,y):");
private JTextField tfFirstPoint = new JTextField(10);
private JLabel lblSecondPoint = new JLabel("第二个点(x,y):");
private JTextField tfSecondPoint = new JTextField(10);
private JLabel lblThirdPoint = new JLabel("第三个点(x,y):");
private JTextField tfThirdPoint = new JTextField(10);

private JPanel pDrawPanel = new JPanel();
private JLabel lblResult = new JLabel();
private JButton btnDraw = new JButton("确定");

private Test() {
super("绘图");
this.setSize(400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setExtendedState(MAXIMIZED_BOTH);
init();
}

private void init() {
Container c = this.getContentPane();

c.setLayout(new FlowLayout(FlowLayout.LEFT));

c.add(lblFirstPoint);
c.add(tfFirstPoint);
c.add(lblSecondPoint);
c.add(tfSecondPoint);
c.add(lblThirdPoint);
c.add(tfThirdPoint);

btnDraw.addActionListener(this);
c.add(btnDraw); c.add(lblResult); pDrawPanel.setBorder(new BevelBorder(1));
pDrawPanel.setPreferredSize(new Dimension(800, 600));
c.add(pDrawPanel);
}

public static void main(String[] args) {
Test t = new Test();
t.setVisible(true);
} @Override
public void actionPerformed(ActionEvent arg0) {
if (validateInput()) {
judgeResult();
}
} private void judgeResult() {
String strFirstPoint = tfFirstPoint.getText().trim();
String strSecondPoint = tfSecondPoint.getText().trim();
String strThirdPoint = tfThirdPoint.getText().trim();

String[] sFirstPoint = strFirstPoint.split("\\s*,\\s*");
Point p1 = new Point(Integer.parseInt(sFirstPoint[0]), Integer.parseInt(sFirstPoint[1]));

String[] sSecondPoint = strSecondPoint.split("\\s*,\\s*");
Point p2 = new Point(Integer.parseInt(sSecondPoint[0]), Integer.parseInt(sSecondPoint[1])); String[] sThirdPoint = strThirdPoint.split("\\s*,\\s*");
Point p3 = new Point(Integer.parseInt(sThirdPoint[0]), Integer.parseInt(sThirdPoint[1]));

if (p1.distance(p2) == 0 && p2.distance(p3) == 0) {
lblResult.setText("三点为同一点。");
} else {
if (isInSameLine(p1, p2, p3)) {
lblResult.setText("三点在同一直线上。");
} else {
lblResult.setText("三点构成一个三角形。");
}
} Graphics2D g2 = (Graphics2D) pDrawPanel.getGraphics();

g2.draw(new Line2D.Double(p1, p2));
g2.draw(new Line2D.Double(p2, p3));
g2.draw(new Line2D.Double(p3, p1));
} private boolean isInSameLine(Point p1, Point p2, Point p3) {
if ((p1.getX() == p2.getX() && p2.getX() == p3.getX()) ||
(p1.getY() == p2.getY() && p2.getY() == p3.getY())){
return true;
} else {
if (p2.getY() == p1.getY()) {
if (((p2.getY() - p1.getY()) / (p3.getY() - p1.getY()) == (p2.getX() - p1.getX()) / (p3.getX() - p1.getX()))) {
return true;
}
} else {
if (((p3.getY() - p1.getY()) / (p2.getY() - p1.getY()) == (p3.getX() - p1.getX()) / (p2.getX() - p1.getX()))) {
return true;
}
}
return false;
}
} private boolean validateInput() {
if (tfFirstPoint.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(this, "第一个点必须输入!", "错误", JOptionPane.ERROR_MESSAGE);
tfFirstPoint.requestFocus();
return false;
}
if (tfSecondPoint.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(this, "第二个点必须输入!", "错误", JOptionPane.ERROR_MESSAGE);
tfSecondPoint.requestFocus();
return false;
}
if (tfThirdPoint.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(this, "第三个点必须输入!", "错误", JOptionPane.ERROR_MESSAGE);
tfThirdPoint.requestFocus();
return false;
} Pattern p = Pattern.compile("^\\d+\\s*,\\s*\\d+$");
if (!p.matcher(tfFirstPoint.getText().trim()).matches()) {
JOptionPane.showMessageDialog(this, "第一个点输入内容不合法!", "错误", JOptionPane.ERROR_MESSAGE);
tfFirstPoint.requestFocus();
return false;
}
if (!p.matcher(tfSecondPoint.getText().trim()).matches()) {
JOptionPane.showMessageDialog(this, "第二个点输入内容不合法!", "错误", JOptionPane.ERROR_MESSAGE);
tfSecondPoint.requestFocus();
return false;
}
if (!p.matcher(tfThirdPoint.getText().trim()).matches()) {
JOptionPane.showMessageDialog(this, "第三个点输入内容不合法!", "错误", JOptionPane.ERROR_MESSAGE);
tfThirdPoint.requestFocus();
return false;
}

return true;
}}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式