从另一个类在JPanel上绘制图像

时间:2015-05-17 09:34:46

标签: java swing

我正在进行环形模拟。到目前为止,图像都在doDrawing()中绘制,但程序与服务器连接,因此我想根据服务器发送的内容从另一个类中绘制(生成)图像。出于测试目的,我在byte类的mousePressed()上标识了Roundabout数组。这有效我可以看到println中的spawnPedestrian(),但行人没有在JPanel上绘制。

我需要在spawnPedestrian()更改哪些内容才能使其正常工作?

Roundabout.java - 主要类

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 ArrayList<TrafficLight> trafficLights = new ArrayList<>{trafficLight3}();

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());
            ByteProtocol proto=new ByteProtocol();
            proto.identifyByteArray(new byte []{0x01,0x02,0x01,0x00});

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

  }
}

调用spawnPedestrian()的ByteProtocol.java方法。

public void identifyByteArray(byte [] inputArray) {

    byte identifier=inputArray[0];


    switch(identifier){
        case vehicle: 
            System.out.println("Identifier: Vehicle");
            decodeVehicleArray(inputArray);
            break;
        case trafficLight:
            System.out.println("Traffic light");
            decodeTrafficLightArray(inputArray);
            break;
        case vehicleRegistration:
            System.out.println("Vehicle registration");
            decodeVehicleRegistrationArray(inputArray);
            break;
        default: System.out.println("Unable to identify the byte array.");
            break;
    }
}

public void decodeVehicleArray(byte [] vehicleArray){

    byte startPosition=vehicleArray[1];
    String startPositionString = directions.get(startPosition);
    System.out.println("Start position: "+startPositionString);

    byte endPosition=vehicleArray[2];
    String endPositionString = directions.get(endPosition);
    System.out.println("End position: "+endPositionString);

    byte vehicleType=vehicleArray[3];
    String vehicleTypeString=vehicles.get(vehicleType);
    System.out.println("Vehicle: "+vehicleTypeString);


    Spawn spawn=new Spawn();
    spawn.spawnPedestrian();


    }

Spawn.java

public class Spawn {

Roundabout roundabout = new Roundabout();

public void spawnPedestrian(){
    Pedestrian p = new Pedestrian(30,50);

    System.out.println("Spawn me ");

    p.setVisible(true);
    roundabout.surface.add(p);
    roundabout.surface.revalidate();
    roundabout.surface.repaint();
    roundabout.revalidate();
    roundabout.repaint();

   }
}

1 个答案:

答案 0 :(得分:0)

当你在Spawn类中编写Roundabout roundabout = new Roundabout();时,你正在创建一个Roundabout类型的新对象(最后是一个新的JFrame),你不想要一个这种类型的新对象,你想要一个已经创建的对象。

您需要做的是通过getter引用Spawn类中的JPanel曲面。所以将此方法添加到Roundabout类并从Spawn调用它以获取其上下文并能够在其上绘制:

public static JPanel getSurface()
{
    return surface;
}

然后在您的Spawn类中删除Roundabout roundabout = new Roundabout();,获取Surface JPanel对象并在其上绘图:

public class Spawn {

    public void spawnPedestrian(){
    Pedestrian p = new Pedestrian(30,50);

    System.out.println("Spawn me ");

    p.setVisible(true);
    Roundabout.getSurface.add(p);
    Roundabout.getSurface.revalidate();
    Roundabout.getSurface.repaint();
    Roundabout.getSurface.revalidate();
    Roundabout.getSurface.repaint();
   }
}

请注意,您需要将JPanel Surface对象设为静态。