Java 将多个JComponents绘制到一个框架

Java 将多个JComponents绘制到一个框架,java,swing,user-interface,graphics,paintcomponent,Java,Swing,User Interface,Graphics,Paintcomponent,我试图在同一个窗口上绘制多个汽车对象,但它们似乎正在相互覆盖 这是我在Car类中重写的paintComponent方法 public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setColor(wheelColor); g2.fill(leftWheel); g2.fill(rightWheel); g2.setColor(bodyColor); g2

我试图在同一个窗口上绘制多个汽车对象,但它们似乎正在相互覆盖

这是我在Car类中重写的paintComponent方法

public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setColor(wheelColor);
    g2.fill(leftWheel);
    g2.fill(rightWheel);
    g2.setColor(bodyColor);
    g2.fill(body);
    g2.fill(cab);
}
在我的查看器类中:

JFrame f = new JFrame();
initializeFrame(f);

Car x = new Car(100, 100);
Car y = new Car(300, 300);

f.add(x);
f.add(y);
虽然坐标看起来不同,但只绘制了最后一辆车


有什么建议吗?谢谢

您要做的是使用
Car
对象的数据结构,并在
paintcomont
方法中循环它们。差不多

List<Car> cars = new ArrayList<>();
....
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    for (Car car : cars) {
        car.drawCar(g);
    }
}

请参阅更多示例和


更新

这里是一个简单的例子,使用我制作的一些“法拉利”,也使用了一些动画,但与上面的基本点相同

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class DrawCar extends JPanel{
    private static final int D_W = 400;
    private static final int D_H = 400;

    List<Car> cars;
    public DrawCar() {
        cars = new ArrayList<>();
        cars.add(new Car(100, 300));
        cars.add(new Car(200, 100));

        Timer timer = new Timer(50, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                for (Car car : cars) {
                    car.move();
                    repaint();
                }
            }
        });
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Car car : cars) {
            car.drawCar(g);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(D_W, D_H);
    }

    public class Car {
        private static final int INCREMENT = 5;
        int x, y;
        public Car(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public void drawCar(Graphics g) {
            g.setColor(Color.BLUE);
            g.fillRect(x, y, 100, 30);
            g.setColor(Color.BLACK); // body
            g.fillOval(x + 15, y + 20, 15, 15); // wheel
            g.fillOval(x + 60, y + 20, 15, 15); // wheel
            g.fillRect(x + 15, y - 20, 60, 20); // top
        }

        public void move() {
            if (x == D_W) {
                x = 0;
            } else {
                x += INCREMENT;
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new DrawCar());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
导入javax.swing.Timer;
公共级牵引车扩展JPanel{
专用静态最终整数D_W=400;
专用静态最终整数D_H=400;
列出车辆清单;
公共牵引车(){
cars=新阵列列表();
添加(新车(100300));
添加(新车(200100));
计时器计时器=新计时器(50,新ActionListener(){
已执行的公共无效操作(操作事件e){
用于(汽车:汽车){
汽车。移动();
重新油漆();
}
}
});
timer.start();
}
@凌驾
受保护组件(图形g){
超级组件(g);
用于(汽车:汽车){
牵引车(g);
}
}
@凌驾
公共维度getPreferredSize(){
返回新维度(D_W,D_H);
}
公车{
私有静态最终整数增量=5;
int x,y;
公共汽车(国际x、国际y){
这个.x=x;
这个。y=y;
}
公共真空牵引车(图g){
g、 setColor(Color.BLUE);
g、 fillRect(x,y,100,30);
g、 setColor(Color.BLACK);//正文
g、 圆角椭圆形(x+15,y+20,15,15);//车轮
g、 圆角椭圆形(x+60,y+20,15,15);//车轮
g、 fillRect(x+15,y-20,60,20);//顶部
}
公开作废动议(){
如果(x==D_W){
x=0;
}否则{
x+=增量;
}
}
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
JFrame=新JFrame();
框架。添加(新牵引车());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
}
但它们似乎正在相互覆盖

JFrame的默认布局管理器是BorderLayout。因此,默认情况下,您将所有组件添加到BorderLayout的中心。但是,您只能将一个零部件添加到中心,以便仅显示最后一辆车

将布局管理器更改为FlowLayout以查看差异


或者,看起来您试图在随机位置绘制汽车,在这种情况下,您应该使用“空”布局。然后您将负责设置每个汽车部件的尺寸/位置。

谢谢,这对我帮助很大!:)看我的一些动画更新:)好吧,你成功地在大约10分钟内完成了我在过去2个小时里试图制作的程序!哇!哈哈,谢谢:)
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class DrawCar extends JPanel{
    private static final int D_W = 400;
    private static final int D_H = 400;

    List<Car> cars;
    public DrawCar() {
        cars = new ArrayList<>();
        cars.add(new Car(100, 300));
        cars.add(new Car(200, 100));

        Timer timer = new Timer(50, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                for (Car car : cars) {
                    car.move();
                    repaint();
                }
            }
        });
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Car car : cars) {
            car.drawCar(g);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(D_W, D_H);
    }

    public class Car {
        private static final int INCREMENT = 5;
        int x, y;
        public Car(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public void drawCar(Graphics g) {
            g.setColor(Color.BLUE);
            g.fillRect(x, y, 100, 30);
            g.setColor(Color.BLACK); // body
            g.fillOval(x + 15, y + 20, 15, 15); // wheel
            g.fillOval(x + 60, y + 20, 15, 15); // wheel
            g.fillRect(x + 15, y - 20, 60, 20); // top
        }

        public void move() {
            if (x == D_W) {
                x = 0;
            } else {
                x += INCREMENT;
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new DrawCar());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}