简体   繁体   中英

why won't my drawings display without a loop?

I was experimenting with buffers and i tried to fill a buffer with the color green and then display it immediately on the screen but the screen wont display the green color without a loop. can some one explain this to me?

public void draw() {
    createBufferStrategy(2);

    Graphics g = image.createGraphics();
    BufferStrategy bs = getBufferStrategy();
    for (int i = 0; i < 16; i++) {
        System.out.println(i);
        if (!a) {
            g.setColor(Color.green);
            g.fillRect(0, 0, w, h);
        }
        a = true;
        Graphics bsg = bs.getDrawGraphics();
        // bsg.drawImage(image, 0, 0, h, w, null);
        // g.clearRect(0, 0, w, h);
        // g.drawOval(0, 0, 50, 50);
        bsg.setColor(Color.green);
        bsg.fillRect(0, 0, getWidth(), getHeight());
        bsg.dispose();
        bs.show();
    }

}

and I also found that if I set the loop to less than exactly 16 times or cancel it the image won't be displayed

here is a complete runnable code

public class BufferTest extends Canvas implements Runnable {

int h = 400;
int w = 400;
BufferedImage image;
boolean a;

public static void main(String[] a) {
    JFrame frame = new JFrame();
    BufferTest test = new BufferTest();
    frame.add(test);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    test.draw();
    Thread t = new Thread(test);

    // t.start();

}

public BufferTest() {
    this.setPreferredSize(new Dimension(w, h));
    image = new BufferedImage(h, w, BufferedImage.TYPE_INT_RGB);

}

public void draw() {
    createBufferStrategy(2);

//  Graphics g = image.createGraphics();
    BufferStrategy bs = getBufferStrategy();
    for (int i = 0; i < 16; i++) {
        System.out.println(i);
        //if (!a) {
            //g.setColor(Color.green);
            //g.fillRect(0, 0, w, h);
        //}
        //a = true;
        Graphics bsg = bs.getDrawGraphics();
         //bsg.drawImage(image, 0, 0, h, w, null);
         //g.clearRect(0, 0, w, h);
         //g.drawOval(0, 0, 50, 50);
        bsg.setColor(Color.green);
        bsg.fillRect(0, 0, getWidth(), getHeight());
        bsg.dispose();
        bs.show();
    }

}

@Override
public void run() {
    while (true) {
        draw();// TODO Auto-generated method stub
    }
}
}

When the loop is less than 16 the green color is on the screen but it disappears so quick that we can't understand. A cycle of loop is executed for nearly 1 ms. This is to fast for our brain. 16 cycles means nearly 16 ms and this is the minimum of speed that we can catch. That's why you see the green screen when it executes 16 times. In fact the screen changes 16 times but because of the speed the screens superposes with each other and we see it as a single screen. Try to put Thread t on sleep for some ms to get a better view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM