从其他类添加图像到JPanel

时间:2015-05-14 13:40:38

标签: java swing

我创建了一个环形交叉口模拟,直到现在我只是将图像添加到JPanel中的doDrawing(Graphics g)。但是我的程序与服务器进行通信,该服务器将数据包发送给产生的车辆和行人,所以我现在也希望从其他类中添加图像,而不是在doDrawing(Graphics g)中添加所有图像

使用Surface类(JPanel)内部的主类(Roundabout(JFrame))的代码。

public class Roundabout extends JFrame{

Track track=new Track();
TrafficLight trafficLight1=new TrafficLight(1);
TrafficLight trafficLight3=new TrafficLight(3);
TrafficLight trafficLight2=new TrafficLight(2);
TrafficLight trafficLight4=new TrafficLight(4);
TrafficLight trafficLight5=new TrafficLight(5);
Car car=new Car(412, 750); // south to west
Car car2=new Car(50,400); // west to south
Car car3=new Car(700,290); //east to south
Car car4=new Car(470,750);
Bus bus=new Bus();
Bicycle bicycle=new Bicycle();
Pedestrian pedestrian = new Pedestrian(571,750);
ArrayList<Car> cars = new ArrayList<>();

public static Map<Integer,TrafficLight> trafficLights = new HashMap<>();

byte[] array=new byte[]{0,2,1,1}; //test byte array

private Long startTime;
private long playTime = 4000;
private double i;

static TCPClient client;

Surface surface=new Surface();


class Surface extends JPanel {

private void doDrawing(Graphics g) {

    Dimension size = getSize();
    Insets insets = getInsets();

    int w = size.width - insets.left - insets.right;
    int h = size.height - insets.top - insets.bottom;

    /* Draw the track first */
    track.paint(g);

    /* Draw a car */
    //car.START_POS = new Point(412, 750);
    car.setCarLane(Lane.topLane);
    car.paint(g);
    cars.add(car); //add to list

    //car2.START_POS=new Point(50,400);
    car2.carRotation=180;
    car2.setCarLane(Lane.wsLane);
    car2.paint(g);
    cars.add(car2);

    car3.carRotation=360;
    car3.setCarLane(Lane.esLane);
    car3.paint(g);
    cars.add(car3);

    car4.setCarLane(Lane.seLane);
    car4.paint(g);
    cars.add(car4);

    /*Draw a bus*/
    bus.paint(g);

    /*Draw a bicycle */
    bicycle.setBicyclePath(Lane.bicyclePath);
    bicycle.paint(g);

    /*Draw a pedestrian */
    pedestrian.setPedestrianPath(Lane.pedesSePath);
    pedestrian.paint(g);

    /* Draw traffic light*/ 

    trafficLight1.setPosition(520, 333);
    trafficLight1.paint(g);

    trafficLight3.setPosition(100, 275);
    trafficLight3.paint(g);

    trafficLight2.setPosition(100, 400);
    trafficLight2.paint(g);

    trafficLight4.setPosition(355, 535);
    trafficLight4.paint(g);

    trafficLight5.setPosition(404, 535);
    trafficLight5.paint(g);


}

@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);
    doDrawing(g);
}
}

public Roundabout(){
    initUI();

    trafficLights.put(trafficLight1.id, trafficLight1);
    trafficLights.put(trafficLight2.id,trafficLight2);
    trafficLights.put(trafficLight3.id,trafficLight3);
    trafficLights.put(trafficLight4.id,trafficLight4);
    trafficLights.put(trafficLight5.id,trafficLight5);
}

private void initUI() {

    setTitle("Roundabout");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    add(surface);

    //add start
    this.addMouseListener(new MouseAdapter() {// empty implementation of all
        // MouseListener`s methods
        @Override
        public void mousePressed(MouseEvent e) {
            System.out.println(e.getX() + "," + e.getY());
        }
    });
    //end add

    //setSize(580, 550);
    setSize(1618,850);
    setLocationRelativeTo(null);

}


public static void main(String[] args) {


    //Swing thread
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {

            Roundabout roundabout=new Roundabout();
            roundabout.setVisible(true);    
            roundabout.moveCar();       
        }
    });             

}

Spawn课程的下方,我试图将行人添加到Surface JPanel,但在Surface面板上没有绘制任何内容,我确定{ {1}}被调用。如何使spawnPedestrian()工作,这是借鉴spawnPedestrian()面板?

Spawn.java

surface

0 个答案:

没有答案