package com.bmesta.intellij;

import java.awt.*;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import javax.swing.*;

import com.intellij.openapi.editor.Editor;
import com.intellij.ui.JBColor;

/**
 * @author Baptiste Mesta
 */
public class ParticleContainer extends Canvas {


    private BufferStrategy bufferstrat;

    public ParticleContainer(Editor editor) {
        JComponent parent = editor.getContentComponent();
        parent.add(this);
        createBufferStrategy(2);
        setSize(100, 100);
        bufferstrat = getBufferStrategy();
        setVisible(true);
        setIgnoreRepaint(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {

                    updateParticles();
                    render();

                    try {
                        Thread.sleep(1000 / 60);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }).start();
    }

    private void updateParticles() {

        for (int i = 0; i <= particles.size() - 1; i++) {
            if (particles.get(i).update())
                particles.remove(i);
        }

    }

    private ArrayList<Particle> particles = new ArrayList<Particle>(500);

    public void addParticle(boolean bool, int x, int y) {
        int dx, dy;
        if (bool) {
            dx = (int) (Math.random() * 3);
            dy = (int) (Math.random() * 3);
        } else {
            dx = (int) (Math.random() * -3);
            dy = (int) (Math.random() * -3);
        }
        int size = (int) (Math.random() * 5);
        int life = (int) (Math.random() * (120)) + 380;
        final Particle e = new Particle(x, y, dx, dy, size, life, JBColor.green);
        particles.add(e);
        System.out.println("created particle "+e);
    }

    public void renderParticles(Graphics2D g2d) {
        for (int i = 0; i <= particles.size() - 1; i++) {
            particles.get(i).render(g2d);
        }
    }

    public void render() {
        do {
            do {
                Graphics2D g2d = (Graphics2D) bufferstrat.getDrawGraphics();
                g2d.fillRect(0, 0, getWidth(), getHeight());
                renderParticles(g2d);
                g2d.dispose();
            } while (bufferstrat.contentsRestored());
            bufferstrat.show();
        } while (bufferstrat.contentsLost());
    }

    public void update(Point point) {
        final int midX = getWidth() / 2;
        final int midY = getHeight() / 2;
        this.setBounds(point.x - midX, point.y - midY, getWidth(), getHeight());
        System.out.println("set the canvas to " + point);
        addParticle(true, midX, midY);
        addParticle(false, midX, midY);
        addParticle(true, midX, midY);
        addParticle(false, midX, midY);
        addParticle(true, midX, midY);
        addParticle(false, midX, midY);
        render();
        this.repaint();
    }
}

@@ -46,46 +46,8 @@
 .*;%0A
-import java.awt.image.BufferStrategy;%0A
 impo
@@ -246,57 +246,61 @@
 nds 
-Canvas %7B%0A%0A%0A    private BufferStrategy bufferstrat
+JComponent %7B%0A%0A%0A    public static final int SIZE = 100
 ;%0A%0A 
@@ -440,153 +440,18 @@
     
-createBufferStrategy(2);%0A        setSize(100, 100);%0A        bufferstrat = getBufferStrategy();%0A        setVisible(true);%0A        setIgnoreRepaint
+setVisible
 (tru
@@ -618,38 +618,8 @@
 s();
-%0A                    render();
 %0A%0A  
@@ -865,32 +865,162 @@
 start();%0A    %7D%0A%0A
+    @Override%0A    protected void paintComponent(Graphics g) %7B%0A        super.paintComponent(g);%0A        renderParticles(g);%0A    %7D%0A%0A
     private void
@@ -1184,24 +1184,48 @@
 );%0A        %7D
+%0A        this.repaint();
 %0A%0A    %7D%0A%0A   
@@ -1813,59 +1813,8 @@
 e);%0A
-        System.out.println(%22created particle %22+e);%0A
     
@@ -1852,26 +1852,24 @@
 les(Graphics
-2D
  g2d) %7B%0A    
@@ -1985,410 +1985,8 @@
  %7D%0A%0A
-    public void render() %7B%0A        do %7B%0A            do %7B%0A                Graphics2D g2d = (Graphics2D) bufferstrat.getDrawGraphics();%0A                g2d.fillRect(0, 0, getWidth(), getHeight());%0A                renderParticles(g2d);%0A                g2d.dispose();%0A            %7D while (bufferstrat.contentsRestored());%0A            bufferstrat.show();%0A        %7D while (bufferstrat.contentsLost());%0A    %7D%0A
 %0A   
@@ -2045,26 +2045,20 @@
  midX = 
-getWidth()
+SIZE
  / 2;%0A  
@@ -2080,27 +2080,20 @@
  midY = 
-getHeight()
+SIZE
  / 2;%0A  
@@ -2149,89 +2149,18 @@
 dY, 
-getWidth(), getHeight());%0A        System.out.println(%22set the canvas to %22 + point
+SIZE, SIZE
 );%0A 
@@ -2399,26 +2399,8 @@
 Y);%0A
-        render();%0A
     

use jconponent instead of canvas