【问题标题】:PaintComponent coordinates on two different JPanels两个不同 JPanel 上的 PaintComponent 坐标
【发布时间】:2012-03-25 20:52:44
【问题描述】:

我需要制作一个程序,其中有 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);  
        }}

我似乎无法让两辆车有不同的坐标。 有什么解决办法吗?

【问题讨论】:

    标签: java swing timer jpanel paintcomponent


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多