public override void draw(Graphics graphics, GISView view) { Point screenpoint = view.ToScreenPoint(centroid); graphics.FillEllipse(new SolidBrush(Color.Red), new Rectangle(screenpoint.X - 3, screenpoint.Y - 3, 6, 6)); }
时间: 2024-02-14 12:35:17 浏览: 26
这是一个 C# 语言中的方法,它覆盖了父类的 draw 方法。该方法接受两个参数:一个 Graphics 对象和一个 GISView 对象。在方法中,首先通过 GISView 对象的 ToScreenPoint 方法将当前对象的质心坐标转为屏幕坐标,然后使用 Graphics 对象在屏幕上绘制一个填充颜色为红色的椭圆形,其坐标为以质心为中心,边长为 6 的正方形。该方法的作用是在屏幕上绘制当前对象的符号化表示。
相关问题
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
要确定`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`。
package work; import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; public class CyrusBeckAlgorithmApplet extends Applet { private static final long serialVersionUID = 1L; private Point2D.Double[] clipWindow; private Point2D.Double[][] lines; private double[][] vectors; private double[] p1, p2, D; @Override public void init() { clipWindow = new Point2D.Double[3]; clipWindow[0] = new Point2D.Double(200, 275); clipWindow[1] = new Point2D.Double(250.0 / 3, 100); clipWindow[2] = new Point2D.Double(950.0 / 3, 100); lines = new Point2D.Double[2][2]; lines[0][0] = new Point2D.Double(0, 120); lines[0][1] = new Point2D.Double(400, 120); lines[1][0] = new Point2D.Double(0, 180); lines[1][1] = new Point2D.Double(400, 180); vectors = new double[2][2]; D = new double[2]; } @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; // draw clip window g2d.setColor(Color.BLACK); g2d.draw(new Line2D.Double(clipWindow[0], clipWindow[1])); g2d.draw(new Line2D.Double(clipWindow[1], clipWindow[2])); g2d.draw(new Line2D.Double(clipWindow[2], clipWindow[0])); // draw lines for (int i = 0; i < lines.length; i++) { Point2D.Double p1 = lines[i][0]; Point2D.Double p2 = lines[i][1]; cyrusBeckClip(g2d, p1, p2); } } private void cyrusBeckClip(Graphics2D g2d, Point2D.Double p1, Point2D.Double p2) { double tE = 0, tL = 1; double dx = p2.x - p1.x; double dy = p2.y - p1.y; for (int i = 0; i < clipWindow.length; i++) { Point2D.Double P1 = clipWindow[i]; Point2D.Double P2 = clipWindow[(i + 1) % clipWindow.length]; double nx = -(P2.y - P1.y); double ny = P2.x - P1.x; double D = -nx * P1.x - ny * P1.y; double numerator = nx * p1.x + ny * p1.y + D; double denominator = -(nx * dx + ny * dy); if (denominator == 0) { if (numerator < 0) { return; } } else { double t = numerator / denominator; if (denominator < 0) { tE = Math.max(tE, t); } else { tL = Math.min(tL, t); } } } if (tE <= tL) { double x1 = p1.x + tE * dx; double y1 = p1.y + tE * dy; double x2 = p1.x + tL * dx; double y2 = p1.y + tL * dy; g2d.setColor(Color.BLUE); g2d.draw(new Line2D.Double(p1, new Point2D.Double(x1, y1))); g2d.setColor(Color.RED); g2d.draw(new Line2D.Double(new Point2D.Double(x1, y1), new Point2D.Double(x2, y2))); g2d.setColor(Color.BLUE); g2d.draw(new Line2D.Double(new Point2D.Double(x2, y2), p2)); } } } 将此代码改为 Java 应用程序运行
将代码中的 `extends Applet` 和 `public void init()` 方法删除,并将 `public void paint(Graphics g)` 方法改为 `public static void main(String[] args)` 方法,代码如下:
```java
package work;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CyrusBeckAlgorithmApp {
private static final Point2D.Double[] clipWindow = { new Point2D.Double(200, 275),
new Point2D.Double(250.0 / 3, 100), new Point2D.Double(950.0 / 3, 100) };
private static final Point2D.Double[][] lines = { { new Point2D.Double(0, 120), new Point2D.Double(400, 120) },
{ new Point2D.Double(0, 180), new Point2D.Double(400, 180) } };
public static void main(String[] args) {
JFrame frame = new JFrame("Cyrus Beck Algorithm");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// draw clip window
g2d.setColor(Color.BLACK);
g2d.draw(new Line2D.Double(clipWindow[0], clipWindow[1]));
g2d.draw(new Line2D.Double(clipWindow[1], clipWindow[2]));
g2d.draw(new Line2D.Double(clipWindow[2], clipWindow[0]));
// draw lines
for (int i = 0; i < lines.length; i++) {
Point2D.Double p1 = lines[i][0];
Point2D.Double p2 = lines[i][1];
cyrusBeckClip(g2d, p1, p2);
}
}
};
frame.add(panel);
frame.setSize(400, 300);
frame.setVisible(true);
}
private static void cyrusBeckClip(Graphics2D g2d, Point2D.Double p1, Point2D.Double p2) {
double tE = 0, tL = 1;
double dx = p2.x - p1.x;
double dy = p2.y - p1.y;
for (int i = 0; i < clipWindow.length; i++) {
Point2D.Double P1 = clipWindow[i];
Point2D.Double P2 = clipWindow[(i + 1) % clipWindow.length];
double nx = -(P2.y - P1.y);
double ny = P2.x - P1.x;
double D = -nx * P1.x - ny * P1.y;
double numerator = nx * p1.x + ny * p1.y + D;
double denominator = -(nx * dx + ny * dy);
if (denominator == 0) {
if (numerator < 0) {
return;
}
} else {
double t = numerator / denominator;
if (denominator < 0) {
tE = Math.max(tE, t);
} else {
tL = Math.min(tL, t);
}
}
}
if (tE <= tL) {
double x1 = p1.x + tE * dx;
double y1 = p1.y + tE * dy;
double x2 = p1.x + tL * dx;
double y2 = p1.y + tL * dy;
g2d.setColor(Color.BLUE);
g2d.draw(new Line2D.Double(p1, new Point2D.Double(x1, y1)));
g2d.setColor(Color.RED);
g2d.draw(new Line2D.Double(new Point2D.Double(x1, y1), new Point2D.Double(x2, y2)));
g2d.setColor(Color.BLUE);
g2d.draw(new Line2D.Double(new Point2D.Double(x2, y2), p2));
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)