Java 在JFrame上绘制的点的总距离

Java 在JFrame上绘制的点的总距离,java,swing,Java,Swing,我试图打印总距离,就像你们看到的,我有page.drawString(“距离:+fmt.format(length),10,15);在我的代码中。但这只增加了从点1到点2的长度。我希望它继续添加,例如,我从点1到点2画了一条线,距离为30,然后再次从点1到点2画了一条线,距离为20,因此我的抽绳应该显示50 import javax.swing.JPanel; import java.awt.*; import java.awt.event.*; import java.text.Decimal

我试图打印总距离,就像你们看到的,我有page.drawString(“距离:+fmt.format(length),10,15);在我的代码中。但这只增加了从点1到点2的长度。我希望它继续添加,例如,我从点1到点2画了一条线,距离为30,然后再次从点1到点2画了一条线,距离为20,因此我的抽绳应该显示50

import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class RubberLinesPanel extends JPanel {

    private Point current = null, point2 = null;
    private double length;
    private DecimalFormat fmt;

    public RubberLinesPanel() {
        LineListener listener = new LineListener();
        addMouseListener(listener);
        addMouseMotionListener(listener);
        setBackground(Color.black);
        setPreferredSize(new Dimension(400, 200));
        fmt = new DecimalFormat("0.##");
    }

     public void paintComponent(Graphics page) {
        super.paintComponent(page);
        page.setColor(Color.yellow);
        if (current != null && point2 != null)
        page.drawLine(current.x, current.y, point2.x, point2.y);
        page.drawString("Distance: " + fmt.format(length), 10, 15);
    }

    private class LineListener implements MouseListener, MouseMotionListener {

        public void mousePressed(MouseEvent event) {
            current = event.getPoint();
        }
        //--------------------------------------------------------------
        // Gets the current position of the mouse as it is dragged and
        // redraws the line to create the rubberband effect.
        //--------------------------------------------------------------
        public void mouseDragged(MouseEvent event) {
            point2 = event.getPoint();
            length = Math.sqrt(Math.pow((current.x - point2.x), 2) +
            Math.pow((current.y - point2.y), 2));
            repaint();
        }
        //--------------------------------------------------------------
        // Provide empty definitions for unused event methods.
        //--------------------------------------------------------------
        public void mouseClicked(MouseEvent event) {}
        public void mouseReleased(MouseEvent event) {}
        public void mouseEntered(MouseEvent event) {}
        public void mouseExited(MouseEvent event) {}
        public void mouseMoved(MouseEvent event) {}
    }
}

解决方案是简单地给类a
双totalDistance
字段,将其初始化为0,然后在计算后立即将每个计算长度添加到totalDistance字段

public class RubberLinesPanel extends JPanel {
    private Point current = null, point2 = null;
    private double length;
    private double totalDistance = 0.0;  // ***** add this *****
    private DecimalFormat fmt;

    public RubberLinesPanel() {
        // .... etc .....
    }

    public void paintComponent(Graphics page) {
        super.paintComponent(page);
        page.setColor(Color.yellow);
        if (current != null && point2 != null)
            page.drawLine(current.x, current.y, point2.x, point2.y);
        page.drawString("Distance: " + fmt.format(length), 10, 15);

        // draw totalDistance here  // ************ draw it here
    }

    private class LineListener implements MouseListener, MouseMotionListener {

        public void mousePressed(MouseEvent event) {
            current = event.getPoint();
        }

        public void mouseDragged(MouseEvent event) {
            point2 = event.getPoint();
            length = Math.sqrt(Math.pow((current.x - point2.x), 2)
                    + Math.pow((current.y - point2.y), 2));
            totalDistance += length;   // ******* calculate it here
            repaint();
        }

或者我过度简化了你的问题,因为这个解决方案似乎太简单了?

解决方案是给你的类一个
双totalDistance
字段,将其初始化为0,然后在计算后立即将每个计算的长度添加到totalDistance字段

public class RubberLinesPanel extends JPanel {
    private Point current = null, point2 = null;
    private double length;
    private double totalDistance = 0.0;  // ***** add this *****
    private DecimalFormat fmt;

    public RubberLinesPanel() {
        // .... etc .....
    }

    public void paintComponent(Graphics page) {
        super.paintComponent(page);
        page.setColor(Color.yellow);
        if (current != null && point2 != null)
            page.drawLine(current.x, current.y, point2.x, point2.y);
        page.drawString("Distance: " + fmt.format(length), 10, 15);

        // draw totalDistance here  // ************ draw it here
    }

    private class LineListener implements MouseListener, MouseMotionListener {

        public void mousePressed(MouseEvent event) {
            current = event.getPoint();
        }

        public void mouseDragged(MouseEvent event) {
            point2 = event.getPoint();
            length = Math.sqrt(Math.pow((current.x - point2.x), 2)
                    + Math.pow((current.y - point2.y), 2));
            totalDistance += length;   // ******* calculate it here
            repaint();
        }

或者我过度简化了您的问题,因为此解决方案似乎过于简单?

请避免使用所有大写字母,因为这相当于在本网站和其他网站上叫喊。谢谢我编辑了你的问题标题并删除了所有的大写字母。我明白了,但这不是我问题的解决方案。不,但这可能有助于让更多的眼睛看到你的问题,增加你得到答案的几率。不客气。请避免使用所有大写字母,因为这相当于在本网站和其他网站上叫喊。谢谢我编辑了你的问题标题并删除了所有的大写字母。我明白了,但这不是我问题的解决方案。不,但这可能有助于让更多的眼睛看到你的问题,增加你得到答案的几率。不客气,谢谢你的帮助。这太简单了,我想说声谢谢你的帮助。这是如此简单,我是做循环笑