我用Java製作了一個JSlider,它改變了線條需要傾斜的角度。Java Swing,以特定角度畫一條線?
angle = new JSlider(SwingConstants.HORIZONTAL, 0, 180, 90);
angle.setSize(300, 50);
angle.setLocation(650, 60);
angle.setPaintTicks(true);
angle.setPaintTrack(true);
angle.setMinorTickSpacing(10);
angle.setMajorTickSpacing(30);
angle.setPaintLabels(true);
angle.addChangeListener(this);
thepanel.add(angle);
我想讓代碼畫出實現JSlider角度的那條線。
這裏是我的代碼:
public void paintComponent(Graphics g){
super.paintComponent(g);
int angle = intAngle;
Graphics2D graphics = (Graphics2D)g;
int startX = getWidth()/2;
int startY = getHeight()/2;
int length = 200;
int endX = startX + length * (int)Math.cos(Math.toRadians(angle));
int endY = startY + length * (int)Math.sin(Math.toRadians(angle));
graphics.drawLine(startX, startY, endX, endY);
}
什麼是背後的旋轉給出一個值線的數學?
http://en.wikipedia.org/wiki/Trigonometric_functions :) – Fildor
你在'JPanel'繪製的角度? –
我正在繪製公共類的角度JProject extends JPanel { – Eliscpe