Animation von Robotern auf FactoryFloor - repaint()?

Status
Nicht offen für weitere Antworten.

rudi.schraml

Mitglied
Hallo Leute, 🙁

so ihr seit jetzt meine letzte Hoffnung. Nach 2 Wochen und ca. 40 h Bitte ich um eure Hilfe.
Ich soll auf einem fixen Floor Roboter animieren und dieses auf bestimmte positionen fahren lassen. Das Node und die Routenberechnung funktionieren wunderbar, aber ich schaffe es nicht das der Roboter auf meinem Factory Floor fährt.
Ich erzeuge zuerste ein JFrame und in die Content Pane zeichne ich dann zuerst den Floor und wenn ein Roboter instanziert wird soll er auch auf das JFrame: Bitte schaut euch die beiden CODES an - ihr würdet mir echt helfen wenn ihr mir sagen könntet wie ich das lösen kann:

Momentan fährt der Roboter zwar, aber der BlueFloor wird dann übermalt und er soll aber fix bleiben
!

zuerst der CODE der GUI:

Code:
public class GUI extends JFrame{
	
	public Container c;
	Graphics g;
		
	public GUI(){
		setResizable(false);
		setSize(660,450);
		setTitle("BlueFrame");
		setLocation(500,50);
		setBackground(Color.white);
		c = getContentPane();
		c.setVisible(true);
                                c.add(new BlueFloor());    
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	
	public void addRobot(Robots robot, Object[] array, int speed)
	{	robot.setXY(array);
		robot.setSpeed(speed);
		c.add(robot);
		c.setVisible(true);
		robot.startThread();

            public class BlueFloor extends JPanel
            {

		
	public void paintComponent(Graphics g)
	{
		path.NodePath path = new path.NodePath();
		path.getMachineNames();
		String[] machines;
		machines = path.getMachineNames();

		g.drawRect(20, 30, 600, 350);
		g.setFont(new Font("Arial", Font.BOLD, 17));
		g.drawString("G1", 128, 28);
		g.drawString("G2", 528, 28);
		g.drawString("G3", 28, 128);
		g.drawString("G4", 590, 128);
		g.drawString("G5", 28, 128+150);
		g.drawString("G6", 590, 128+150);
		g.drawString("G7", 128, 378);
		g.drawString("G8", 528, 128+250);...........

Hier die Klasse für die Roboter:

Code:
public class Robots extends JPanel implements Runnable {
	
	private static Integer countId = new Integer(0);
	private Integer id;
	private int xPos;
	private int yPos;
	private int finalxPos;
	private int finalyPos;
	private int speed = 50 ;
	private int xoldPos;
	private int yoldPos;
	private int addx = 2;
	private int addy = 3;
	private int counter;
	private Object[] move;
	public Thread thr;
	private boolean busy = false;
	Graphics r;
	String msa;
	
	//Konstruktor für Starposition
	public Robots(Object[] array, int speed)
	{ 	
		move = array;
		xPos=(Integer)move[0];
		yPos=(Integer)move[1];		
		id = countId++;
		if(speed != 0)
		{
			this.speed = 50-this.speed*5;
		}
		msa = "speed:"+speed+"mm/s";
	}
	public Robots()
	{
		id = countId++;
	}
	
	void changeColor(int x,int y,Graphics a){
		a.setColor(Color.blue);
		a.fillOval(x, y, 10, 10);}
	//sobald Roboter instanziert wird kann er auch gezeichnet werden
	
	public void paint (Graphics r)
	{	
	
		if(xPos>1&&yPos>1){
			if(xPos<599&&yPos<349){
		r.setColor(Color.blue);
		r.fillOval(xPos+20, yPos+30, 20, 20);
		r.setColor(Color.white);
		String a = "R";
		r.drawString(a, xPos+25, yPos+45);
		r.setColor(Color.blue);
		//r.drawString(msa, xPos+10, yPos+28);
		}
		}
		
	}
	
	//Threadsteuerung:
	//-------------------------------------------------------------------------------
	
	public void startThread() 
	{
      thr = new Thread(this);                 
      thr.start();                              
     }
 	public void run () 
 	{
 		busy = true;
 		xPos=(Integer)move[0];
		yPos=(Integer)move[1];	
		addx = 2;
		addy = 3;

		do{
			
			finalxPos = (Integer) move[addx];
		    finalyPos = (Integer) move[addy];
			
			do{                        
				try  {Thread.sleep (speed);} 
				catch  (InterruptedException e){}
    
			    if(finalxPos != xPos)
			    {
			    	if(xPos < finalxPos)
			    	{
			    		xPos++;
			    	}
			    	else
			    	{
			    		xPos--;
			    	}
			    }
			    else
			    {
			    	if(yPos < finalyPos)
			    	{
			    		yPos++;
			    	}
			    	else
			    	{
			    		yPos--;
			    	}
			    }
			    
			    revalidate();
			}
			while(xPos!=finalxPos || yPos!=finalyPos);
		
		addx = addx+2;
	    addy = addy+2;

		}while(addy<move.length);
		
		busy = false;
		thr.stop(); 
		lastMessage();                                 
	}



Das GUI wird in der Main Methode instanziert und wenn der Befehl für einen Roboter eigegeben wird, wird die GUI - Methode addRobot aufgerufen! Der Roboter fährt zwar, aber meine Factory wird übermalt.

Bitte um Hilfe!!

Sg Rudi






[/code]
 
Anstatt paint musst du paintComponent überschreiben.
Darin solltest du unbedingt super.paintComponent aufrufen.
Speicher dir keine Graphics Objekte.
 
Beim Roboter habe ich jetzt auf paintComponent und im Thread auf revalidate() gestellt!
Code:
public void paintComponent (Graphics r)
	{	super.paintComponent(r);
	
		if(xPos>1&&yPos>1){
			if(xPos<599&&yPos<349){
		r.setColor(Color.blue);
		r.fillOval(xPos+20, yPos+30, 20, 20);
		r.setColor(Color.white);
		String a = "R";
		r.drawString(a, xPos+25, yPos+45);
		r.setColor(Color.blue);
		//r.drawString(msa, xPos+10, yPos+28);
		}
		}
		
	}
	
	//Threadsteuerung:
	//-------------------------------------------------------------------------------
	
	public void startThread() 
	{
      thr = new Thread(this);                 
      thr.start();                              
     }
 	public void run () 
 	{
 		busy = true;
 		xPos=(Integer)move[0];
		yPos=(Integer)move[1];	
		addx = 2;
		addy = 3;

		do{
			
			finalxPos = (Integer) move[addx];
		    finalyPos = (Integer) move[addy];
			
			do{                        
				try  {Thread.sleep (speed);} 
				catch  (InterruptedException e){}
    
			    if(finalxPos != xPos)
			    {
			    	if(xPos < finalxPos)
			    	{
			    		xPos++;
			    	}
			    	else
			    	{
			    		xPos--;
			    	}
			    }
			    else
			    {
			    	if(yPos < finalyPos)
			    	{
			    		yPos++;
			    	}
			    	else
			    	{
			    		yPos--;
			    	}
			    }
			    
			    revalidate();
			}
			while(xPos!=finalxPos || yPos!=finalyPos);
		
		addx = addx+2;
	    addy = addy+2;

		}while(addy<move.length);
		
		busy = false;
		thr.stop(); 
		lastMessage();

Und im GUI habe ich auch auf paintComponent gestellt, jetzt würde er fahren aber ich sehe den Roboter erst wenn ich das Fenster auf und zumache !!

Code:
public class BlueFloor extends JPanel
{

		
	public void paintComponent(Graphics g)
	{
		path.NodePath path = new path.NodePath();
		path.getMachineNames();
		String[] machines;
		machines = path.getMachineNames();

		g.drawRect(20, 30, 600, 350);
HIILLLLFE
 
Code:
public class BlueFloor extends JPanel
{

      
   public void paintComponent(Graphics g)
   {
      path.NodePath path = new path.NodePath();
      path.getMachineNames();
      String[] machines;
      machines = path.getMachineNames();

      g.drawRect(20, 30, 600, 350);
Hier fehlt immer noch das paintComponent().
revalidate brauchst du nicht. nimm repaint().
validate brauchst du nur wenn du Komponenten entfernst/hinzufügst.
 
Wildcard hat gesagt.:
Code:
public class BlueFloor extends JPanel
{

      
   public void paintComponent(Graphics g)
   {
      path.NodePath path = new path.NodePath();
      path.getMachineNames();
      String[] machines;
      machines = path.getMachineNames();

      g.drawRect(20, 30, 600, 350);
Hier fehlt immer noch das paintComponent().
revalidate brauchst du nicht. nimm repaint().
validate brauchst du nur wenn du Komponenten entfernst/hinzufügst.

Wie meinst du das? Wo fehlt es? Tut mir leid aber ich sehs nicht - bin aber auch noch Anfänger!
 
Code:
public class BlueFloor extends JPanel
{

     
   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      path.NodePath path = new path.NodePath();
      path.getMachineNames();
      String[] machines;
      machines = path.getMachineNames();

      g.drawRect(20, 30, 600, 350);
 
GUI

Code:
public class GUI extends JFrame{
	
	public Container c;
	Graphics g;
		
	public GUI(){
		setResizable(false);
		setSize(660,450);
		setTitle("BlueFrame");
		setLocation(500,50);
		setBackground(Color.white);
		c = getContentPane();
		c.setVisible(true);
		c.add(new BlueFloor());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	
	public void addRobot(Robots robot, Object[] array, int speed)
	{	robot.setXY(array);
		robot.setSpeed(speed);
		c.add(robot);
		c.setVisible(true);
		robot.startThread();
		
		
	}
	
	public void paintMachine(Object[] array, String color)
	{	
		add(new machinePaint(array, color));
		setVisible(true);
	}
   
  public class machinePaint extends JPanel
  {
	private Object[] move;
	int x,y;
	private String colore;
	machinePaint(Object[] array, String colora){
		this.colore = colora;
		move = array;
		x=((Integer)move[move.length-2]);
		y=(Integer)move[move.length-1];}
			
	
	public void paintComponent(Graphics a){
		
		if(colore.equals("Blue")){
				a.setColor(Color.blue);
				}
			if(colore.equals("Red")){
				a.setColor(Color.red);}
			
			if(colore.equals("Green")){
					a.setColor(Color.green);}
			
			if(colore.equals("Yellow")){
				a.setColor(Color.yellow);}
			
			a.fillOval(x+15, y+20, 12, 12);
			}
		
}

	
		
public class BlueFloor extends JPanel
{

		
	public void paintComponent(Graphics g)
	{	super.paintComponent(g);
		path.NodePath path = new path.NodePath();
		path.getMachineNames();
		String[] machines;
		machines = path.getMachineNames();

		g.drawRect(20, 30, 600, 350);
		g.setFont(new Font("Arial", Font.BOLD, 17));
		g.drawString("G1", 128, 28);
		g.drawString("G2", 528, 28);
		g.drawString("G3", 28, 128);
		g.drawString("G4", 590, 128);
		g.drawString("G5", 28, 128+150);
		g.drawString("G6", 590, 128+150);
		g.drawString("G7", 128, 378);
		g.drawString("G8", 528, 128+250);
		
		
		
    	//pathways
		g.drawLine(20, 130, 620, 130 );
		g.drawLine(20, 280, 620, 280 );
		g.drawLine(120, 30, 120, 380 );
		g.drawLine(520, 30, 520, 380 );
		//pathways machines top
		
		g.setFont(new Font("Arial", Font.ITALIC, 12));
		g.drawString(machines[0], 212, 68);
		g.drawString(machines[1], 312, 68);
		g.drawString(machines[2], 412, 68);
		g.drawString(machines[3], 212, 202);
		g.drawString(machines[4], 312, 202);
		g.drawString(machines[5], 412, 202);
		g.drawString(machines[6], 212, 218);
		g.drawString(machines[7], 312, 218);
		g.drawString(machines[8], 412, 218);
		g.drawString(machines[9], 212, 352);
		g.drawString(machines[10], 312, 352);
		g.drawString(machines[11], 412, 352);
		
		g.drawLine(220, 80, 220, 180 );
		g.drawLine(320, 80, 320, 180 );
		g.drawLine(420, 80, 420, 180 );
		
		//pathways machines bottom
		g.drawLine(220, 230, 220, 330 );
		g.drawLine(320, 230, 320, 330 );
		g.drawLine(420, 230, 420, 330 );
		
		//Gates
		g.fillRect(13, 123, 15, 15);
		g.fillRect(13, 273, 15, 15);
		
		g.fillRect(613, 273, 15, 15);
		g.fillRect(613, 123, 15, 15);
		
		g.fillRect(113, 24, 15, 15);
		g.fillRect(513, 24, 15, 15);
		
		g.fillRect(113, 374, 15, 15);
		g.fillRect(513, 374, 15, 15);
		
		//Machines bottom
		
		g.fillOval(215, 220, 10, 10);
		g.fillOval(315, 220, 10, 10);
		g.fillOval(415, 220, 10, 10);
		
		g.fillOval(215, 330, 10, 10);
		g.fillOval(315, 330, 10, 10);
		g.fillOval(415, 330, 10, 10);
		
		//Machines top
		g.fillOval(215, 70, 10, 10);
		g.fillOval(315, 70, 10, 10);
		g.fillOval(415, 70, 10, 10);
		
		g.fillOval(215, 180, 10, 10);
		g.fillOval(315, 180, 10, 10);
		g.fillOval(415, 180, 10, 10);
    	   	
    	}
    	
   
	}

   }

Roboter:
Code:
public class Robots extends JPanel implements Runnable {
	
	private static Integer countId = new Integer(0);
	private Integer id;
	private int xPos;
	private int yPos;
	private int finalxPos;
	private int finalyPos;
	private int speed = 50 ;
	private int xoldPos;
	private int yoldPos;
	private int addx = 2;
	private int addy = 3;
	private int counter;
	private Object[] move;
	public Thread thr;
	private boolean busy = false;
	Graphics r;
	String msa;
	
	//Konstruktor für Starposition
	public Robots(Object[] array, int speed)
	{ 	
		move = array;
		xPos=(Integer)move[0];
		yPos=(Integer)move[1];		
		id = countId++;
		if(speed != 0)
		{
			this.speed = 50-this.speed*5;
		}
		msa = "speed:"+speed+"mm/s";
	}
	public Robots()
	{
		id = countId++;
	}
	
	void changeColor(int x,int y,Graphics a){
		a.setColor(Color.blue);
		a.fillOval(x, y, 10, 10);}
	//sobald Roboter instanziert wird kann er auch gezeichnet werden
	
	public void paintComponent (Graphics r)
	{	super.paintComponent(r);
	
		if(xPos>1&&yPos>1){
			if(xPos<599&&yPos<349){
		r.setColor(Color.blue);
		r.fillOval(xPos+20, yPos+30, 20, 20);
		r.setColor(Color.white);
		String a = "R";
		r.drawString(a, xPos+25, yPos+45);
		r.setColor(Color.blue);
		//r.drawString(msa, xPos+10, yPos+28);
		}
		}
		
	}
	
	//Threadsteuerung:
	//-------------------------------------------------------------------------------
	
	public void startThread() 
	{
      thr = new Thread(this);                 
      thr.start();                              
     }
 	public void run () 
 	{
 		busy = true;
 		xPos=(Integer)move[0];
		yPos=(Integer)move[1];	
		addx = 2;
		addy = 3;

		do{
			
			finalxPos = (Integer) move[addx];
		    finalyPos = (Integer) move[addy];
			
			do{                        
				try  {Thread.sleep (speed);} 
				catch  (InterruptedException e){}
    
			    if(finalxPos != xPos)
			    {
			    	if(xPos < finalxPos)
			    	{
			    		xPos++;
			    	}
			    	else
			    	{
			    		xPos--;
			    	}
			    }
			    else
			    {
			    	if(yPos < finalyPos)
			    	{
			    		yPos++;
			    	}
			    	else
			    	{
			    		yPos--;
			    	}
			    }
			    
			    repaint();
			}
			while(xPos!=finalxPos || yPos!=finalyPos);
		
		addx = addx+2;
	    addy = addy+2;

		}while(addy<move.length);
		
		busy = false;
		thr.stop(); 
		lastMessage();                                 
	}

hier wird das System instanziert: 
	[code]frame = new GUI();
	frame.setVisible(true);
            frame.addRobot(robot,xyway, speed);

naja und jetzt sieht man den Roboter gar nicht mehr. Vielleicht ist ja mein ganzer Lösungsansatz falsch. bitte schaus dir nochmal an - ich bin echt am Ende!!

Danke[/code]
 
Davon abgesehen dass das ziemlich schwer zu lesen ist da du dich nicht an die Konventionen hälst, ist das was du hier postest nicht kompilierfähig.
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben