PaintComponent在两个不同的JPanel上进行坐标

时间:2012-03-25 20:52:44

标签: java swing timer jpanel paintcomponent

我需要制作一个程序,每个img上有2个拥有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);  
        }}
我似乎无法让两辆车有不同的坐标。 任何解决方案?

1 个答案:

答案 0 :(得分:2)

您对汽车坐标的硬编码与所有汽车使用这些值的位置相同:

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

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