两个不同JPanel上的PaintComponent坐标



我需要制作一个程序,该程序有两辆车,每个img上有16个不同的位置(22.5%的旋转)。他们需要在跑道(长方形)上比赛。问题是我无法让汽车在不同的位置启动。

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);  
        }}

我似乎无法使这两辆车有不同的坐标。有什么解决方案吗?

您正在对汽车的坐标进行硬编码,使其与所有汽车的位置相同:

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

所以我对你遇到这个问题并不感到惊讶。也许你想创建一个Car类,它包含汽车的位置,也许还有它的ImageIcon,并将每个Car对象设置为具有不同的位置。

最新更新