2012-03-25 11 views
0

各imgに16種類の位置(回転22.5%)の2台の車があるプログラムを作成する必要があります。彼らはトラック(四角形)でレースする必要があります。問題は、車を別の場所から始めることができないことです。PaintComponentは2つの異なるJPanelsに座標合わせされます

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.ImageIcon; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class LogoAnimatorJPanel extends JPanel{ 

    protected ImageIcon carImages[]; 
    private final int numberOfImages = 16; 
    private int currentImage = 0; 
    private Timer spinTimer; 
    private String color; 
    private int gX = 375, gY = 500, sX = 375, sY = 550; 

    public LogoAnimatorJPanel(String carColor) { 
     carImages = new ImageIcon[numberOfImages]; 
     for(int i=0; i<carImages.length;i++){ 
      carImages[i] = new ImageIcon(getClass().getResource("Images/"+carColor+"Car/" + i +".PNG")); 
     } 
     this.setPreferredSize(new Dimension(50,50)); 
     color = carColor; 
    } 
    public void startAnimation(int ANIMATION_DELAY){ 
     if(spinTimer == null){ 
      currentImage = 0; 
      spinTimer = new Timer(ANIMATION_DELAY, new TimerHandler()); 
      spinTimer.start(); 
     } 
     else{ 
      if(!spinTimer.isRunning()){ 
       spinTimer.restart(); 
      } 
     } 
    } 
    public void myPaint(Graphics g){ 
     if("Green".equals(color)){ 
      carImages[ currentImage ].paintIcon(this, g, gX, gY); 
     } 
     if("Silver".equals(color)){ 
      carImages[ currentImage ].paintIcon(this, g, sX, sY);    
     } 
    } 
    @Override 
    public void paintComponent(Graphics g){   
     super.paintComponent(g); 
     g.setColor(Color.GREEN); 
     g.fillRect(150, 200, 550, 300); //grass 
     g.setColor(Color.BLACK); 
     g.drawRect(50, 100, 750, 500); // outer edge 
     g.drawRect(150, 200, 550, 300); // inner edge 
     g.setColor(Color.YELLOW); 
     g.drawRect(100, 150, 650, 400); // mid-lane marker 
     g.setColor(Color.WHITE); 
     g.drawLine(425, 500, 425, 600); // start line 
     myPaint(g); 
     carImages[ currentImage ].paintIcon(this, g, gX, gY); COMMENTED OUT (currently) 
     if (spinTimer.isRunning()) 
      currentImage = (currentImage+1) % carImages.length; 
    } 
    public void stopAnimation(){ 
     spinTimer.stop(); 
    } 
    private class TimerHandler implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent actionEvent){ 
      repaint(); 
     } 
    }} 



    import java.awt.Color; 
    import java.awt.Container; 
    import java.awt.FlowLayout; 
    import javax.swing.JFrame; 

    public class LogoAnimator { 
     public static void main(String[] args) { 
      JFrame window = new JFrame("Wacky Racing"); 

      LogoAnimatorJPanel carGreen = new LogoAnimatorJPanel("Green"); 
      window.add(carGreen); --->>> the order of this 
      LogoAnimatorJPanel carSilver = new LogoAnimatorJPanel("Silver"); 
      window.add(carSilver); ----->>>> and this seems to cause the problem 

      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      window.setLocation(300, 250); 
      window.setSize(850, 750); 
      window.setResizable(false); 
      window.setVisible(true); 

      carGreen.startAnimation(50); 
      carSilver.startAnimation(100); 
     }} 

私は2台の車に異なる座標があるように見えません。 すべてのソリューション?

答えて

2

あなたがハードコーディング、すべての車がその場所のため、これらの値を使用して車の座標が同じになるようにしている:

private int gX = 375, gY = 500, sX = 375, sY = 550; 

ので、私はあなたがこれを経験していることに驚いていませんよ問題。おそらく、車の位置とおそらくはそのImageIconを保持し、それぞれのCarオブジェクトを異なる場所に設定するCarクラスを作成したいと考えています。

関連する問題