package demo; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.geom.Path2D; import javax.swing.JFrame; import javax.swing.JPanel; public class PointTest extends JFrame{ int[] xPoints = { 71, 163, 163, 256}; int[] yPoints = { 89, 89, 254, 254}; // 创建多段线路径 Path2D path = new Path2D.Double(); Point point2 = new Point(117, 131); public PointTest() { path.moveTo(xPoints[0], yPoints[0]); for (int i = 1; i < xPoints.length; i++) { path.lineTo(xPoints[i], yPoints[i]); } System.out.println(path.contains(point2)); } public static void main(String[] args) { PointTest frame = new PointTest(); Canvas panel = new Canvas(); panel.path2d = frame.path; panel.point = frame.point; panel.point2 = frame.point2; frame.setContentPane(panel); frame.pack(); frame.setDefaultCloseOperation(3); frame.setLocationRelativeTo(null); frame.setVisible(true); } } class Canvas extends JPanel { Path2D path2d; Point point, point2; @Override public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.BLUE); g2d.draw(path2d); g2d.setColor(Color.RED); g2d.fillOval(point.x, point.y, 4, 4); g2d.fillOval(point2.x, point2.y, 4, 4); } @Override public Dimension getPreferredSize() { return new Dimension(300, 200); } } 如何让point2只在线path上时候才为true
时间: 2024-04-11 10:29:04 浏览: 185
很久以前用java.awt库写的小游戏的汇总.zip
要确定`point2`只在线`path`上时才为`true`,可以使用`Path2D`的`contains()`方法来判断。但是在当前的代码中,`path`只是一条简单的多段线路径,并不包含闭合的区域。因此,我们可以使用`Path2D`的`contains()`方法来判断点是否在路径上。
以下是修改后的代码,其中添加了判断点是否在线上的逻辑:
```java
package demo;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PointTest extends JFrame{
int[] xPoints = { 71, 163, 163, 256};
int[] yPoints = { 89, 89, 254, 254};
// 创建多段线路径
Path2D path = new Path2D.Double();
Point point2 = new Point(117, 131);
public PointTest() {
path.moveTo(xPoints[0], yPoints[0]);
for (int i = 1; i < xPoints.length; i++) {
path.lineTo(xPoints[i], yPoints[i]);
}
path.closePath();
System.out.println(isPointOnPath(point2));
}
public boolean isPointOnPath(Point point) {
return path.contains(point);
}
public static void main(String[] args) {
PointTest frame = new PointTest();
Canvas panel = new Canvas();
panel.path2d = frame.path;
panel.point = frame.point;
panel.point2 = frame.point2;
frame.setContentPane(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class Canvas extends JPanel {
Path2D path2d;
Point point, point2;
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLUE);
g2d.draw(path2d);
g2d.setColor(Color.RED);
g2d.fillOval(point.x, point.y, 4, 4);
g2d.fillOval(point2.x, point2.y, 4, 4);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
}
```
在`PointTest`类中添加了一个`isPointOnPath()`方法,用于判断点是否在线上。在`PointTest`构造函数中,调用`isPointOnPath()`方法判断`point2`是否在线上,并输出结果。
这样修改后,当`point2`在线上时,输出为`true`,不在线上时,输出为`false`。
阅读全文