Move a point and go painting where Java passes

4

My problem is that I can move the circle but I can not make it repaint where it happens. I want him to leave color wherever he passes

Code:

import java.awt.Color;
import java.awt.Graphics;

/**
 *
 * @author isaac
 */
public class PanelGrafica extends javax.swing.JPanel implements Runnable{

    /**
     * Creates new form PanelGrafica
     */
    //Line Y
    int x=10;
    int y=10;
    int x1=10;
    int y1=200;


    //Line X
    int xx=10;
    int yy=150;
    int xx1=510;
    int yy1=150;

    //Binarios
    int bin,bin1,bin2,bin3,bin4,bin5,bin6,bin7;




    //si en 1
    int mov=5;
    int mov1=100;

    //si es 0
    int mov2=5;
    int mov3=145;


    Graphics e;
   Thread h;
    public PanelGrafica() {
        initComponents();
        h=new Thread(this);

    }

    //Binarios metodos
    public int binario(int bin)
    {
        this.bin=bin;
        return this.bin;

    }

    public int binario1(int bin1)
    {
        this.bin1=bin1;
        return this.bin1;

    }

    public int binario2(int bin1)
    {
        this.bin2=bin1;
        return this.bin2;

    }

    public int binario3(int bin1)
    {
        this.bin3=bin1;
        return this.bin3;

    }

    public int binario4(int bin1)
    {
        this.bin4=bin1;
        return this.bin4;

    }

    public int binario5(int bin1)
    {
        this.bin5=bin1;
        return this.bin5;

    }

    public int binario6(int bin1)
    {
        this.bin6=bin1;
        return this.bin6;

    }

    public int binario7(int bin1)
    {
        this.bin7=bin1;
        return this.bin7;

    }
    @Override
    public void paint(Graphics g)
    {
       g.setColor(getBackground());
       g.fillRect(0, 0, getWidth(), getHeight());
       g.setColor(Color.yellow);
       g.drawLine(x, y, x1, y1);
       g.setColor(Color.blue);
       g.drawLine(xx, yy, xx1, yy1);
       g.setColor(Color.red);
       g.setColor(Color.orange);
       g.drawLine(60, 150,60 ,140);
       g.drawLine(105, 150,105 ,140);
       g.drawLine(155, 150,155 ,140);
       g.drawLine(205, 150, 205, 140);
       g.drawLine(255, 150, 255, 140);


       if(bin == 0)
            {

             g.fillOval(mov2, mov3, 10, 10);

            }else 
           if(bin ==1)
           {

            g.fillOval(mov, mov1, 10, 10);
           }

    }


    public void Iniciar(){
    h.start();

    }

    public void Detener(){
    h.suspend();

    }

