使用 acm.graphics 时 mouseListener 的语法是啥

Posted

技术标签:

【中文标题】使用 acm.graphics 时 mouseListener 的语法是啥【英文标题】:What is the syntax for a mouseListener when using acm.graphics使用 acm.graphics 时 mouseListener 的语法是什么 【发布时间】:2014-04-11 11:24:34 【问题描述】:

我有一个“按钮”,它只是来自ACM Graphics Library 的GRect。它有一个继承的方法addMouseListener,我已经尝试过(但失败了)。我需要知道语法,以便当有人单击“按钮”时,我可以调用位于同一类中的另一个方法。

我想知道:

    ButtonName.addMouseListener还是addMouseListener(ButtonName) 我可以在创建按钮后立即输入上述行吗? 允许单击按钮调用某物的语法是什么? 问题 3 中的语法应该放在哪里?它有自己的类吗?

我的截断代码:

import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Shooter extends GraphicsProgram


…
public GRect newShotB;
…
public void run()
            
        test = new GLabel("start",printx+75,100);
        add(test);  
        setup();
        test.setLabel("runing");
        fire();
        test.setLabel("Waiting");
        newShotB.addMouseListener(new MouseAdapter()
        
                @Override
                public void mouseClicked(MouseEvent arg0) 
                    test.setLabel("clicked");
                    fire();
                
            );        
    

我的完整代码:

import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
 * Write a description of class Shooter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shooter extends GraphicsProgram

    // instance variables - replace the example below with your own
    private static final int width = 400;       // Width of Canvas
    private static final int height = 600;      // Height of canvas
    private static final int BALL_Radius = 30;  // radius of the Ball
    public int startxpos = 10;                  // starting xpos
    public int startypos =  400;                // starting ypos            
    public double xpos = startxpos;             // x position of gp
    public double ypos = startypos;             // y postion of gp
    public double t;                            // Time (for equations)
    public int score;                           // Score Aquired
    public int force;                           // Magnitude of Velocity
    public double angleR;                       // Angle in Radianss
    public double angleD;                       // Angle in Degrees
    public GLabel anglePrint;                   // Print out of angle
    public GLabel xposPrint;                    // Print out of x pos
    public GLabel yposPrint;                    // Print out of y pos
    public GLabel scorePrint;                   // Print out of score
    public GLabel timePrint;                    // Print out of t
    public GLabel nSBPrint;
    public GLabel test;
    public GOval gp;                            // The game Piece
    public GLine ground;                        // The Line that is the ground
    public GRect newShotB;                      // New Shot Button
    public GRect trusShotB;                     // New TrusShot Button

    public int printx = 25;                     // X Position of Glabels
    public int printy = 25;                     // Y position of Glabels


    /*
    public static void main(String[] ags)
    
        String[] sizeArgs =  "height= " +HEIGHT;
        new Shooter().start(sizeArgs);
    
    */
    /**
     * Constructor for objects of class Shooter
     */
    public Shooter()
       
    
    public void run()
            
        test = new GLabel("start",printx+75,100);
        add(test);  
        setup();
        test.setLabel("runing");
        fire();
        test.setLabel("Waiting");
        newShotB.addMouseListener(new MouseAdapter()
        
                @Override
                public void mouseClicked(MouseEvent arg0) 
                    test.setLabel("clicked");
                    fire();
                
            );        
    

    public void setup()
    
        timePrint = new GLabel ("Time:  "+String.valueOf(t),printx, printy);
        add(timePrint);

        anglePrint = new GLabel("Angle: "+String.valueOf(angleD), printx, printy+10);
        add(anglePrint);

        xposPrint = new GLabel ("x-pos: "+String.valueOf(xpos), printx, printy+20);
        add(xposPrint);

        yposPrint = new GLabel ("y-pos: "+String.valueOf(ypos), printx, printy+30);
        add(yposPrint);

        scorePrint = new GLabel("Score: "+String.valueOf(score), printx, printy+40);                
        add(scorePrint);

        ground = new GLine(10,startypos + BALL_Radius, 300, startypos + BALL_Radius);
        add(ground);

        GRect newShotB = new GRect (printx, 100, 50, 20);
        newShotB.setFilled(true);
        newShotB.setColor(Color.green);
        add(newShotB);

        nSBPrint = new GLabel ("New Shot",printx ,100);
        add(nSBPrint);

        gp = new GOval(startxpos,startypos,BALL_Radius, BALL_Radius);
        gp.setFilled(true);
        gp.setVisible(true);
        gp.setColor(Color.blue);
        add(gp);

        //trusShotB = new GRect (printx, 150, 50, 20);
        //trusShotB.setFilled(true);
        //trusShotB.setColor(Color.green);
        //add(trusShotB);



        test.setLabel("endStart");

    
        public void setpar()
    


    

    public void fire()
    
        test.setLabel("Firing");
        ypos = startypos;
        xpos = startxpos;        
        double vx;// = 10;
        double vy;// = 50;
        angleD = 45;
        force =50; 

        angleR = Math.toRadians(angleD);
        vx = (Math.cos(angleR)*force);
        vy = (Math.sin(angleR)*force); 
        t = 0;
        while (ypos<=startypos)
        
            xpos=startxpos+vx*t;
            ypos=startypos-(vy*t+.5*(-9.8)*t*t);
            gp.setLocation(xpos,ypos);
            waitTime();
            timePrint.setLabel(String.valueOf(t));
        
        test.setLabel("fired");
        gp.setVisible(false);

    
    public void trusShot()
    
        int sl= 20; //sidelength of the Truss
        int h = 100;// hight of truss
        int d = 200; //distance of truss from edgee
        GRoundRect t = new GRoundRect(sl, sl, h, d);
        t.setColor(Color.gray);

    
    public void waitTime()
    
        int waitTime = 100;
        try
        
            Thread.sleep(waitTime);
        
        catch(Exception e)
        
            //ignoring
        
        t = t+(waitTime/1000.0);
    
    public void changeColor()
    
        if(gp.getColor() == Color.red)
            gp.setColor(Color.blue);
        else if (gp.getColor() == Color.blue)    
        
            gp.setColor(Color.red);
        
    
    public void raiseAngle()
    
        angleD = angleD +1;
    
    public void lowerAngle()
    
        angleD = angleD -1;
    

