2014-03-02 86 views
2

我想繪製多個汽車對象到同一個窗口,但它似乎是他們互相覆蓋。繪製多個JComponents到一個框架

這裏是Car類

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

在我的瀏覽器類我重寫的paintComponent方法:

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

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

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

雖然座標似乎是不同的,只有最後一輛車正在繪製。

有什麼建議嗎?謝謝

回答

12

你想要做的是使用Car對象的數據結構,並在paintComonent方法中遍歷它們。像

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

drawCar方法的東西會來自你的Car

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

查看更多例子herehereherehereherehere


UPDATE

下面是一個簡單的例子,用一些「法拉利」我颳起,也採用了一些動畫,但具有相同的基本點,我有以上。

enter image description here

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

謝謝,這對我幫助非常大! :) – MathMajor

+1

看到我的**更新**與一些動畫:) –

+1

那麼,你設法讓我的程序,我試圖在約10分鐘的最後2小時!哇!哈哈,並感謝:) – MathMajor

5

但現在看來,他們互相覆蓋。

JFrame的默認佈局管理器是一個BorderLayout。因此,默認情況下,您將所有組件添加到BorderLayout的中心。但是,您只能將一個組件添加到CENTER中,以便僅顯示最後一輛車。

將佈局管理器更改爲FlowLayout以查看差異。

或者,它看起來像是在隨機位置上繪製汽車,在這種情況下,您應該使用「空白」佈局。然後,您將負責設置每個汽車組件的大小/位置。