绘制一个等边三角形,当它移动时保持等边三角形

时间:2012-12-04 23:29:02

标签: java graphics mouseevent graphics2d mousemotionevent

我正在尝试创建一个程序,当按下鼠标时会在中心点绘制一个椭圆形的等边三角形,并且在拖动鼠标的同时,三角形将自行重绘,以便三角形保持等边距鼠标所在的位置(鼠标作为三角形的一个角)。到目前为止,我有以下代码。

public class EquilateralRubberBand extends JFrame {

/**
 *
 */
public EquilateralRubberBand() {
    super("Equilateral Triangle Rubber Band");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBackground(Color.white);
    setContentPane(new DrawTrianglePanel());
    setSize(400, 400); // you can change this size but don't make it HUGE!
    setVisible(true);
}

/**
 *
 */
private class DrawTrianglePanel extends JPanel implements MouseListener,
  MouseMotionListener {

    final float dash[] = { 5.0f };
    final BasicStroke dashed = new BasicStroke(1.0f,
        BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3.0f, dash, 0.0f);
    final BasicStroke solid = new BasicStroke(1.0f);
    final int sizeOfCenter = 5;
    final int sizeOfRectangles = 10;

    private String displayString;
    private boolean start;
    private Point centerPoint;
    private Point p1;
    private Point p2;
    private Point p3;



    /**
     */
    public DrawTrianglePanel() {
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    /**
     *
     * @param g the graphics context with which we paint into.
     */
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2;
        g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        if (start) {
            g2.setColor(Color.RED);
            g2.fillOval(centerPoint.x, centerPoint.y, sizeOfCenter, 
                    sizeOfCenter);
            g2.setColor(Color.BLUE);
            g2.drawRect(p1.x, p1.y, sizeOfRectangles, sizeOfRectangles);
            g2.drawRect(p2.x, p2.y, sizeOfRectangles, sizeOfRectangles);
            g2.drawRect(p3.x, p3.y, sizeOfRectangles, sizeOfRectangles);
            g2.drawString(displayString, 50, 25);
            g2.setStroke(dashed);
            g2.drawLine(p1.x, p1.y, p2.x, p2.y);
            g2.drawLine(p1.x, p1.y, p3.x, p3.y);
            g2.drawLine(p2.x, p2.y, p3.x, p3.y);
        }
    }

    public void mousePressed(MouseEvent e) {
        start = true;
        centerPoint = e.getPoint();
        doCoordinateCalculations(e.getPoint());
        displayString = "Pressed: " + centerPoint.x + ", " + centerPoint.y;
        repaint();
    }

    public void mouseReleased(MouseEvent e) {
        Point currentPoint = e.getPoint();
        displayString = "Released: " + currentPoint.x + ", " 
                + currentPoint.y;
        repaint();
    }

    public void mouseDragged(MouseEvent e) {
        Point currentPoint = e.getPoint();
        displayString = "Dragging: " + currentPoint.x + ", " 
                + currentPoint.y;
        doCoordinateCalculations(currentPoint);
        repaint();
    }

    public void mouseEntered(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
    public void mouseClicked(MouseEvent e) { }
    public void mouseMoved(MouseEvent e) { }

    public void doCoordinateCalculations(Point point) {
        double distanceFromCenter;
        double angleP1;
        double angleP2;
        double angleP3;
        double v2x;
        double v2y;
        double v3x;
        double v3y;
        double p2x;
        double p2y;
        double p3x;
        double p3y;
        p1 = point;
        distanceFromCenter = Math.sqrt((Math.pow((p1.x
                - centerPoint.x), 2) + Math.pow((p1.y 
                        - centerPoint.y), 2)));
        angleP1 = Math.atan2(p1.y - centerPoint.y,
                p1.x - centerPoint.x);
        angleP1 = ((2 * Math.PI)/3) - angleP1;

        angleP2 = angleP1 + ((2 * Math.PI)/3);
        v2x = distanceFromCenter * Math.cos(angleP2);
        v2y = distanceFromCenter * Math.sin(angleP2);
        p2x = v2x + centerPoint.x;
        p2y = v2y + centerPoint.y;
        p2 = new Point((int)p2x, (int)p2y);

        angleP3 = angleP1 - ((2 * Math.PI)/3);
        v3x = distanceFromCenter * Math.cos(angleP3);
        v3y = distanceFromCenter * Math.sin(angleP3);
        p3x = v3x + centerPoint.x;
        p3y = v3y + centerPoint.y;
        p3 = new Point((int)p3x, (int)p3y);
    }
}

/**
 *
 */
public static void main(String[] args) {
    new EquilateralRubberBand();
}

};

我已经有了绘制三角形的代码但是当我尝试移动它时,它不是保持等边而只是围绕中心旋转,它会改变形状并能够在屏幕上移动。我假设我的数学有问题,但我找不到问题。有人有什么想法吗?

0 个答案:

没有答案