如何随机显示线条图的距离?

时间:2013-11-08 10:43:52

标签: java swing jpanel paintcomponent

我正在尝试绘制实心圆圈,以随机位置为中心,用一条连接圆圈的线条。中心之间的距离显示在该行上,每当您调整框架大小时,圆圈将在新的随机位置重新显示。 我被困在如何显示距离? 任何帮助是赞赏和thx提前。 这是代码(我设法做的):

public class Test extends JFrame {

    public Test() {
        add(new LineConnectingTheTwoCircles());
    }

    // Panel class
    class LineConnectingTheTwoCircles extends JPanel {
        //Default constructor
        LineConnectingTheTwoCircles() {
        }
        /* Override paintComponent (getting access to the panel's Graphics
           class) */
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);    
            int radius = 15;
            // getting coordinates of circle 1
            int x1 = (int) (Math.random() * (getWidth()));
            int y1 = (int) (Math.random() * (getHeight()));
            // getting coordinates of circle 2
            int x2 = (int) (Math.random() * (getWidth()));
            int y2 = (int) (Math.random() * (getWidth()));
            // Setting color and drawing a filled circles (1 & 2)
            g.setColor(Color.BLUE);
            g.fillOval(x1 - radius, y1 - radius, 2 * radius, 2 * radius);
            g.drawString("1", x1 - 25, y1);
            g.setColor(Color.RED);
            g.fillOval(x2 - radius, y2 - radius, 2 * radius, 2 * radius);
            g.drawString("2", x2 - 25, y2);    
            connectingTheTwoCircles(g, x1, y1, x2, y2);
        }

        // Connecting the two circles from the center
        private void connectingTheTwoCircles(Graphics g, int x1, int y1,
                int x2, int y2) {
            //Distance between the circles centered
            double D = Math.sqrt((Math.pow((y2 - y1), 2))
                    + (Math.pow((x2 - x1), 2)));
            //Getting the coordinates for the line l
            int x11 = x1;
            int y11 = y1;
            int x21 = x2;
            int y21 = y2;
            g.setColor(Color.BLACK);
            g.drawLine(x11, y11, x21, y21);                         
        }

        public double getDistance(double x1, double y1, double x2, double y2) {
            return Math.sqrt((Math.pow((y2 - y1), 2))
                    + (Math.pow((x2 - x1), 2)));
        }
    }

    public static void main(String[] args) {
        // Frame declaration
        Test frame = new Test();    
        /*
         * Invoking some methods, to set a title on the title bar, to specifier
         * the size of the frame to centre it on the screen, to tell the program
         * to terminate when the frame is closed and finally to display it
         */
        frame.setTitle("This is a test");
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);    
    }
}

1 个答案:

答案 0 :(得分:1)

尝试下一个代码在行中心绘制距离

  double distance = getDistance(x11, y11, x21, y21);
  g.drawString(distance+" ",
                x11> x21 ? x21 + (x11-x21)/2 : x11 + (x21 - x11)/2 ,
                y11> y21 ? y21 + (y11-y21)/2 : y11 + (y21 - y11)/2 );