Question

Je suis en train de faire une peinture en Java, avec des classes et de la hiérarchie. Mais ma région de peinture ne reçoit pas la couleur d'arrière-plan (défini comme blanc) et quand je clique sur il fait un écran d'impression dans la zone DPanel du dessin. Avec super.paintComponent (g) l'interface apparaît bien, mais je n'obtenir un point de chaque fois. Avec super.paintComponents (g) on ??imprime l'image dans la zone de DPanel.

des idées sur ce qui est arrivé?

public class MandaDesenhar extends JPanel
{
static int x;
static int y;

private static final long serialVersionUID = 1L;
int i = 0;

public void paintComponent(Graphics g)
{   
    super.paintComponents(g);

    if (Paint4Fun.lista.size() == 0)
        return;

    while (i<Paint4Fun.lista.size())
    {
        FormaPrimitiva forma = Paint4Fun.lista.get(i);
        forma.desenha(g);
        i++;
    }
}
Était-ce utile?

La solution

You should define i locally in your paintComponent method, not outside of it, and initialize it there to 0.

Otherwise you are always only painting the new elements of your list, not the older ones.

Edit: You can write your loop better as a for-loop:

for(int i = 0; i < Paint4Fun.lista.size(); i++) {
   FormaPrimitiva forma = Paint4Fun.lista.get(i);
   forma.desenha(g); 
}

or even more clearly:

for(FormaPrimitiva forma : Paint4Fun.lista) {
    forma.desenha(g);
}

Generally, always declare variables (like the i here) in the smallest possible scope (the method or even the loop, here).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top