在不同类的同一Panel上添加图形

时间:2013-12-31 10:52:08

标签: java swing graphics jpanel paintcomponent

我正在开发一个模拟交通路口的项目。到目前为止,我做了地图,交通信号灯。现在我想在我的项目中添加一些动作,一些汽车。我面临的问题是我无法在不同类别的同一个Panel上添加图片。有人可以给我一个很好的教程,我可以学习如何移动多个图形(在我的情况下是汽车)。

主要课程:

import java.awt.Color;

import javax.swing.JFrame;

public class Main {

    public static void main(String[] args) {
        MyMap map = new MyMap(); 
        MyCar car = new MyCar();
        Thread x = new Thread(map);
        x.start();
        JFrame f = new JFrame("Broadway Intersection");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        map.setBackground(Color.white);
        f.add(map);
        f.add(car);
        f.setSize(1366, 738);
        f.setVisible(true); 
    }

}

地图类:

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class MyMap extends JPanel implements Runnable {
    Color color = Color.RED;
    Color color2 = Color.RED;
    Color color3 = Color.RED;
    Color color4 = Color.RED;
    int[] faza = new int[4];
    int k;
    int faza_curenta = 0;

    public MyMap() {

        faza[0] = 20;
        faza[1] = 20;
        faza[2] = 20;
        faza[3] = 20;
    }

    public void run() {
        color = Color.GREEN;
        while (true) {
            System.out.println("Faza = " + faza_curenta + " k= " + k);
            k++;
            if (k == faza[0]) {
                faza_curenta = 1;
                color = Color.RED;
                color2 = Color.GREEN;
            } else if (k == (faza[0] + faza[1])) {
                faza_curenta = 2;
                color = Color.RED;
                color2 = Color.RED;
                color3 = Color.GREEN;
            } else if (k == (faza[0] + faza[1] + faza[2])) {
                faza_curenta = 3;
                color = Color.RED;
                color3 = Color.RED;
                color4 = Color.GREEN;
            } else if (k == (faza[0] + faza[1] + faza[2] + faza[3])) {
                faza_curenta = 0;
                color = Color.GREEN;
                color4 = Color.RED;
                k = 0;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void paintComponent(final Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.GREEN);
        g.fillRect(0, 0, 500, 250); // stanga sus
        g.fillRect(900, 0, 500, 250); // dreapta sus
        g.fillRect(0, 500, 500, 250);// stanga jos
        g.fillRect(900, 500, 500, 250); // dreapta jos

        g.setColor(Color.GRAY);
        g.fillRect(500, 0, 400, 900);
        g.fillRect(0, 250, 500, 250);
        g.fillRect(900, 250, 500, 250);

        g.setColor(Color.WHITE);
        g.fillRect(695, 0, 5, 100);// linii verticale
        g.fillRect(695, 150, 5, 100);
        g.fillRect(695, 500, 5, 100);
        g.fillRect(695, 650, 5, 50);

        g.fillRect(0, 370, 50, 5);
        g.fillRect(100, 370, 100, 5); // linii orizontale
        g.fillRect(250, 370, 100, 5);
        g.fillRect(400, 370, 100, 5);
        g.fillRect(900, 370, 100, 5);
        g.fillRect(1050, 370, 100, 5);
        g.fillRect(1200, 370, 100, 5);

        g.setColor(Color.BLACK);
        g.fillRect(470, 220, 30, 30); // semafor Nord
        g.setColor(color);
        g.fillOval(475, 225, 20, 20); // semafor Nord

        g.setColor(Color.BLACK);
        g.fillRect(900, 220, 30, 30); // semafor Est
        g.setColor(color2);
        g.fillOval(905, 225, 20, 20); // semafor Nord

        g.setColor(Color.BLACK);
        g.fillRect(470, 500, 30, 30); // semafor Vest
        g.setColor(color4);
        g.fillOval(475, 505, 20, 20); // semafor Nord

        g.setColor(Color.BLACK);
        g.fillRect(900, 500, 30, 30); // semafor Sud
        g.setColor(color3);
        g.fillOval(905, 505, 20, 20); // semafor Nord
        repaint();
    }

}

最后是MyCar类:

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class MyCar extends JPanel  {
    int x; // X position
      int y; // Y position
      int xSpeed; // Speed in the X direction
      int ySpeed; // Speed in the Y direction

    public void paintComponent(final Graphics g) {
        super.paintComponent(g);

        g.setColor(Color.GREEN);
        g.fillRect(420, 200, 30, 30); 
        repaint();
    }   
}

1 个答案:

答案 0 :(得分:0)

您可以设置Panel.setLayout(null);并通过设置图片图标添加JButton,现在您可以按button.setLocation(x,y);

控制按钮(CAR)位置