【问题标题】:Point in Polygon or not test点在多边形中或不测试
【发布时间】:2018-11-01 11:56:39
【问题描述】:

我尝试编写一个 Java 应用程序,它会打印出鼠标给定点是在给定多边形内部还是外部。但我不知道如何使用公共布尔包含 Methode。有人可以帮助我吗?我的数组 arrx 和 arry 保存了 Poly 的坐标,但我怎么能说他必须检查我的 poly 中是否有 Point。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PiP extends JPanel implements MouseListener,MouseMotionListener  {
int x0,x1,y0,y1,i = 0,z = 1; 
int [][] pkt;
boolean zeichnen;
int []points = new int[100];
double y;
public static void main(String[] args) {
PiP p= new PiP();
}
public PiP() {
  JFrame fenster = new JFrame("Fenster");
  fenster.setBounds(0,0,600,600);
  fenster.setVisible(true);
  fenster.add(this); 
  fenster.addMouseListener(this);
  fenster.addMouseMotionListener(this);
  fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}  

public void mousePressed(MouseEvent e) {          //if mouse pressed
  pkt = new int[z][4]; 
  zeichnen = true;
  x1 = e.getX();
  y1 = e.getY();    
  pkt[i][0] = x1;
  pkt[i][1] = y1;
  System.out.println("x = "+pkt[i][0]+" und"+" y = "+pkt[i][1]);
  repaint();
  i++;
  z++;
}
public void Polygon(int arrx, int arry){
return arrx;
}
public boolean contains(int x1,int y1){     //here I tried to use contains, but I am 
return true;                                 //not sure how to take the variables from 
}                                           //the polygon to test if the Points are in
else {                                      //the Polygon
 return false;
}
}
public void mouseClicked(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseMoved(MouseEvent e) { }
public void mouseDragged(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void paint(Graphics g) {
    g.setColor(Color.RED);
int []arrx = {163,123,81,163,293,332,426,461,493,491,383,328,313,263};     //Poly x coordinates
int []arry = {143,219,359,433,478,523,448,401,306,238,219,205,168,158};   //Poly y coordinates
    g.drawPolygon(arrx,arry,arrx.length);
    if (zeichnen) {
     g.drawRect(x1,y1,10,10);
    g.fillRect(x1,y1,10,10);  
    } // end of if
  }
  }

【问题讨论】:

标签: java swing awt polygon point


【解决方案1】:
//here I tried to use contains, but I am 
//not sure how to take the variables from 
//the polygon to test if the Points are in
//the Polygon

arrxarry 数组值建立一个基于AWT 的Polygon,然后调用contains(x,y) 方法。

例如

public boolean containsPoint(int x1, int y1) {
    Polygon polygon = new Polygon(arrx, arry, arrx.length);
    return polygon.contains(x1,y1);
}

一边

在可以执行该测试之前(或者甚至在代码编译之前),需要对代码进行更改。例如,arrxarry 数组需要可访问(在其范围内)检查包含点的方法。

提示

  • 对于任何JComponent(如JPanel),正确覆盖绘画的方法是paintComponent(Graphics) 方法,而不是paint(Graphics) 方法。始终首先调用 super 方法,以清除之前的任何绘图。
  • 鼠标侦听器应添加到面板,而不是框架。
  • 与其设置框架的大小,不如在添加完所有组件后覆盖JPanelpack()框架的getPreferedSize()
  • 应在 EDT 上创建和更新 Swing (& AWT) GUI。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多