Login  Register

Re: Implement AWT in JOGL

Posted by choracy69 on Feb 17, 2016; 1:41pm
URL: https://forum.jogamp.org/Implement-AWT-in-JOGL-tp4036268p4036285.html

Ok, I will try with byteBUffer.

This is my fragment of code:

public class Fire extends Applet
    implements MouseListener, MouseMotionListener, KeyListener, Runnable
{
    int d = 900;
    int h = 600;
   
    Random rand = new Random();
    BufferedImage bi;
    Graphics2D big;
    Thread artist = null;
   
    int [][] grid, futureGrid;  // asigns 2 arrays
    int a = 0;
    int b = 0;
    int tempx = 0;
    int tempy = 0;
    int mwfuel, tranfuel, conifuel, decifuel, burnt, bfire, fire;    //color of different fuel types
    int terrain = 0;
    int E =  rand.nextInt(100);
    int windd = 0;
    int wspeed = 0;
    int xt = 0;
    String direc;
    int spread = 0;
    int spoty = 0;
    int spotx = 0;
   
   
   
    public void init()
    {
        setSize(d, h);
        setBackground( new Color(48,48,48) );
        addMouseMotionListener(this);
        addMouseListener(this);
        addKeyListener(this);
        setFocusable(true);
        bi = (BufferedImage) createImage(d, h);
        big = bi.createGraphics();
       
        wspeed = rand.nextInt(25);
        mwfuel = new Color(190,204,91).getRGB();
        tranfuel = new Color(168,192,20).getRGB();;
        conifuel = new Color(85,101,39).getRGB();;
        decifuel = new Color(110,80,42).getRGB();;
        burnt = new Color(48,48,48).getRGB();;
        bfire = new Color(192,64,0).getRGB();;
        fire = new Color(255,96,0).getRGB();;
       
        grid = new int[d][h];
        futureGrid = new int[d][h];
       
        for (int i=0; i<d;i++){        // sets the whole terrain to 0 indicating that this pixel did not catch fire
          for (int j=0; j<h;j++){
            grid[i][j] = 0;
            terrain = rand.nextInt(5);
            switch(terrain) {
            case 1:  
              bi.setRGB(i,j,mwfuel);
              break;
            case 2:
              bi.setRGB(i,j,tranfuel);
              break;
            case 3:
              bi.setRGB(i,j,conifuel);
              break;
            case 4:
              bi.setRGB(i,j,decifuel);
              break;
            }
          }
        }

        windd = rand.nextInt(5);
        switch (windd) {
        case 1:
          direc = "North";
          break;
        case 2:
          direc = "South West";
          break;
        case 3:
          direc = "West";
          break;
        case 4:
          direc = "North West";
          break;
        }
    System.out.println("Init sie wykonał");
    }

...
}

And fragment of paint()

public void paint(Graphics g)
    {
    Graphics2D g2 = (Graphics2D) g;  

    //System.out.println("PAINT");
    if (xt<wspeed) {
    xt++;
    }
    if (xt>wspeed) {
      xt--;
    }
    if (xt==wspeed) {
      wspeed = rand.nextInt(25);
    }
    if ((xt>10)&&(xt<15)) {
      spread = 1;
    }
    if ((xt>=15)&&(xt<20)) {
      spread = 2;
    }
    if (xt>=20) {
      spread = 3;
    }    

    for (int x=1;x<d-1; x++){
        for (int y=1; y<h-1;y++){
       
         
           
        int nb = neighbors(x,y);
        if ((grid[x][y] == 0)&&(nb == 0)){           //current is green and neighbours are green
          futureGrid[x][y] = 0;    
        }
        else if ((grid[x][y] == 0) && (nb != 0) && (bi.getRGB(x,y) == mwfuel) && (E<59)) {
                    bi.setRGB(x,y,fire);
            futureGrid[x][y] = 1;
            if(windd == 1) {
              if((y-spread>0)&&(y-spread<h)){
                for(int w=0;w<=spread;w++) {
                  bi.setRGB(x,y-w,fire);
                  futureGrid[x][y-w] = 1;
                }
                int tempy = rand.nextInt(y);
                if (spread == 3) {
                  if (tempy<5) {
                    tempy = rand.nextInt(y);
                    bi.setRGB(x,tempy,fire);
                    grid[x][tempy]=1;
                  }
                }
              }
            }

            if(windd ==2) {
              if((x-spread>0)&&(x-spread<d-1)) {
                if((y+spread>0)&&(y+spread<h)){
                  for(int w=0;w<=spread;w++) {
                    bi.setRGB(x-w,y+w,fire);
                    futureGrid[x-w][y+w] = 1;
                  }
                }
              }
            }
...
    int[][] temp = grid;
    grid = futureGrid;
    futureGrid = temp;
    g2.drawImage(bi, null, 0, 0);
    }