java - Add image to JPanel from other class -


i'm creating roundabout simulation , until i'm adding images jpanel in dodrawing(graphics g). program communicates server sends packets spawn vehicles , pedestrians, want add images class instead of adding them in dodrawing(graphics g)

code of main class (roundabout(jframe)) inside surface class (jpanel).

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 west car car2=new car(50,400); // west south car car3=new car(700,290); //east 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 track first */     track.paint(g);      /* draw car */     //car.start_pos = new point(412, 750);     car.setcarlane(lane.toplane);     car.paint(g);     cars.add(car); //add 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 bus*/     bus.paint(g);      /*draw bicycle */     bicycle.setbicyclepath(lane.bicyclepath);     bicycle.paint(g);      /*draw 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         // 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();                }     });               } 

below spawn class i'm trying add pedestrian surface jpanel nothing drawn on surface panel, i'm sure spawnpedestrian() called. how can make spawnpedestrian() work, draws on surface panel?

spawn.java

    public class spawn {      roundabout roundabout = new roundabout();      public void spawnpedestrian(){         pedestrian p = new pedestrian(50,50);          system.out.println("spawn me ");           roundabout.surface.add(p);         roundabout.surface.revalidate();         roundabout.surface.repaint();         roundabout.revalidate();         roundabout.repaint();      } } 


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -