0

I have a project, in which I should create a robot that moves between randomly created squares, till it sticks between two squares, or it reaches the end of the Canvas. For that I have a Super class Figur that has two subclass (Kreis) Circle and (Rechteck) Square, I have a Class Spielfeld(Gamefield) that has methods to populate an arraylist of figure, commands that user gives, of what robot should do, ...and at the end of this methods it calls the method zeichnen(draw) from Class Leinwand(Canvas).

public abstract class Figur{
///..some fields, constructions, methhods..
abstract public void zeichnen(Graphics g);
}
public class Rechteck extends Figur{
///..some fields, constructions, methhods..

   public void zeichnen(Graphics g){
       g.setColor(getFarbe());
       g.drawRect(getPosition().getX(),getPosition().getY(),getBreite(),getLaenge());
       g.fillRect(getPosition().getX(),getPosition().getY(),getBreite(),getLaenge());
    }

public class Kreis extends Figur{
      public void zeichnen(Graphics g){
       g.setColor(getFarbe());
       g.drawOval(getPosition().getX(),getPosition().getY(),getBreite(),getLaenge());
       g.fillOval(getPosition().getX(),getPosition().getY(),getBreite(),getLaenge());
    }

public class Spielfeld {
static ArrayList<Figur> myCharacters = new ArrayList<>();
//some methods that adds points(circles) and Squares to the arraylist MyCharacters
//some methods like movebetweensquares that calls method zeichnen in Leinwand
public class Leinwand{

      public void zeichnen(ArrayList<Figur> figur){
            loeschen();
            zeichenflaeche.repaintFiguren(figur);   
        }

      private void loeschen() {
        Color original = graphic.getColor();
        graphic.setColor(hintergrundfarbe);
        Dimension size = zeichenflaeche.getSize();
        graphic.fill(new Rectangle(0, 0, size.width, size.height));
        graphic.setColor(original);
      }


    private class Zeichenflaeche extends JPanel {
        private static final long serialVersionUID = 20060330L;


     

        public void paintComponent(Graphics g){
          
          super.paintComponent(g);
          
            for(int j=0; j<Spielfeld.get_ myCharacters ().size();j++){
              Figur s= Spielfeld.get_ myCharacters ().get(j);
              s.zeichnen(g);
           }
       }

       public void repaintFiguren(ArrayList<Figur> figuren{
          
           for( Figur f: Spielfeld.get_ myCharacters ()){
              for(int i=0; i<Spielfeld.get_ myCharacters ().size();i++){
              if(f instanceof Rechteck){
                               repaint(figuren.get(i).getPosition().getX(),
                               figuren.get(i).getPosition().getY(),
                               figuren.get(i).getBreite(),figuren.get(i).getLaenge()); }

              else if(f instanceof Kreis){
                     repaint(figuren.get(i).getPosition().getX(),
                             figuren.get(i).getPosition().getY(),
                             figuren.get(i).getBreite(),
                             figuren.get(i).getLaenge());}
               }
            }
        } 
   }
  

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Override paintComponent only. Do not override `paint(Graphics)`. See https://docs.oracle.com/javase/tutorial/uiswing/painting/. – VGR Jul 11 '21 at 15:30
  • @user16320675 Thank you. I didn't know that :) – Thariel wooden Jul 11 '21 at 17:22
  • I realized I need to write a code to clear my Canvas first before I paint the Components. I write the method Loeschen, but I get an error Nullpointerexception when running the program! @VGR – Thariel wooden Jul 11 '21 at 17:58
  • For a NullPointerException, see https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it. – VGR Jul 11 '21 at 19:08

0 Answers0