先上图:
图中的D、C、F点都会随着P点的移动而移动,拖动下面的滑块即可移动P点。
是不是看起来像初中数学几何题?没错,就是因为题目做了出来,想看一下运动轨迹,才写了这个程序。
代码简单,有过一丢丢开发经验的人一看就懂:
// By woffee. May 7, 2019.
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
class Test {
public static void main(String args[]) {
TheWindow w = new TheWindow();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setSize(800, 600);
w.setVisible(true);
}
}
class Point{
public int x = 0;
public int y = 0;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
class DrawOval extends JPanel {
private int SIZE = 20;
private int WIDTH = 760;
private int HEIGHT = 500;
private Point ip = new Point(SIZE,SIZE + HEIGHT);
private int dd = 230; //SET START SIZE OF CIRCLE
private Point o,a,b,c,d,p,f;
public DrawOval()
{
this.o = new Point(0,0);
this.a = new Point(400,0);
this.b = new Point(200,(int)(200 * Math.sqrt(3)));
this.c = new Point(0,0);
this.d = new Point(0,0);
this.p = new Point(0,0);
this.f = new Point(0,0);
}
public void paintComponent(Graphics g) {
super.paintComponent(g); //CALL JPANEL PAINT.COMPONENT METHOD
// 已知等边三角形两点d和p,求另一点c。 参考:https://blog.csdn.net/lkj345/article/details/78375862
p.x = dd;
p.y = 0;
d.x = (int)( (b.x+p.x)/2 );
d.y = (int)( (b.y+p.y)/2 );
c.x = (int)( p.x + (d.x-p.x)/2.0 + (d.y-p.y)/2.0*Math.sqrt(3) );
c.y = (int)( p.y + (d.y-p.y)/2.0 - (d.x-p.x)/2.0*Math.sqrt(3) );
f.x = c.x;
f.y = 0;
g.setColor(Color.gray);
g.fillRect(SIZE,SIZE,WIDTH,HEIGHT);
g.setColor(Color.black);
myDrawLine(g, o, a);
myDrawLine(g, o, b);
myDrawLine(g, a, b);
myDrawLine(g, b, p);
myDrawLine(g, d, c);
myDrawLine(g, p, c);
myDrawLine(g, f, c);
g.setColor(Color.GREEN);
myDrawPoint(g,"D", d);
myDrawPoint(g,"P", p);
myDrawPoint(g,"C", c);
myDrawPoint(g,"F", f);
myDrawPoint(g,"O", o);
myDrawPoint(g,"A", a);
myDrawPoint(g,"B", b);
//g.setColor(Color.green);
//g.fillOval(10, 10, dd, dd); // DRAW CIRCLE
}
public void myDrawPoint(Graphics g, String s, Point aa){
g.drawString(s + ":"+aa.x+","+aa.y, ip.x + aa.x + 10, ip.y - aa.y -10);
}
public void myDrawLine(Graphics g, Point aa, Point bb)
{
g.drawLine(ip.x+aa.x, ip.y-aa.y, ip.x+bb.x, ip.y-bb.y);
}
// CREATE SETTER METHOD TO CHANGE VARIABLE AND REPAINT
public void setD(int newD) {
dd = (newD >= 0 ? newD : 230);
repaint();
}
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
}
class TheWindow extends JFrame {
private JSlider slider; // INSTANTIATE JSLIDER
private DrawOval myPanel; // INSTANTIATE DRAWOVAL CLASS
public TheWindow() {
super("The Slider Demo");
myPanel = new DrawOval();
myPanel.setBackground(Color.white);
slider = new JSlider(SwingConstants.HORIZONTAL, 0, 400, 230);
slider.setMajorTickSpacing(10);
slider.setPaintTicks(true);
// ADD LISTERNER TO SLIDER
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
myPanel.setD(slider.getValue()); //CALL SETTER METHOD
}
});
add(slider, BorderLayout.SOUTH); // ADD SLIDER + LAYOUT
add(myPanel, BorderLayout.CENTER); // ADD PANEL TO FRAME
}
}