0

画面をクリックすると円を描くプログラムを作成しました。好きなだけ円を描くことができるように動作させています。ドラッグしている円をハードコーディングすると、他の円ではなく1つの円をドラッグすることもできます。コードは次のとおりです。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;

public class DrawBall extends JPanel implements MouseListener, MouseMotionListener {
private List<Ball> balls;
private int x, y;
private int numBalls = 0;
boolean drag = false;

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

private static void createAndShowGUI() {
    JFrame frame = new JFrame("Draw Ball");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JComponent newContentPane = new DrawBall();
    newContentPane.setOpaque(true);
    frame.setContentPane(newContentPane);
    frame.setSize(300, 300);
    frame.setVisible(true);
}

public DrawBall() {
    super(new BorderLayout());
    balls = new ArrayList<Ball>(10);
    addMouseListener(this);
    addMouseMotionListener(this);
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHint(RederingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    for(Ball ball: balls) {
        ball.paint(g2d);
    }
    g2d.dispose();
}

public void mouseClicked(MouseEvent event) {
    x = (int) event.getPoint().getX();
    y = (int) event.getPoint().getY();
    Ball ball = new Ball(Color.red, numBalls, 30);
    ball.setX(x);
    ball.setY(y);
    balls.add(ball);
    numBalls = numBalls + 1;
    repaint();
}

public void mousePressed(MouseEvent event) {
    drag = true;
}

public void mouseReleased(MouseEvent event) {
    drag = false;
}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}

public void mouseDragged(MouseEvent event) {
    if(drag == true) {
        x = (int) event.getPoint().getX();
        y = (int) event.getPoint().getY();
        if(event.getSource() == balls.get(0)) {
            Ball ball = balls.get(0);
            ball.setX(x);
            ball.setY(y);
            balls.set(0,ball);
        }
        repaing();
    }
}

public void mouseMoved(MouseEvent event) {}

public class Ball {
    private Color color;
    private int x, y, diameter, id;

    public Ball(Color color, int id, int diameter) {
        setColor(color);
        setID(id);
        setDiameter(diameter);
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setID(int id) {
        this.id = id;
    }

    public void setDiameter(int diameter) {
        this.diameter = diameter;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getID() {
        return id;
    }

    public int getDiameter() {
        return diameter;
    }

    public Color getColor() {
        return color;
    }

    protected void paint(Graphics2D g2d) {
        int x = getX();
        int y = getY();
        g2d.setColor(getColor());
        g2d.fillOval(x, y, getDiameter(), getDiameter());
    }
}
}

私の質問はこれです:私のmouseDraggedメソッドで、どの円にカーソルを合わせているかを簡単に見分ける方法はありますか?私はevent.getSource()をいじってみましたが、少なくとも私が期待する方法では、私のサークルでは機能していないようです。助けてくれてありがとう。

4

3 に答える 3

2

Ballクラスを変更して、左上の点と直径ではなく、中心点と半径に基づいて円を作成します。

次に、クリックしたポイントと各ボールの中心点に距離の式を順番に適用することで、円の内側をクリックしたかどうかを計算できます。

距離が半径よりも小さい最初のボールは、クリックしたボールです。または、より洗練されたものにしたい場合は、半径よりも短い距離のボールがクリックしたボールです。

于 2012-11-07T18:23:35.370 に答える
0

この場合、イベントソースはボールではなくJPanelです。System.out.println(event.getSource());mouseDragged()メソッドにを追加して、自分の目で確かめてください。他の人が示唆しているように、マウスを押したポイントからサークルまでの距離を計算するだけです。中心/半径に基づいて計算すると、計算が簡単になります。

于 2012-11-07T18:36:51.323 に答える
0

シェイプのAPIを利用するようにBallクラスを変更し、を使用して円の座標を維持し、レンダリングすることができます。Ellipse2D

次に、を使用しEllipse2D#containsて、図形がクリックされたかどうかを判断できます。

あなたは最後に到達するか、あなたが探しているボールを見つけるまで、それぞれをチェックするボールのコレクションをループする必要があります

于 2012-11-07T19:47:43.237 に答える