在Applet中缓冲

时间:2015-05-20 23:35:40

标签: java canvas graphics applet buffer

我正在尝试使用移动的图形对象创建一个刷新的applet。但是我试图结合缓冲以防止我在执行程序时遇到的闪烁。如果您能提供有关如何使该计划有效的任何帮助,我将不胜感激。

import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.applet.*;
import java.util.*;
import java.io.*;

import javax.imageio.ImageIO;

public class Flappy extends Applet {

    static flappyBird Flappy1;
    flappyBird FlappyF;
    static Illuminati Illuminati1;
    static Illuminati IlluminatiEye;
    static Illuminati IlluminatedFlappy;
    static Illuminati IlluminatiTroll;
    static BufferedImage img;
    static int x;
    DoubleB Buffers;
    @Override
    public void init() {
            Flappy1 = new flappyBird(500,500, 1);
            Illuminati1 = new Illuminati(10, 50, 1);
            IlluminatiEye = new Illuminati_eye(100,50,1);
            IlluminatiTroll = new Illuminati_Troll(200, 50, 1);
            IlluminatedFlappy = new illuminati_Flappy(300,50,1);
            FlappyF = new flappyFly(800,500,1);
                System.out.println("init");
                this.setSize(1000,1000); 
                this.setVisible(true); 

                try {
                    img = ImageIO.read(new File("bgflappy.jpg"));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }    
                x=2;
    }
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
         RenderingHints rh1 = new RenderingHints(
                 RenderingHints.KEY_ANTIALIASING,
                 RenderingHints.VALUE_ANTIALIAS_ON);
         RenderingHints rh2 = new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
         g2.setRenderingHints(rh1); 
         g2.setRenderingHints(rh2);
         Flappy1.drawFlappy(g2);
         FlappyF.drawFlappy(g2); 
        Illuminati1.gridIlluminati(g2);
        IlluminatiEye.gridIlluminati(g2);
        IlluminatiTroll.gridIlluminati(g2);
        IlluminatedFlappy.gridIlluminati(g2);

        }


    class DoubleB extends Canvas{
        Graphics g;
        public DoubleB (Graphics g2){
        g=g2;

        }
        public void rfresh(Graphics g)
            {

            Graphics gg;
            Image offscreen = null;
            Dimension d = new Dimension(1000,1000);
            offscreen = createImage(d.width, d.height);
            gg = offscreen.getGraphics();
            if (x==2)
            {   
        gg.drawImage(img, 0, 0, null);
        Flappy1.drawFlappy(gg);
        Illuminati1.gridIlluminati(gg);
        IlluminatiEye.gridIlluminati(gg);
        IlluminatiTroll.gridIlluminati(gg);
        IlluminatedFlappy.gridIlluminati(gg);
            }
            if (x==3)
            {
                g.drawImage(offscreen, 0, 0, this);
                x=-1;   

            }
    x++;

}}
class flappyBird {
    private final int topLx, topLy, pxlSize;

    public flappyBird(int topLx, int topLy, int pxlSize) {
        this.topLx = topLx;
        this.topLy = topLy;
        this.pxlSize = pxlSize;
    }

    public void drawFlappy(Graphics g2) {
        int[][] FlappyBird = {
                drawGrid(g2, getTopLx(), getTopLy(), getPxlSize(), FlappyBird);

    }

    public void drawGrid(Graphics g2, int xStart, int yStart, int pixSize,int[][] grid) {
            int nRows = grid.length;
             Color[] pallette = { Color.white, Color.red, Color.green,Color.blue, Color.yellow, Color.cyan, Color.magenta, Color.black };
             Color[] pallette2 = { Color.WHITE, Color.GRAY, Color.YELLOW,Color.ORANGE, Color.green, Color.BLUE, Color.magenta, Color.RED,Color.PINK, Color.BLACK };
        int nCols = grid[0].length;
                System.out.print(nCols);
        int x = xStart;
        int y = yStart;
        int p = pixSize;
        for (int r = 0; r < nRows; r++)
            for (int c = 0; c < nCols; c++) {
                g2.setColor(pallette2[grid[r][c]]);
                g2.fillRect(x + c * p, y + r * p, p, p);
            }
    }

    public int getTopLx() {
        return this.topLx;
    }

    public int getTopLy() {
        return this.topLy;
    }

    public int getPxlSize() {
        return this.pxlSize;
    }

}


class flappyFly extends flappyBird {
    public flappyFly(int topLx, int topLy, int pxlSize) {
        super(topLx, topLy, pxlSize);


        }

