簡體   English   中英

如何實現將有助於在Java中拖動圓圈的鼠標偵聽器?

[英]How to implement a mouse listener that will help drag a circle in java?

我正在嘗試找出如何完成以下任務

  1. 為您的Circle類編寫一個方法void move(Point p),該方法獲取一個Point並移動圓,使其圓心位於該點。
  2. 在CirclePanel構造函數中,創建一個CirclesListener對象,並使它偵聽鼠標事件和鼠標運動事件。
  3. 使CirclesListener類除了MouseListener接口之外,還實現MouseMotionListener接口。 這需要兩個步驟:在標頭中注意CirclesListener實現MouseMotionListener。 為兩個MouseMotionListener方法,mouseDragged和mouseMoved添加主體。 在mouseDragged中,只需將圓移動到MouseEvent的getPoint方法返回的點並重新繪制即可。 為mouseMoved提供一個空的主體。

我知道我需要做的事情(大部分情況下),我只是不知道該怎么做。 (編程新手)。 謝謝!

public class circlePanel extends JPanel {
private final int WIDTH = 600, HEIGHT = 400;
private Circle circle;

// -------------------------------------------------------------------
// Sets up this panel to listen for mouse events.
// -------------------------------------------------------------------
public circlePanel() {
    addMouseListener(new CirclesListener());
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
}

// -------------------------------------------------------------------
// Draws the current circle, if any.
// -------------------------------------------------------------------
public void paintComponent(Graphics page) {
    super.paintComponent(page);
    if (circle != null)
        circle.draw(page);
}

// ******************************************************************
// Represents the listener for mouse events.
// ******************************************************************
private class CirclesListener implements MouseListener, MouseMotionListener {
    // ---------------------------------------------------------------
    // Creates a new circle at the current location whenever the
    // mouse button is pressed and repaints.
    // ---------------------------------------------------------------
    public void mousePressed(MouseEvent event) {
        if (circle == null) {
            circle = new Circle(event.getPoint());
        } else if (circle.isInside(event.getPoint())) {
            circle = null;
        } else {
            circle.move(getMousePosition());
        }
        repaint();
    }

    // -----------------------------------------------------------------
    // Provide empty definitions for unused event methods.
    // -----------------------------------------------------------------
    public void mouseClicked(MouseEvent event) {
    }

    public void mouseReleased(MouseEvent event) {
    }

    public void mouseEntered(MouseEvent event) {
        setBackground(Color.white);
    }

    public void mouseExited(MouseEvent event) {
        setBackground(Color.blue);
    }
}
}

這是圈子課

public class Circles {
// ----------------------------------------------------------------
// Creates and displays the application frame.
// ----------------------------------------------------------------
public static void main(String[] args) {
    JFrame circlesFrame = new JFrame("Circles");
    circlesFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    circlesFrame.getContentPane().add(new circlePanel());
    circlesFrame.pack();
    circlesFrame.setVisible(true);
}
}

aannnddddd ...這是Circle類

public class Circle {
private int centerX, centerY;
private int radius;
private Color color;
static Random generator = new Random();

// ---------------------------------------------------------
// Creates a circle with center at point given, random radius and color
// -- radius 25..74
// -- color RGB value 0..16777215 (24-bit)
// ---------------------------------------------------------
public Circle(Point point) {
    radius = Math.abs(generator.nextInt()) % 50 + 25;
    color = new Color(Math.abs(generator.nextInt()) % 16777216);
    centerX = point.x;
    centerY = point.y;
}

// ---------------------------------------------------------
// Draws circle on the graphics object given
// ---------------------------------------------------------
public void draw(Graphics page) {
    page.setColor(color);
    page.fillOval(centerX - radius, centerY - radius, radius * 2,     
radius * 2);
}

public void move(Point p) {
    centerX = p.x;
    centerY = p.y;
}

public boolean isInside(Point p) {
    if (Math.sqrt(Math.pow(p.x - this.centerX, 2) + Math.pow(p.y - 
this.centerY, 2)) < this.radius) {
        return true;
    } else {
        return false;
    }
}
}

因此,基本上,根據您的代碼示例,您需要:

  • 實現MouseMotionListener的功能,其中將包括mouseDragged方法
  • 您需要將CirclesListener注冊到circlePanelJPanel#addMouseMotionListener
  • 調用mouseDragged ,您需要從MouseEvent獲取Point並調用Circle#moverepaint組件

如果遇到困難,最好的起點是如何編寫鼠標偵聽器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM