/*
 P R O G R A M M B E S C H R E I B U N G
 Dies ist ein schlechtes Demo-Programm für eine Animation, weil in diesem 
 Programm keinnen Wert auf eine saubere Strukturierung gelegt wird.
 Also kein Mehrschichtenmodell bzw. kein MVC oder ähnliches.
*/

package wagenanimationohnemvc10;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;

public class MainWagenAnimationOhneMVC10 {
    public static void main(String[] args) {
        AnimationGUI animation;
        animation=new AnimationGUI();
    }
}

class AnimationGUI extends javax.swing.JFrame{
    
    public AnimationGUI() {
        Timer timer;        
        TimerActionListener tal;
        AnimationJPanel animationJPanel;        
        Container cont;        

        cont = getContentPane();                
        this.setSize(500,500);
        animationJPanel = new AnimationJPanel();
        cont.add(animationJPanel);
        this.setVisible(true);        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        // Wanze anlegen im Controller um Button-Feuer (der View) einzufangen      
        tal=new TimerActionListener(animationJPanel);
        // Wanze an Button "schneller" montieren, und dadurch Button (in der View) Feuer ermöglichen                
        timer = new Timer(2,tal);
        timer.start();
    }
}


class AnimationJPanel extends javax.swing.JPanel {
    private int x;

    public AnimationJPanel() {
        x=0;
    }
    
    public int  getX(){
        return x;
    }
    
    public void fahren(){
        if(x <= 200){
            x = x + 1;
        }
        else{
            x=0;
        }
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(x, 0, 30, 25);                
    }
}


class TimerActionListener implements ActionListener {
    private AnimationJPanel animationJPanel;    

    public TimerActionListener(AnimationJPanel animationJPanel){
        this.animationJPanel=animationJPanel;
    }
    
    public void actionPerformed(ActionEvent ae) {
        animationJPanel.fahren();
        animationJPanel.repaint();
    }
}
