Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多边形中的点是否在测试中_Java_Swing_Awt_Polygon_Point - Fatal编程技术网

Java 多边形中的点是否在测试中

Java 多边形中的点是否在测试中,java,swing,awt,polygon,point,Java,Swing,Awt,Polygon,Point,我尝试编写一个Java应用程序,如果鼠标给定的点位于给定多边形的内部或外部,它就会打印出来。但是我不知道如何使用公共布尔包含方法。有人能帮我吗?我的数组arrx和arry保存了多边形的坐标,但是我怎么说他必须检查我的多边形中是否有点 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PiP extends JPanel implements MouseListener,MouseMoti

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

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
  }
  }
根据
arrx
arry
数组值建立AWT,然后调用该方法

例如

在一边 在执行测试之前(或者在代码编译之前),需要对代码进行更改。例如,
arrx
&
arry
数组需要能够被检查点是否包含的方法访问(在其范围内)

提示
  • 对于任何
    JComponent
    (如
    JPanel
    )绘制的正确替代方法是
    paintComponent(Graphics)
    方法,而不是
    paint(Graphics)
    方法。始终首先调用super方法,以清除以前的任何图形
  • 应该将鼠标侦听器添加到面板,而不是框架
  • 添加所有组件后,最好覆盖
    JPanel
    pack()
    框架的
    getPreferedSize()
    ,而不是设置框架的大小
  • 应在EDT上创建和更新Swing(&AWT)GUI
1)要更快地获得更好的帮助,请发布或。2) 使用缩进代码行和代码块的逻辑和一致形式。缩进的目的是使代码的流程更易于遵循!
//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
public boolean containsPoint(int x1, int y1) {
    Polygon polygon = new Polygon(arrx, arry, arrx.length);
    return polygon.contains(x1,y1);
}