i need to create an inclined plane according to a certain angle with a bloc on it and then use the physics laws to see if the bloc slides or not. For now, i created my bloc with a Path2D.Double, but i can't find a way to change the angle of the bloc. The angle is chosen from a spinner in the frame in the application.I created 2 class, one is the Frame, and the other is the Panel, where i draw my bloc. Here is the code of my path:
@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);
}
I know that for a line, i just have to do
g2d.rotate(Math.toRadians(theSpinner Value));
But for a Path2D, i don'T know how.
Any advice? Thank you !
解决方案
Word of warning, my trig is hopeless, but I believe this is (something) along the lines of what you are looking for...
Basically, this assumes we know the height and the angle, from which it then calculates the length (adjacent leg) using tan(angle) = opposite leg/adjacent leg, in plainer speak width = height * tan(angle)
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();
}
}
}
Now, if you want to calculate the height (opposite), you'd need to use height = width / tan(angle) instead...but you'd have to modify the drawing code ;)
To change the height instead of the width
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();