无法使用鼠标事件删除Java中的形状

时间:2012-09-07 17:09:42

标签: java object methods mouseevent shape

我正在尝试在Eclipse中创建一个原创游戏,我在使用mouseClicked方法编写将删除GObject并更改其他属性的代码时遇到一些麻烦。

问题似乎是在mousePressed方法中无法识别私人即时变量。

有人可以告诉我这里出了什么问题吗?我已经花了一整天时间,我们将非常感谢你的帮助。

亲切的问候,

MEHUL

*/*
 * File: Linesv1.java
 * 07/08/2012
 * 
 */
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.MenuEvent;
public class Linesv1 extends GraphicsProgram {


    /** Width and height of application window in pixels */
    public static final int BACKGROUND_WIDTH = 400;
    public static final int BACKGROUND_HEIGHT = 600;
    /** Dimensions of game board (usually the same) */
    private static final int WIDTH = BACKGROUND_WIDTH;
    private static final int HEIGHT = BACKGROUND_HEIGHT;

    /** Dimensions of triangle*/
    private static final int LENGTH_OF_TRIANGLE_SIDE = 100;

    /** Dimensions of arc height*/
    private static final int ARC_HEIGHT = 100;

    /** Dimensions of radius of switches*/
    private static final int SWITCH_RADII = 5;


    // private instant variables

    private GObject switchA;

    private GOval switchB;

    private GObject triangle;


    private GObject bottomArc;


public void run() {
        addMouseListeners();
        setUpGame();
        }

public void setUpGame(){

    //add central triangle
    GPolygon triangle = new GPolygon (WIDTH/2,HEIGHT/2);
    triangle.addVertex(0,-LENGTH_OF_TRIANGLE_SIDE*2/3);
    triangle.addVertex(LENGTH_OF_TRIANGLE_SIDE/2,+LENGTH_OF_TRIANGLE_SIDE*1/3);
    triangle.addVertex(-LENGTH_OF_TRIANGLE_SIDE/2,+LENGTH_OF_TRIANGLE_SIDE*1/3);
    triangle.setFilled(true);
    triangle.setFillColor(Color.green);
    add(triangle);

    //add topArc
    GArc bottomArc = new  GArc (WIDTH/2-LENGTH_OF_TRIANGLE_SIDE/2, HEIGHT/2+LENGTH_OF_TRIANGLE_SIDE*1/3-ARC_HEIGHT/2, ARC_HEIGHT,ARC_HEIGHT,0,-180);
    bottomArc.setFilled(true);
    bottomArc.setFillColor(Color.green);
    add(bottomArc);

    //add switches to the bottom of the triangle
    GOval switchA = new GOval (WIDTH/2-LENGTH_OF_TRIANGLE_SIDE/2-SWITCH_RADII, HEIGHT/2+LENGTH_OF_TRIANGLE_SIDE*1/3-SWITCH_RADII,SWITCH_RADII*2,SWITCH_RADII*2);
    switchA.setFilled(true);
    switchA.setFillColor(Color.black);
    add(switchA);

    //add switches to the bottom of the triangle
    GOval switchB = new GOval (WIDTH/2+LENGTH_OF_TRIANGLE_SIDE/2-SWITCH_RADII, HEIGHT/2+LENGTH_OF_TRIANGLE_SIDE*1/3-SWITCH_RADII,SWITCH_RADII*2,SWITCH_RADII*2);
    switchB.setFilled(true);
    switchB.setFillColor(Color.black);
    add(switchB);
    }
public void mousePressed(MouseEvent e) {
    findObject(e.getX(),e.getY());
    GObject check = findObject(e.getX(),e.getY());
    if (check!=null){
        remove(triangle);
        switchA.setColor(Color.cyan);
    }
}
private GObject findObject(int a, int b){

    if(getElementAt(a,b) != null) {
        return getElementAt(a,b);
    } else {
        return null;
    }
}
}*

1 个答案:

答案 0 :(得分:0)

您要删除的三角形不是您添加的三角形。您在setupGame()中创建了一个名为triangle的本地GPolygon,并添加了该私有成员。

 private GObject triangle; // this is uninitialized

 public void setUpGame(){

   GPolygon triangle = new GPolygon (WIDTH/2,HEIGHT/2);  // this is hiding your member variable

   // THIS IS LOCAL COPY, NOT MEMBER VARIABLE
   add(triangle);

然后您尝试删除未初始化的成员变量,因此没有任何反应。您可能不希望单独的三角形本地副本。你可能想要

  public void setUpGame() {
    triangle = new GPolygon (WIDTH/2, HEIGHT/2);
    // ...
    add(triangle); // Now, you are adding the member
  }