Java GUI allows you to connect any circle with arrows and store

Source: Internet
Author: User

Java GUI allows you to connect any circle with arrows and store
Pass a small geometric program, the little girl is not talented, I remember thinking for a few days to get it out, O (cute _ cute) O design a GUI program, in the window, use the mouse to draw a circle (all circles are of the same size and can be stored), and drag the mouse to draw a line between the circles (the line must be a straight line of the two centers, it is a program that draws the boundary from one circle to another and has arrows.

Import javax. swing. JFrame; public class Draw {public static void main (String [] args) {JFrame frame = new JFrame ("circle"); frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); frame. add (new DrawPanel (); frame. pack (); frame. setVisible (true) ;}} import javax. swing. JPanel; import java. awt. *; import java. awt. event. *; import java. util. arrayList; public class DrawPanel extends JPanel {private final int r = 50; Po Int p, p1, p2; int l1 =-1, l2 =-1; int a, B, c, d, g, h; arrayList <Point> l = new ArrayList <Point> (); ArrayList <line> al = new ArrayList <line> (); // use ArrayList to store public DrawPanel () {DrawListener listen = new DrawListener (); addMouseListener (listen); addMouseMotionListener (listen); setBackground (Color. black); setPreferredSize (new Dimension (400,700);} public void paintComponent (Graphics page) {super. paintComponen T (page); page. setColor (Color. yellow); for (Point po: l) {if (po! = Null) page. drawOval (po. x-r, po. y-r, 2 * r, 2 * r);} for (line n: al) {// core algorithm, here I used 15 and 5 in a similar program to control the size of the arrows if (n! = Null) {a = (int) (r * (n. b. x-n.a.x)/Math. sqrt (n. a. x-n. B .x) * (n. a. x-n. B .x) + (n. a. y-n. B .y) * (n. a. y-n. B .y); B = (int) (r * (n. b. y-n.a.y)/Math. sqrt (n. a. x-n. B .x) * (n. a. x-n. B .x) + (n. a. y-n. B .y) * (n. a. y-n. B .y); c = (int) (r + 15) * (n. b. x-n.a.x)/Math. sqrt (n. a. x-n. B .x) * (n. a. x-n. B .x) + (n. a. y-n. B .y) * (n. a. y-n. B .y); d = (int) (r + 15) * (n. b. y-n.a.y)/Math. sqrt (n. a. x-n. B .x) * (n. a. x-n. B .x) + (n. a. y-n. B .y) * (n. a. y-n. B .y); g = 5 * B/r; h = 5 * a/r; int Xpoint [] = new int [3]; int Ypoint [] = new int [3]; Xpoint [0] = n. b. x-c-g; Xpoint [1] = n. b. x-c + g; Xpoint [2] = n. b. x-a; Ypoint [0] = n. b. y-d + h; Ypoint [1] = n. b. y-d-h; Ypoint [2] = n. b. y-B; page. drawLine (n. a. x + a, n. a. y + B, n. b. x-a, n. b. y-B); page. fillPolygon (Xpoint, Ypoint, 3) ;}} class line {private Point a, B; line (Point p1, Point p2) {a = p1; B = p2 ;}} private class DrawListener implements MouseListener, MouseMotionLi Stener {public void mousePressed (MouseEvent event) {l2 =-1; l1 =-1; p1 = event. getPoint (); for (int I = 0; I <l. size (); I ++) {if (Math. sqrt (l. get (I ). x-p1.x) * (l. get (I ). x-p1.x + (l. get (I ). y-p1.y) * (l. get (I ). y-p1.y) <r) l1 = I;} repaint ();} public void mouseClicked (MouseEvent event) {p = event. getPoint (); int f = 1; if (l. size () = 0) {repaint (); l. add (p);} else {for (int I = 0; I <l. size (); I ++) {if (Math. sqrt (l. get (I ). x-p.x) * (L. get (I ). x-p.x + (l. get (I ). y-p.y) * (l. get (I ). y-p.y) <2 * r) f = 0;} if (f = 1) {repaint (); l. add (p) ;}} public void mouseReleased (MouseEvent event) {p2 = event. getPoint (); for (int I = 0; I <l. size (); I ++) {if (Math. sqrt (l. get (I ). x-p2.x) * (l. get (I ). x-p2.x + (l. get (I ). y-p2.y) * (l. get (I ). y-p2.y) <r) l2 = I ;}repaint (); if (l1! =-1 & l2! =-1) {al. add (new line (l. get (l1), l. get (l2);} public void mouseEntered (MouseEvent event) {} public void mouseExited (MouseEvent event) {} public void mouseMoved (MouseEvent event) {} public void mouseDragged (MouseEvent event) {p2 = event. getPoint ();}}}

Click draw circle to draw a line with a direction between any two circles!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.