Search code examples
javaswingjpanelpaintcomponent

How can i display the distance of a line drawing randomly?


I'm trying to draw to filled circles, centered at random locations, with a line connecting the circles. The distance between the to centers is displayed on the line and whenever your resize the frame, the circles are redisplayed in new random locations. I'm stuck in how to display the distance? Any help is appreciated and thx for advance. This's the code (what i managed to do):

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);    
    }
}

Solution

  • Try next code draws distance at line center

      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 );