将多个JComponents绘制到一个框架 [英] Drawing Multiple JComponents to a Frame

查看:104
本文介绍了将多个JComponents绘制到一个框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我在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);
}

在我的Viewer类中:

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

车x =新车(100,100);
Car y =新车(300,300);

f.add(x);
f.add(y);

尽管坐标看起来不一样,但只有最后一辆车正在绘制。 b
$ b

有什么建议吗?谢谢

解决方案

你想要做的是使用 Car 对象,并在 paintComonent 方法中遍历它们。类似于

 列表< Car> cars = new ArrayList<>(); 
....
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g); (汽车:汽车)
{
car.drawCar(g);


drawCar 方法来自你的 Car class

  public class汽车{
int x,y;
public Car(int x,int y){
this.x = x;
this.y = y;
}

public void drawCar(Graphics g){
g.setColor(Color.BLACK);
//在这里做所有事情,就像在paintComponent方法中一样


$ c


$ b


查看更多示例此处此处此处此处此处和<这里有一个href =https://stackoverflow.com/a/22123304/2587435> 。






更新



下面是一个简单的例子,用一些Ferraris但与上面提到的基本点相同。

  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;
导入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;

列表< Car>汽车;
public DrawCar(){
cars = new ArrayList<>();
cars.add(新车(100,300));
cars.add(新车(200,100));

定时器计时器=新计时器(50,新ActionListener(){
公共无效actionPerformed(动作事件e){
(汽车:汽车){
汽车.move();
repaint();
}
}
});
timer.start();


@Override
protected void paintComponent(Graphics g){
super.paintComponent(g); (汽车:汽车)
{
car.drawCar(g);


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

公共类汽车{
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); //
}

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



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


I am trying to draw multiple car objects onto the same window but it appears that they are overwriting each other.

Here is my overridden paintComponent method in the Car class

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

And in my Viewer Class:

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

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

f.add(x);
f.add(y);

Although the coordinates seem to be different, only the last car is being drawn.

Any suggestions? Thanks

解决方案

What you want to do is use a data structure of Car objects and loop through them in the paintComonent method. Something like

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

The drawCar method would come from your Car class

public class Car {
    int x, y;
    public Car(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void drawCar(Graphics g) {
        g.setColor(Color.BLACK);
        // do everything here as you would in a paintComponent method
    }
}


See more examples here and here and here and here and here and here.


UPDATE

Here is a simple example use some "Ferraris" I whipped up, also using some animation, but with the same basic points I have above.

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

这篇关于将多个JComponents绘制到一个框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