我无法弄清楚如何在 paintComponent 中使用更改后的变量重新绘制
I can't figure out how to repaint with altered variables in paintComponent
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]
进行更改。
找到答案!
我将其添加到我的脚本并将变量更改为 private double endX/endY
,并添加了一个计时器对象。
public class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
calcEndX( clublength, angle );
calcEndY( clublength, angle );
}
}
public void calcEndX( int clublength, int angle ){
endX = startX + clublength * Math.sin(Math.toRadians(angle));
}
public void calcEndY( int clublength, int angle ){
endY = startY + clublength * Math.cos(Math.toRadians(angle));
}
public double getEndX(){
return endX;
}
public double getEndY(){
return endY;
}
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]
进行更改。
找到答案!
我将其添加到我的脚本并将变量更改为 private double endX/endY
,并添加了一个计时器对象。
public class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
calcEndX( clublength, angle );
calcEndY( clublength, angle );
}
}
public void calcEndX( int clublength, int angle ){
endX = startX + clublength * Math.sin(Math.toRadians(angle));
}
public void calcEndY( int clublength, int angle ){
endY = startY + clublength * Math.cos(Math.toRadians(angle));
}
public double getEndX(){
return endX;
}
public double getEndY(){
return endY;
}