Java AWT / swing “include” and “intersect” methods do not work properly
•
Java
I tried to teach myself some Java AWT and simple graphics, but it was difficult to use the contains and intersects methods
The problem is that it seems to detect a collision of several pixels from the mouse click position and the actual shape
GameDemo. java
package uk.co.mhayward.games.sand@R_863_2419@; import java.awt.Basicstroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Polygon; import java.awt.Shape; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JPanel; public class GameDemo extends JFrame { GamePanel gamePanel = new GamePanel(); public static void main(String[] args) { new GameDemo(); } public GameDemo() { super("click me"); this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); this.add(gamePanel); this.setSize(200,200); this.setVisible(true); this.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) { System.out.println(e.getPoint().toString()); if (gamePanel.shape.contains(e.getPoint())) { System.out.println("IN"); } else { System.out.println("out"); } } public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }); } public class GamePanel extends JPanel { Shape shape = new RegularPolygon(100,100,6,0); @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setstroke(new Basicstroke(1)); g2d.setPaint(Color.WHITE); g2d.fill(shape); g2d.setPaint(Color.BLACK); g2d.draw(shape); } } public static class RegularPolygon extends Polygon { private static final long serialVersionUID = 8828151557263250246L; /** * @param x * @param y * @param r * @param vertexCount */ public RegularPolygon(int x,int y,int r,int vertexCount) { this(x,y,r,vertexCount,0); } /** * @param x * @param y * @param r * @param vertexCount * @param startAngle * 360deg = PI */ public RegularPolygon(int x,int vertexCount,double startAngle) { super(getXCoordinates(x,startAngle),getYCoordinates(x,vertexCount); } protected static int[] getXCoordinates(int x,double startAngle) { int res[] = new int[vertexCount]; double addAngle = 2 * Math.PI / vertexCount; double angle = startAngle; for (int i = 0; i < vertexCount; i++) { res[i] = (int) Math.round(r * Math.cos(angle)) + x; angle += addAngle; } return res; } protected static int[] getYCoordinates(int x,double startAngle) { int res[] = new int[vertexCount]; double addAngle = 2 * Math.PI / vertexCount; double angle = startAngle; for (int i = 0; i < vertexCount; i++) { res[i] = (int) Math.round(r * Math.sin(angle)) + y; angle += addAngle; } return res; } } }
EDITS
04 / Jan / 12 – change the covering paint (g) to paintcomponent (g) – collision is still not detected correctly 05 / Jan / 12 – created an sscce to prove the problem more easily
Solution
Listen on the panel instead of JFrame The offset you see comes from the title bar
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码