    public void Continuar(){

    h.resume();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setBackground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        

    @Override
    public void run() {
        try {

            while(true)
            {
            if(bin==0 )
            {
                while(mov2 <= 50 )
                {
                    Thread.sleep(100);
                    mov2+=5;
                    repaint();

                }
                //linea
            if(bin1==0)
            {
                while(mov2 < 100){
                Thread.sleep(100);
                mov2+=5;
                repaint();
                }
            }else if(bin1==1)
            {
                while(mov3 > 100)
                {
                    Thread.sleep(100);
                    mov3-=5;
                    repaint();

                }
                while(mov2<100)
                {
                    Thread.sleep(100);
                    mov2+=5;
                    repaint();
                }

            }

            if(bin2==0)
            {
                while(mov3 < 145)
                {
                    Thread.sleep(100);
                    mov3+=5;
                    repaint();

                }
                while(mov2 < 150)
                {
                    Thread.sleep(100);
                    mov2+=5;
                    repaint();

                }
            }else if(bin2==1)

            {
                while(mov3 > 100)
                {
                    Thread.sleep(100);
                    mov3-=5;
                    repaint();


                }
                while(mov2<150)
                {
                    Thread.sleep(100);
                    mov2+=5;
                    repaint();

                }

            }

            if(bin3==0)
            {

                while(mov3<145)
                {
                    Thread.sleep(100);
                    mov3+=5;
                    repaint();

                }
                while(mov2<200)
                {
                    Thread.sleep(100);
                    mov2+=5;
                    repaint();

                }
            }else if(bin3==1)
            {
                while(mov3>100)
                {

                    Thread.sleep(100);
                    mov3-=5;
                    repaint();
                }
                while(mov2<200)
                {
                    Thread.sleep(100);
                    mov2+=5;
                    repaint();

                }

            }

            if(bin4==0)
            {
                while(mov3<145)
                {
                    Thread.sleep(100);
                    mov3+=5;
                    repaint();
                }
                while(mov2<250)
                {
                    Thread.sleep(100);
                    mov2+=5;
                    repaint();

                }


            }else if(bin4==1)
            {
                while(mov3>100){
                Thread.sleep(100);
                    mov3-=5;
                    repaint();


                }
                while(mov2<250)
                {
                    Thread.sleep(100);
                    mov2+=5;
                    repaint();

                }

            }

            }

            //si inicia en 1
            if(bin==1)
            {
                while(mov<=50)
                {
                    Thread.sleep(100);
                    mov+=5;
                    repaint();

                }

                if(bin1==0)
                {
                    while(mov1<145)
                    {
                        Thread.sleep(100);
                        mov1+=5;
                        repaint();

                    }
                    while(mov<100)
                    {
                        Thread.sleep(100);
                        mov+=5;
                        repaint();

                    }
                }else if(bin1==1)
                {
                    while(mov<100)
                    {
                        Thread.sleep(100);
                        mov+=5;
                        repaint();

                    }
                }

                    if(bin2==0)
                    {
                        while(mov<100)
                    {
                        Thread.sleep(100);
                        mov+=5;
                        repaint();

                    }


                    }else if(bin2==1)
                    {
                        while(mov1>100)
                        {
                            Thread.sleep(100);
                            mov1-=5;
                            repaint();
                        }
                        while(mov<150)
                        {
                            Thread.sleep(100);
                            mov+=5;
                            repaint();
                        }

                    }

                    if(bin3==0)
                    {
                    while(mov1<145)
                    {
                        Thread.sleep(100);
                        mov1+=5;
                        repaint();
                    }
                    while(mov<250)
                    {
                        Thread.sleep(100);
                        mov+=5;
                        repaint();

                    }

                    }else if(bin3==1)
                    {
                        while(mov<250)
                        {
                            Thread.sleep(100);
                            mov+=5;
                            repaint();

                        }

                    }
                    if(bin4==0)
                    {
                        while(mov1<145)
                        {
                            Thread.sleep(100);
                            mov1+=5;
                            repaint();

                        }
                        while(mov<300)
                        {
                            Thread.sleep(100);
                            mov+=5;
                            repaint();

                        }

                    }else if(bin4==1)
                    {
                        while(mov<300)
                        {
                            Thread.sleep(100);
                            mov+=5;
                            repaint();

                        }


                    }


            }


            }



        } catch (Exception e) {
        }
    }



    // Variables declaration - do not modify                     
    // End of variables declaration                   
}
    
asked by Isaac Barragan 19.11.2016 в 00:06
source

2 answers

3

To leave the trail do not clean the screen every tick. Remove these lines from your paint method:

g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
    
answered by 19.11.2016 в 00:45
0

you must remove all your repaint ();

and then you must create a main method with an infinite while (true)

public void iniciar() {
        while (estado) {
            moverPelota();
            demorar(5);
        }
    }

public void moverPelota(){
//tus movimientos
}

public void demorar(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        repaint();
    }

Now to let your ball trail you have to remove the repaint ();

but if you want it to be a 3 px trace for example you should go copying the last positions of your ball into an array and painting it

editing ...

explaining step by step first I create the point to draw primitives such as line, circle, square

public class Point {

    public static void putPixel(Graphics g2, int x, int y,int color) {
        Graphics2D g = (Graphics2D)g2;
        g.setColor(new Color(color));
        g.drawLine(x, y, x, y);
    }
}

Once this is done, we start drawing a square

public class Cuadrado {

    public int w, h;
    public int x;
    public int y;
    public int color;

    public void dibujarCuadrado(Graphics g, int posX, int posY, int base, int altura,int color) {
        Line.drawline(g, posX, posY, base + posX, posY, color);
        Line.drawline(g, base + posX, posY, base + posX, altura + posY,color);
        Line.drawline(g, base + posX, altura + posY, posX, altura + posY,color);
        Line.drawline(g, posX, altura + posY, posX, posY, color);
        this.w=base;
        this.h=altura;
        this.x=posX;
        this.y=posY;
        this.color=color;
    }

    public void setPosX(int posX) {
        this.x = posX;
    }

    public void setPosY(int posY) {
        this.y = posY;
    }
     public void setBase(int base) {
        this.w = base;
    }

    public void setAltura(int altura) {
        this.h = altura;
    }

    public int getPostX() {
        return x;
    }

    public int getPostY() {
        return y;
    }

    public int getBase() {
        return this.w;
    }

    public int getAltura() {
        return this.h;
    }
}

Now let's implement our class let's say Bala based on our square public class Bullet extends Square {

private Bresenham bre;
public int bd, bde, bdne;

public Bala(int x, int y) {

    this.y = y;
    this.x = x;
    this.w = 2;
    this.h = 5;
    bre=new Bresenham();
}

public void calcularBresenhamBala() {

    bre.desplazar(1, 1);
    bd = bre.getdesicion();
    bde = bre.getdesicionDe();
    bdne = bre.getdesicionDne();
}

public void crearBala(Graphics g) {
    this.dibujarCuadrado(g, x, y, w, h,100);
}

public Cuadrado getCuadrado() {
    return this;
}

public void iterarBala(){


    if (bd <= 0) {
            bd = bd + bde;
            y = y - 2;
        } else {
            bd = bd + bdne;
            x = x + 1;
            y = y - 2;
        }
}
}

editing ...

    
answered by 19.11.2016 в 04:08