我有以下類旋轉圖像,當我給它的度數,它必須旋轉,但它不工作,因爲我想要的。問題是這樣的,例如,如果我旋轉90度,rotationCrossing()內的變量「currentRotationAngle」在旋轉後逐漸設置爲90。發生這種情況是因爲我總是通過偏移量(10)增加該變量。旋轉不工作,因爲我想
下一次我想旋轉180度,因爲變量已經是90,它只旋轉另一個90(90 + 90 = 180)而不是整個180度。我試圖通過在旋轉結束後在「stop()」方法中將「currentRotationAngle」設置爲0來修復它,但它讓旋轉回到其起始位置。
這就是我想要的,當我給它度數時,我希望它完全旋轉到從最後一個旋轉位置開始的那個度數。請以下是班級。
public class CrossingPanel extends JPanel{
private static final long serialVersionUID = 1L;
// private data members
private Image crossingImage;
private int currentRotationAngle;
private int imageWidth;
private int imageHeight;
private AffineTransform affineTransform;
private boolean clockwise;
private static int ROTATE_ANGLE_OFFSET = 10;
private boolean finishRotation = false;
private int degreesToTurn;
private static int LENGTH = 75;
private static int RAIL_LENGTH = 130;
private int ARROWLENGTH = 4;
private static int TICKSIZE = 10;
private GeneralPath path;
private int xCoordinate;
private int yCoordinate;
private javax.swing.Timer timer;
private void initialize(){
//this.crossingImage = getImage("images/railCrossing.JPG");
this.crossingImage = Toolkit.getDefaultToolkit().getImage("images/crossingsHorizontal.JPG");
// MediaTracker mt = new MediaTracker(this);
// mt.addImage(this.crossingImage, 1);
this.path = new GeneralPath();
this.imageWidth = this.getCrossingImage().getWidth(this);
this.imageHeight = this.getCrossingImage().getHeight(this);
this.affineTransform = new AffineTransform();
this.setCurrentRotationAngle(0);
this.setDegreesToTurn(0);
timer = new javax.swing.Timer(20, new MoveListener());
// timer.start();
}
public CrossingPanel(int x, int y/*, BufferedImage img*/) {
this.setxCoordinate(x);
this.setyCoordinate(y);
this.setPreferredSize(new Dimension(50, 50));
this.setBackground(Color.red);
TitledBorder border = BorderFactory.createTitledBorder("image");
//this.setBorder(border);
this.setLayout(new FlowLayout());
this.initialize();
}
public GeneralPath getPath() {
return path;
}
public void setPath(GeneralPath path) {
this.path = path;
}
private void constructInterfaceComponents(){
}
private Shape createVerticalRail(){
//System.out.print("dddd: " + this.LENGTH + "\n");
this.getPath().moveTo(0, LENGTH);
this.getPath().lineTo(RAIL_LENGTH,75);
float cm = 72/2.54f;
float lengthCentimeter = RAIL_LENGTH;
for (float i = 260.0f, j = 340; i >= 0; i -= 15.0f, j += 20) {
float tick = i * cm;
// path.moveTo(340, i);
// path.lineTo(360, i);
}
this.getPath().closePath();
return this.getPath();
}
public void paintComponent(Graphics grp){
Rectangle rect = this.getBounds();
Graphics2D g2d = (Graphics2D)grp;
//set the background color to black
g2d.setColor(Color.BLACK);
// g2d.draw(this.createVerticalRail());
//set the translation to the mid of the component
this.getAffineTransform().setToTranslation(this.getxCoordinate(), this.getyCoordinate());
//rotate with the rotation point as the mid of the image
this.getAffineTransform().rotate(Math.toRadians(this.getCurrentRotationAngle()), this.getCrossingImage().getWidth(this) /2,
this.getCrossingImage().getHeight(this)/2);
//draw the image using the AffineTransform
g2d.drawImage(this.getCrossingImage(), this.getAffineTransform(), this);
}
public void rotateCrossing(int degrees){
// this condition is there to avoid division of zero
// and also there is no need to rotate 0 degrees
if(degrees == 0){
this.stop();
return;
}
currentRotationAngle += ROTATE_ANGLE_OFFSET;
if (currentRotationAngle % degrees == 0) {
timer.stop();
this.finishRotation = true;
}
repaint();
}
public int getDegreesToTurn() {
return degreesToTurn;
}
public void setDegreesToTurn(int degreesToTurn) {
this.degreesToTurn = degreesToTurn;
}
private class MoveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
rotateCrossing(degreesToTurn);
}
}
void start(int degreesToTurn) {
if (timer != null) {
timer.start();
}
this.setDegreesToTurn(degreesToTurn);
}
void stop() {
timer.stop();
this.setCurrentRotationAngle(0);
}
public Image getCrossingImage() {
return crossingImage;
}
public void setCrossingImage(Image crossingImage) {
this.crossingImage = crossingImage;
}
public int getCurrentRotationAngle() {
return currentRotationAngle;
}
public void setCurrentRotationAngle(int currentRotationAngle) {
this.currentRotationAngle = currentRotationAngle;
}
public int getImageWidth() {
return imageWidth;
}
public void setImageWidth(int imageWidth) {
this.imageWidth = imageWidth;
}
public int getImageHeight() {
return imageHeight;
}
public void setImageHeight(int imageHeight) {
this.imageHeight = imageHeight;
}
public AffineTransform getAffineTransform() {
return affineTransform;
}
public void setAffineTransform(AffineTransform affineTransform) {
this.affineTransform = affineTransform;
}
public boolean isClockwise() {
return clockwise;
}
public void setClockwise(boolean clockwise) {
this.clockwise = clockwise;
}
public int getxCoordinate() {
return xCoordinate;
}
public void setxCoordinate(int xCoordinate) {
this.xCoordinate = xCoordinate;
}
public int getyCoordinate() {
return yCoordinate;
}
public void setyCoordinate(int yCoordinate) {
this.yCoordinate = yCoordinate;
}
public javax.swing.Timer getTimer() {
return timer;
}
public void setTimer(javax.swing.Timer timer) {
this.timer = timer;
}
public boolean isFinishRotation() {
return finishRotation;
}
public void setFinishRotation(boolean finishRotation) {
this.finishRotation = finishRotation;
}
}
我想某人幫我修復它。
謝謝你們。
請我加入了「旋轉」的標籤,因爲我真正關心的不是處理旋轉圖像旋轉。我在問你是否可以再次添加「旋轉」標籤。謝謝 – kap 2010-08-12 21:42:24