    public Color Little(double valH, double valS, double valB) { ////////////////////////////////////////////////////////////////////////////TRANSPARENT COLOR
        double H = valH; 
        double S = valS; 
        double B =  valB; 
        int rgb = Color.HSBtoRGB((float)H, (float)S, (float)B);
        int red = (rgb >> 16) & 0xFF;
        int green = (rgb >> 8) & 0xFF;
        int blue = rgb & 0xFF;
        Color color = new Color(red, green, blue, 200);
        return color;
        }
    public Color color(double valH, double valS, double valB) { ////////////////////////////////////////////////////////////////////////////TRANSPARENT COLOR
        double H = valH * 0.3; 
        double S = valS*0.9; 
        double B =  valB*0.9; 
        int rgb = Color.HSBtoRGB((float)H, (float)S, (float)B);
        int red = (rgb >> 16) & 0xFF;
        int green = (rgb >> 8) & 0xFF;
        int blue = rgb & 0xFF;

        Color color = new Color(red, green, blue, 0x00);
        return color;}

    public void drawGrid(Graphics g2, int xStart, int yStart, int pixSize,int[][] grid)
    {
        Buffers=new DoubleB(g2);


        int nRows = grid.length;
        Color[] pallette = { Color.white, Color.red, Color.green,Color.blue, Color.yellow, Color.cyan, Color.magenta, Color.black };
        Color[] pallette2 = { Color.WHITE, Color.GRAY, Color.YELLOW,Color.ORANGE, Color.green, Color.BLUE, Color.magenta, Color.RED,Color.PINK, Color.BLACK };
    int nCols = grid[0].length;
           System.out.print(nCols);
    double x = xStart;
    double y = yStart;
    int p = pixSize;
    double addy=0;
    double addx=0;

    Random Randy = new Random();
    int z =0;
    while (z!=1)
    {   
    int xcurr = Randy.nextInt(1000)+1;
    int ycurr = Randy.nextInt(700)+151;
    System.out.println("RndX: " + xcurr);
    System.out.println("RndY: " + ycurr);
    addx=(double)(xcurr-x)/x;
    addy=(double)(ycurr-y)/y;
    while(Math.abs(x-xcurr)>50 && Math.abs(y-ycurr)>50)
    {
    /*  if (x<=0||x>=1000)
        {   xcurr = -xcurr;
            System.out.println("xcurr: "+xcurr);

        }
        if (y<=5||y>=995)
            ycurr=-ycurr;
    */
        Buffers.update(g2);
            System.out.println("addy:" + addy + "\n addx:" + addx );
        x+=addx;
        y+=addy;
    for (int r = 0; r < nRows; r++)
        for (int c = 0; c < nCols; c++) {
            g2.setColor(pallette2[grid[r][c]]);
            if(grid[r][c]==2)
                g2.setColor(Little(61,86,70));
            if (grid[r][c]==0)
                g2.setColor(color(0,0,0));
            if (grid[r][c]==3)
                g2.setColor(Little(37, 88, 83));
            if(grid[r][c]==9)
                g2.setColor(Little(0,0,0));

            g2.fillRect((int)x + c * p, (int)y + r * p, p, p);
        }


    try {Thread.sleep(25);} catch(InterruptedException intrx) {/* handle the exception */}
    System.out.println("x " + x + "  y " + y);
    System.out.println("xcurr: "+xcurr+"   ycurr: " + ycurr);


}}}}

谢谢, 梅森

0 个答案:

没有答案