Java 小程序中的缓冲

Java 小程序中的缓冲,java,canvas,graphics,applet,buffer,Java,Canvas,Graphics,Applet,Buffer,我试图制作一个小程序,用移动的图形对象刷新。然而,我正试图结合缓冲,以防止我在执行程序时所经历的闪烁。我将非常感谢您对如何使程序正常运行提供的任何帮助 import java.awt.*; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.applet.*; import java.util.*; import java.io.*; import javax.imagei

我试图制作一个小程序,用移动的图形对象刷新。然而,我正试图结合缓冲,以防止我在执行程序时所经历的闪烁。我将非常感谢您对如何使程序正常运行提供的任何帮助

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);


}}}}
import java.awt.*;
导入java.awt.image.BufferStrategy;
导入java.awt.image.buffereImage;
导入java.applet.*;
导入java.util.*;
导入java.io.*;
导入javax.imageio.imageio;
公共类Flappy扩展Applet{
静态飞翼鸟;
扑翼鸟扑翼鸟;
静态照明1;
静态照明;
静态照明照明;
静态照明器;
静态缓冲图像;
静态int x;
双B缓冲器;
@凌驾
公共void init(){
Flappy1=新的flappyBird(500500,1);
照明1=新照明(10,50,1);
照明眼=新的照明眼(100,50,1);
照明巨魔=新的照明巨魔(200,50,1);
IlluminatedFlappy=新照明灯具(300,50,1);
FlappyF=新flappyFly(800500,1);
System.out.println(“init”);
此.setSize(10001000);
此.setVisible(true);
试一试{
img=ImageIO.read(新文件(“bgflappy.jpg”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}    
x=2;
}
公共空间涂料(图g){
图形2d g2=(图形2d)g;
RenderingHights rh1=新的RenderingHights(
RenderingHists.KEY\u抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
RenderingHights rh2=新的RenderingHights(RenderingHights.KEY\u ALPHA\u插值,RenderingHights.VALUE\u ALPHA\u插值\u质量);
g2.设置渲染提示(rh1);
g2.设置渲染提示(rh2);
Flappy1.drawFlappy(g2);
FlappyF.drawFlappy(g2);
照明1.网格照明(g2);
光照眼。网格光照(g2);
光照者。网格光照者(g2);
被照亮的,被照亮的,被照亮的(g2);
}
类DoubleB扩展画布{
图形g;
公共双B(图形g2){
g=g2;
}
公共无效rfresh(图g)
{
图形gg;
屏幕外图像=空;
尺寸d=新尺寸(10001000);
屏幕外=创建图像(d.宽度,d.高度);
gg=offscreen.getGraphics();
如果(x==2)
{   
drawImage(img,0,0,null);
Flappy1.drawFlappy(gg);
照明1.网格照明(gg);
光照眼.网格光照(gg);
光照巨魔(gg);
被照亮的lappy.gridlightia(gg);
}
如果(x==3)
{
g、 drawImage(屏幕外,0,0,此);
x=-1;
}
x++;
}}
类飞禽{
私有最终int topLx、topLy、pxlSize;
公共飞翼鸟(int-topLx、int-topLy、int-pxlize){
this.topLx=topLx;
this.topLy=topLy;
this.pxlize=pxlize;
}
公共空间drawFlappy(图形g2){
int[]FlappyBird={
drawGrid(g2、getTopLx()、getTopLy()、getPxlSize()、FlappyBird);
}
公共void绘图网格(图形g2、int-xStart、int-yStart、int-pixSize、int[][]网格){
int nRows=网格长度;
颜色[]淡色={Color.white,Color.red,Color.green,Color.blue,Color.yellow,Color.cyan,Color.洋红,Color.black};
Color[]Pallete2={Color.WHITE,Color.GRAY,Color.YELLOW,Color.ORANGE,Color.green,Color.BLUE,Color.品红色,Color.RED,Color.PINK,Color.BLACK};
int nCols=网格[0]。长度;
系统输出打印(nCols);
intx=xStart;
int y=yStart;
int p=像素大小;
对于(int r=0;r>16)和0xFF;
绿色整数=(rgb>>8)&0xFF;
蓝色整数=rgb&0xFF;
颜色=新颜色(红色、绿色、蓝色、200);
返回颜色;
}
公共颜色(双谷、双谷、双谷){//////////////////////////////////////////////////////////////
双H=valH*0.3;
双S=VAL*0.9;
双B=valB*0.9;
int rgb=Color.HSBtoRGB((浮点数)H,(浮点数)S,(浮点数)B);
红色整数=(rgb>>16)和0xFF;
绿色整数=(rgb>>8)&0xFF;
蓝色整数=rgb&0xFF;
颜色=新颜色(红色、绿色、蓝色,0x00);
返回颜色;}
公共void绘图网格(图形g2、int-xStart、int-yStart、int-pixSize、int[][]网格)
{
缓冲区=新的双B(g2);
int nRows=网格长度;
颜色[]淡色={Color.white,Color.red,Color.green,Color.blue,Color.yellow,Color.cyan,Color.洋红,Color.black};
颜色[]Pallete2={Color.WHITE,Color.GRAY,Color.YELLOW,Color.ORANGE,Color.green,Color.BLUE,