-1
int club_num = 0;
private int angle = 90;
private int startX = 72;
private int startY = 329;
private double endX = startX + clublength * Math.sin(Math.toRadians(angle));
private double endY = startY + clublength * Math.cos(Math.toRadians(angle));
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(startX, startY, (int)endX, (int)endY); //this is what i want to change
}
public class Keys extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_LEFT){
club_num--; //ultimately changes endX and endY
if (club_num < 0)
club_num = club[0].length-1; //ultimately changes endX and endY
repaint();
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT){
club_num++; //ultimately changes endX and endY
if (club_num > club[0].length-1)
club_num = 0; //ultimately changes endX and endY
repaint();
}
}
}
當按下後,一個按鈕和一個變量值改變稱爲repaint()
方法,我想行[g.drawLine(startX, startY, (int)endX, (int)endY)]
根據更新的變量參數來改變[endX and endY]
。我無法弄清楚如何與改變的變量的paintComponent重繪