先上图:
    image.png
    图中的D、C、F点都会随着P点的移动而移动,拖动下面的滑块即可移动P点。

    是不是看起来像初中数学几何题?没错,就是因为题目做了出来,想看一下运动轨迹,才写了这个程序。

    代码简单,有过一丢丢开发经验的人一看就懂:

    1. // By woffee. May 7, 2019.
    2. import javax.swing.*;
    3. import java.awt.*;
    4. import javax.swing.event.*;
    5. class Test {
    6. public static void main(String args[]) {
    7. TheWindow w = new TheWindow();
    8. w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    9. w.setSize(800, 600);
    10. w.setVisible(true);
    11. }
    12. }
    13. class Point{
    14. public int x = 0;
    15. public int y = 0;
    16. public Point(int x, int y){
    17. this.x = x;
    18. this.y = y;
    19. }
    20. }
    21. class DrawOval extends JPanel {
    22. private int SIZE = 20;
    23. private int WIDTH = 760;
    24. private int HEIGHT = 500;
    25. private Point ip = new Point(SIZE,SIZE + HEIGHT);
    26. private int dd = 230; //SET START SIZE OF CIRCLE
    27. private Point o,a,b,c,d,p,f;
    28. public DrawOval()
    29. {
    30. this.o = new Point(0,0);
    31. this.a = new Point(400,0);
    32. this.b = new Point(200,(int)(200 * Math.sqrt(3)));
    33. this.c = new Point(0,0);
    34. this.d = new Point(0,0);
    35. this.p = new Point(0,0);
    36. this.f = new Point(0,0);
    37. }
    38. public void paintComponent(Graphics g) {
    39. super.paintComponent(g); //CALL JPANEL PAINT.COMPONENT METHOD
    40. // 已知等边三角形两点d和p,求另一点c。 参考:https://blog.csdn.net/lkj345/article/details/78375862
    41. p.x = dd;
    42. p.y = 0;
    43. d.x = (int)( (b.x+p.x)/2 );
    44. d.y = (int)( (b.y+p.y)/2 );
    45. c.x = (int)( p.x + (d.x-p.x)/2.0 + (d.y-p.y)/2.0*Math.sqrt(3) );
    46. c.y = (int)( p.y + (d.y-p.y)/2.0 - (d.x-p.x)/2.0*Math.sqrt(3) );
    47. f.x = c.x;
    48. f.y = 0;
    49. g.setColor(Color.gray);
    50. g.fillRect(SIZE,SIZE,WIDTH,HEIGHT);
    51. g.setColor(Color.black);
    52. myDrawLine(g, o, a);
    53. myDrawLine(g, o, b);
    54. myDrawLine(g, a, b);
    55. myDrawLine(g, b, p);
    56. myDrawLine(g, d, c);
    57. myDrawLine(g, p, c);
    58. myDrawLine(g, f, c);
    59. g.setColor(Color.GREEN);
    60. myDrawPoint(g,"D", d);
    61. myDrawPoint(g,"P", p);
    62. myDrawPoint(g,"C", c);
    63. myDrawPoint(g,"F", f);
    64. myDrawPoint(g,"O", o);
    65. myDrawPoint(g,"A", a);
    66. myDrawPoint(g,"B", b);
    67. //g.setColor(Color.green);
    68. //g.fillOval(10, 10, dd, dd); // DRAW CIRCLE
    69. }
    70. public void myDrawPoint(Graphics g, String s, Point aa){
    71. g.drawString(s + ":"+aa.x+","+aa.y, ip.x + aa.x + 10, ip.y - aa.y -10);
    72. }
    73. public void myDrawLine(Graphics g, Point aa, Point bb)
    74. {
    75. g.drawLine(ip.x+aa.x, ip.y-aa.y, ip.x+bb.x, ip.y-bb.y);
    76. }
    77. // CREATE SETTER METHOD TO CHANGE VARIABLE AND REPAINT
    78. public void setD(int newD) {
    79. dd = (newD >= 0 ? newD : 230);
    80. repaint();
    81. }
    82. public Dimension getPreferredSize() {
    83. return new Dimension(200, 200);
    84. }
    85. public Dimension getMinimumSize() {
    86. return getPreferredSize();
    87. }
    88. }
    89. class TheWindow extends JFrame {
    90. private JSlider slider; // INSTANTIATE JSLIDER
    91. private DrawOval myPanel; // INSTANTIATE DRAWOVAL CLASS
    92. public TheWindow() {
    93. super("The Slider Demo");
    94. myPanel = new DrawOval();
    95. myPanel.setBackground(Color.white);
    96. slider = new JSlider(SwingConstants.HORIZONTAL, 0, 400, 230);
    97. slider.setMajorTickSpacing(10);
    98. slider.setPaintTicks(true);
    99. // ADD LISTERNER TO SLIDER
    100. slider.addChangeListener(new ChangeListener() {
    101. public void stateChanged(ChangeEvent e) {
    102. myPanel.setD(slider.getValue()); //CALL SETTER METHOD
    103. }
    104. });
    105. add(slider, BorderLayout.SOUTH); // ADD SLIDER + LAYOUT
    106. add(myPanel, BorderLayout.CENTER); // ADD PANEL TO FRAME
    107. }
    108. }