【问题讨论】:

【参考方案1】:

根据addMouseListener() 的文档,您将MouseListener 的实现添加到GObject,特别是GRect。这是一个添加匿名侦听器的示例:

rect.addMouseListener(new MouseListener() 
    @Override
    public void mouseClicked(MouseEvent arg0) 
        // TODO Auto-generated method stub
    

    @Override
    public void mouseEntered(MouseEvent arg0) 
        // TODO Auto-generated method stub
    

    @Override
    public void mouseExited(MouseEvent arg0) 
        // TODO Auto-generated method stub
    

    @Override
    public void mousePressed(MouseEvent arg0) 
        // TODO Auto-generated method stub
    

    @Override
    public void mouseReleased(MouseEvent arg0) 
        // TODO Auto-generated method stub
    
); 

你也可以使用已经实现了MouseListener所有方法的MouseAdapter,即:

rect.addMouseListener(new MouseAdapter() 
    @Override
    public void mouseClicked(MouseEvent arg0) 
        // TODO Auto-generated method stub
    
);

或非匿名案例:

class CustomListener extends MouseAdapter 
    @Override
    public void mouseClicked(MouseEvent arg0) 
        // TODO Auto-generated method stub
    


CustomListener listener = new CustomListener();
rect.addMouseListener(listener);

这是一个简单的演示程序,可以在点击时改变 GRect 的颜色:

import acm.program.*;
import acm.util.RandomGenerator;
import acm.graphics.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TestRect extends GraphicsProgram 
    private static RandomGenerator rand = new RandomGenerator();

    public void run() 
        final GRect rect = new GRect(10, 10, 100, 100);
        rect.setFilled(true);
        rect.setColor(Color.RED);
        add(rect);

        rect.addMouseListener(new MouseAdapter() 
            @Override
            public void mouseClicked(MouseEvent arg0) 
                rect.setColor(rand.nextColor());
            
        );
    

【讨论】:

我真的迷路了,你所说的一切都很有帮助,但我仍然对去哪里感到困惑。例如: Line:rect.addMouseListener(new MouseListener() 去哪里?还有我在哪里输入鼠标点击时会发生什么? @user3395120 我刚刚添加了一个简短的演示程序,希望它可以帮助您澄清困惑。 @user3395120 我用RandomGenerator 简化了一点:) 另外,看看The ACM Java Libraries 教程,它有一些简单的例子。 非常感谢。它曾经工作过,但现在它不会再次工作。 @user3395120 不客气! 'it won't work again' 你指的是我刚刚在答案中发布的示例中的内容吗?【参考方案2】:

我认为您可以使用 GMouseEvent 来完成此任务以获取更多信息,您可以查看下一个链接:

http://www.matcmp.ncc.edu/~sherd/classdoc/cmp211/cs2Programs/doc/acm/graphics/GMouseEvent.html

【讨论】:

以上是关于使用 acm.graphics 时 mouseListener 的语法是啥的主要内容,如果未能解决你的问题,请参考以下文章

当 wmode=opaque 或 wmode=transparent 时如何检测 Event.MOUSE_LEAVE

几个mouse事件的特点

无法导入 pynput.mouse 控制器、按钮

从 AWTEvent 确定是不是为 Mouse_Exited 事件

通过硬件点击来区分mouse_event的点击次数

MOUSE_WHEEL 在触发之前是不是有最小增量值?