gpt4 book ai didi

java - 斜面 java (带角的三角形)

转载 作者:行者123 更新时间:2023-12-02 05:10:13 25 4
gpt4 key购买 nike

我需要按照一定的角度创建一个斜面,上面有一个 block ,然后利用物理定律来看看 block 是否滑动。现在,我用 Path2D.Double 创建了我的 block ,但我找不到改变 block 角度的方法。角度是从应用程序框架中的微调器中选择的。我创建了 2 个类,一个是框架,另一个是面板,我在其中绘制我的 block 。这是我的路径的代码:

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);


Path2D.Double bloc = new Path2D.Double();

bloc.moveTo(10,0);

bloc.lineTo(getWidth(), getHeight());
bloc.lineTo(10, getHeight());
bloc.closePath();


g2d.setColor(Color.black);
g2d.fill(bloc);

}

我知道对于一行,我只需要这样做

 g2d.rotate(Math.toRadians(theSpinner Value));

但是对于 Path2D,我不知道如何。有什么建议吗?谢谢!

最佳答案

警告的话,我的三角函数是无望的,但我相信这(某些东西)符合您正在寻找的内容......

基本上,这假设我们知道高度和角度,然后使用 tan(angle) = opposite leg/adjacent leg 计算长度(相邻腿) ,更简单地说width = height * tan(angle)

Trig

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

final TestPane tp = new TestPane();
final JSlider slider = new JSlider(0, 90, 0);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
tp.setAngle(slider.getValue());
}
});
slider.setValue(45);

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tp);
frame.add(slider, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private int angle;

public TestPane() {
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();

int width = getWidth();
int height = getHeight();

Path2D triangle = new Path2D.Double();
triangle.moveTo(10, 10);
triangle.lineTo(10, height - 20);

double b = angle * Math.tan(Math.toRadians(angle));
triangle.lineTo(10 + b, height - 20);
triangle.closePath();

System.out.println("....");
System.out.println(angle);
System.out.println(b);

g2d.setColor(Color.BLACK);
g2d.draw(triangle);

g2d.dispose();
}

protected void setAngle(int value) {
angle = value;
repaint();
}

}

}

现在,如果你想计算高度(相反),你需要使用 height = width / tan(angle)相反...但你必须修改绘图代码;)

更改高度而不是宽度

        double b = width / Math.tan(Math.toRadians(angle));

Path2D triangle = new Path2D.Double();
triangle.moveTo(10, height - 20);
triangle.lineTo(10, height - 20 - b);

triangle.lineTo(width - 20, height - 20);
triangle.closePath();

关于java - 斜面 java (带角的三角形),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27393856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com