Grafik blinkt in JApplet

Ich weiß, dass dies schon oft gefragt wurde, aber jedes Mal, wenn ich den Fragenthread besuche und die gestellte Lösung ausführe, funktioniert es bei mir immer noch nicht.

Bevor ich Verwirrung stelle, ist das Problem, dass der Bildschirm, den ich zeichne, ständig blinkt. Dies liegt daran, dass ich jedes Mal, wenn ich den Bildschirm zeichne, den Bildschirm mit einem vollständig weiß gefüllten Rechteck lösche, die Dinge, die ich über das Rechteck zeichne wird blinken.

Hier ist mein Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
import javax.swing.Timer.*;

/**
  *
  * beschrijving
  *
  * @version 1.0 van 16-6-2016
  * @author 
  */

public class shadows3 extends JApplet implements ActionListener{
  // Begin variabelen
  int[] loc = new int[2];
  int[][] wall = new int[10][8];
  // Einde variabelen

  public void init() {
    Container cp = getContentPane();
    cp.setLayout(null);
    cp.setBounds(0, 0, 600, 600);
    // Begin componenten
    for (int a=0; a<10; a++) {
      int tempx = (int) (Math.random()*20)*30;
      int tempy = (int) (Math.random()*20)*30;
      wall[a][0] = tempx;
      wall[a][1] = tempy;
      wall[a][2] = tempx+30;
      wall[a][3] = tempy;
      wall[a][4] = tempx+30;
      wall[a][5] = tempy+30;
      wall[a][6] = tempx;
      wall[a][7] = tempy+30;
    } // end of for

    loc[0] = 300;
    loc[1] = 300;

    Timer step = new Timer(20, this);
    step.start();
    // Einde componenten

  } // end of init
  private int length(int x1, int y1, int x2, int y2) {
    double distance = Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
    return (int) distance;
  }

  // Begin eventmethoden

  public void paint (Graphics g){
    g.setColor(Color.WHITE);
    g.fillRect(0,0,600,600);

    int[] xpoints = new int[8];
    int[] ypoints = new int[8];
    int[] list = new int[3];
    for (int a=0; a<5; a++) {
      for (int b=0; b<4; b++) {
        if (length(wall[a][b*2],wall[a][b*2+1],loc[0],loc[1])==Math.max(Math.max(length(wall[a][0],wall[a][1],loc[0],loc[1]),
        length(wall[a][2],wall[a][3],loc[0],loc[1])),
        Math.max(length(wall[a][4],wall[a][5],loc[0],loc[1]),
        length(wall[a][6],wall[a][7],loc[0],loc[1])))) {
          int temp = b;
          for (int c=0; c<3; c++) {
            temp += 1;
            if (temp == 4) {
              temp = 0;
            } // end of if
            list[c] = temp;
          } // end of for
        } // end of if
      } // end of for
      xpoints[0] = wall[a][list[0]*2  ];
      ypoints[0] = wall[a][list[0]*2+1];

      xpoints[1] = wall[a][list[1]*2  ];
      ypoints[1] = wall[a][list[1]*2+1];

      xpoints[2] = wall[a][list[2]*2  ];
      ypoints[2] = wall[a][list[2]*2+1];

      xpoints[3] = wall[a][list[2]*2  ]+(wall[a][list[2]*2  ]-loc[0])*10000;
      ypoints[3] = wall[a][list[2]*2+1]+(wall[a][list[2]*2+1]-loc[1])*10000;

      xpoints[4] = wall[a][list[0]*2  ]+(wall[a][list[0]*2  ]-loc[0])*10000;
      ypoints[4] = wall[a][list[0]*2+1]+(wall[a][list[0]*2+1]-loc[1])*10000;

      g.setColor(Color.BLACK);
      g.fillPolygon(xpoints, ypoints, 5);
      //g.fillRect(wall[a][0],wall[a][1],30,30);
    } // end of for
  }

  //@Override
  public void actionPerformed(ActionEvent e){
    loc[0] += 4;
    loc[1] += 2;

    repaint();
  }
  // Einde eventmethoden

} // end of class shadows3

Wenn Sie sich fragen, was ich zu erstellen versuche, ist es eine Art Echtzeit-Schatten-Engine, es ist ziemlich langsam, aber es ist nur ein unterhaltsames Projekt. Das Blinken des Bildschirms ist jedoch für viele meiner Projekte ein echtes Problem.

Vielen Dank im Voraus

Antworten auf die Frage(4)

Ihre Antwort auf die Frage