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]
 

Wildcard

Top Contributor
Anstatt paint musst du paintComponent überschreiben.
Darin solltest du unbedingt super.paintComponent aufrufen.
Speicher dir keine Graphics Objekte.
 

rudi.schraml

Mitglied
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
 

Wildcard

Top Contributor
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.
 

rudi.schraml

Mitglied
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!
 

Wildcard

Top Contributor
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);
 

rudi.schraml

Mitglied
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]
 

Wildcard

Top Contributor
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.
Ähnliche Java Themen
  Titel Forum Antworten Datum
P Animation läuft nicht korrekt AWT, Swing, JavaFX & SWT 8
Ernesto95 JavaFX Return Value nach Beendigung einer Animation AWT, Swing, JavaFX & SWT 15
H Simple Animation mit Swing AWT, Swing, JavaFX & SWT 2
DeBoiJoshua 2D-Grafik Gif Animation will nicht laden AWT, Swing, JavaFX & SWT 1
S JavaFX WebView zeigt keine Animation AWT, Swing, JavaFX & SWT 5
E showAndWait is not allowed during animation or layout processing Memory FX AWT, Swing, JavaFX & SWT 2
A 2D-Grafik Ruckelfreie Animation AWT, Swing, JavaFX & SWT 20
L JavaFX Animation, erst zeichnen dann anzeigen AWT, Swing, JavaFX & SWT 4
L JavaFX Animation für Panel wechsel AWT, Swing, JavaFX & SWT 3
J Java FX Koordinaten NACH Animation setzen, wie? AWT, Swing, JavaFX & SWT 9
Pr0m3theus Animation nach Event AWT, Swing, JavaFX & SWT 6
F JavaFX Timeline Animation soll X- und Y-Position während Animation ändern AWT, Swing, JavaFX & SWT 2
javampir 2D-Grafik Effizienz bei animation AWT, Swing, JavaFX & SWT 0
C Pixel-Rendering/Animation Performance in BufferedImage AWT, Swing, JavaFX & SWT 1
wolfgang63 JavaFX Animation, Kreise im vorgegebem Takt durchs Fenster laufen lassen AWT, Swing, JavaFX & SWT 3
P Ansatz für 2D Animation gesucht AWT, Swing, JavaFX & SWT 2
Thallius HHübsche Kopier Animation? AWT, Swing, JavaFX & SWT 5
L JPanel kleine "Animation" AWT, Swing, JavaFX & SWT 7
E Animation läuft nicht mehr flüssig AWT, Swing, JavaFX & SWT 8
E Warum macht die einfache Animation einen kleinen Fehler? AWT, Swing, JavaFX & SWT 14
B 2D-Grafik Dynamisches Erstellen von Images und deren Animation AWT, Swing, JavaFX & SWT 4
R Swing Komponenten bleiben bei Animation unsichtbar AWT, Swing, JavaFX & SWT 7
J GIF Animation AWT, Swing, JavaFX & SWT 2
B Swing Thread+Animation AWT, Swing, JavaFX & SWT 7
R Swing Animation mit JLayeredPane? AWT, Swing, JavaFX & SWT 8
K 3D-Grafik Animation AWT, Swing, JavaFX & SWT 4
U Gif Animation mit JLabel AWT, Swing, JavaFX & SWT 3
P KeyListener + Animation AWT, Swing, JavaFX & SWT 2
D 2D-Grafik Animation flackert AWT, Swing, JavaFX & SWT 8
R Polygon-Animation mit Darstellungsfehlern AWT, Swing, JavaFX & SWT 5
StupidAttack Animation, JComponent AWT, Swing, JavaFX & SWT 3
S SWT Rudimentäre Bild Animation AWT, Swing, JavaFX & SWT 3
C Animation auf einem JPanel AWT, Swing, JavaFX & SWT 3
A paintComponent() - Animation AWT, Swing, JavaFX & SWT 2
S Ich brauche eine Idee: Animation mit teil eines Bildes AWT, Swing, JavaFX & SWT 16
H "Animation" AWT, Swing, JavaFX & SWT 2
S Animation korrekt darstellen AWT, Swing, JavaFX & SWT 8
Developer_X Nach Animation Button adden AWT, Swing, JavaFX & SWT 3
Developer_X Swing JPanel-THE ANIMATION AWT, Swing, JavaFX & SWT 3
T JFrame und Scale-Animation AWT, Swing, JavaFX & SWT 8
M Animation berechnen AWT, Swing, JavaFX & SWT 4
M Animation auf JPanel per Knopfdruck AWT, Swing, JavaFX & SWT 12
P Problem bei Animation AWT, Swing, JavaFX & SWT 2
K Animation auf GUI AWT, Swing, JavaFX & SWT 3
R ruckelfreie animation AWT, Swing, JavaFX & SWT 8
T Animation will nicht trotz Thread. AWT, Swing, JavaFX & SWT 14
S animation Flackern trotz doppelpufferung wieso? AWT, Swing, JavaFX & SWT 2
S Animation geht nicht AWT, Swing, JavaFX & SWT 3
m@nu Animation in GlassPane: Performanceeinbruch JFrame maximiert AWT, Swing, JavaFX & SWT 17
J Animation - Runnable AWT, Swing, JavaFX & SWT 3
O Animation in einem JPanel AWT, Swing, JavaFX & SWT 2
H Animation startet nicht/ Thread AWT, Swing, JavaFX & SWT 6
N Animation nach einem bestimmten Ereignis starten lassen? AWT, Swing, JavaFX & SWT 4
A Problem mit Animation AWT, Swing, JavaFX & SWT 4
M Animation mit Keylistener AWT, Swing, JavaFX & SWT 2

Ähnliche Java Themen


Oben