下面列出了怎么用java.awt.image.BufferStrategy的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Lets the manager know that this buffer strategy is no longer interested
* in being v-synced.
*/
public static synchronized void releaseVsync(BufferStrategy bs) {
VSyncedBSManager bsm = getInstance(false);
if (bsm != null) {
bsm.relinquishVsync(bs);
}
}
public void draw() throws Exception {
BufferStrategy strategy = getBufferStrategy();
Graphics2D graphics = (Graphics2D) strategy.getDrawGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.setColor(Color.white);
graphics.fill(new Rectangle(dimension));
currentScreen = currentScreen.getNextScreen();
currentScreen.draw(graphics);
graphics.dispose();
strategy.show();
}
/**
* Lets the manager know that this buffer strategy is no longer interested
* in being v-synced.
*/
public static synchronized void releaseVsync(BufferStrategy bs) {
VSyncedBSManager bsm = getInstance(false);
if (bsm != null) {
bsm.relinquishVsync(bs);
}
}
@Override
public synchronized void relinquishVsync(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy b = strategy.get();
if (b == bs) {
strategy.clear();
strategy = null;
}
}
}
@Override
public synchronized boolean checkAllowed(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy current = strategy.get();
if (current != null) {
return (current == bs);
}
}
strategy = new WeakReference<BufferStrategy>(bs);
return true;
}
public void render() {
ImageCapabilities imgBackBufCap = new ImageCapabilities(true);
ImageCapabilities imgFrontBufCap = new ImageCapabilities(true);
BufferCapabilities bufCap =
new BufferCapabilities(imgFrontBufCap,
imgBackBufCap, BufferCapabilities.FlipContents.COPIED);
try {
createBufferStrategy(2, bufCap);
} catch (AWTException ex) {
createBufferStrategy(2);
}
BufferStrategy bs = getBufferStrategy();
do {
Graphics g = bs.getDrawGraphics();
g.setColor(Color.green);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.red);
g.drawString("Rendering test", 20, 20);
g.drawImage(bi, 50, 50, null);
g.dispose();
bs.show();
} while (bs.contentsLost()||bs.contentsRestored());
}
public void render() {
ImageCapabilities imgBackBufCap = new ImageCapabilities(true);
ImageCapabilities imgFrontBufCap = new ImageCapabilities(true);
BufferCapabilities bufCap =
new BufferCapabilities(imgFrontBufCap,
imgBackBufCap, BufferCapabilities.FlipContents.COPIED);
try {
createBufferStrategy(2, bufCap);
} catch (AWTException ex) {
createBufferStrategy(2);
}
BufferStrategy bs = getBufferStrategy();
do {
Graphics g = bs.getDrawGraphics();
g.setColor(Color.green);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.red);
g.drawString("Rendering test", 20, 20);
g.drawImage(bi, 50, 50, null);
g.dispose();
bs.show();
} while (bs.contentsLost()||bs.contentsRestored());
}
/**
* Lets the manager know that this buffer strategy is no longer interested
* in being v-synced.
*/
public static synchronized void releaseVsync(BufferStrategy bs) {
VSyncedBSManager bsm = getInstance(false);
if (bsm != null) {
bsm.relinquishVsync(bs);
}
}
/**
* Lets the manager know that this buffer strategy is no longer interested
* in being v-synced.
*/
public static synchronized void releaseVsync(BufferStrategy bs) {
VSyncedBSManager bsm = getInstance(false);
if (bsm != null) {
bsm.relinquishVsync(bs);
}
}
@Override
public synchronized boolean checkAllowed(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy current = strategy.get();
if (current != null) {
return (current == bs);
}
}
strategy = new WeakReference<BufferStrategy>(bs);
return true;
}
public void render() {
ImageCapabilities imgBackBufCap = new ImageCapabilities(true);
ImageCapabilities imgFrontBufCap = new ImageCapabilities(true);
BufferCapabilities bufCap =
new BufferCapabilities(imgFrontBufCap,
imgBackBufCap, BufferCapabilities.FlipContents.COPIED);
try {
createBufferStrategy(2, bufCap);
} catch (AWTException ex) {
createBufferStrategy(2);
}
BufferStrategy bs = getBufferStrategy();
do {
Graphics g = bs.getDrawGraphics();
g.setColor(Color.green);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.red);
g.drawString("Rendering test", 20, 20);
g.drawImage(bi, 50, 50, null);
g.dispose();
bs.show();
} while (bs.contentsLost()||bs.contentsRestored());
}
/**
* Lets the manager know that this buffer strategy is no longer interested
* in being v-synced.
*/
public static synchronized void releaseVsync(BufferStrategy bs) {
VSyncedBSManager bsm = getInstance(false);
if (bsm != null) {
bsm.relinquishVsync(bs);
}
}
/**
* Lets the manager know that this buffer strategy is no longer interested
* in being v-synced.
*/
public static synchronized void releaseVsync(BufferStrategy bs) {
VSyncedBSManager bsm = getInstance(false);
if (bsm != null) {
bsm.relinquishVsync(bs);
}
}
@Override
public synchronized boolean checkAllowed(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy current = strategy.get();
if (current != null) {
return (current == bs);
}
}
strategy = new WeakReference<BufferStrategy>(bs);
return true;
}
@Override
public synchronized boolean checkAllowed(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy current = strategy.get();
if (current != null) {
return (current == bs);
}
}
strategy = new WeakReference<BufferStrategy>(bs);
return true;
}
public void render() {
ImageCapabilities imgBackBufCap = new ImageCapabilities(true);
ImageCapabilities imgFrontBufCap = new ImageCapabilities(true);
BufferCapabilities bufCap =
new BufferCapabilities(imgFrontBufCap,
imgBackBufCap, BufferCapabilities.FlipContents.COPIED);
try {
createBufferStrategy(2, bufCap);
} catch (AWTException ex) {
createBufferStrategy(2);
}
BufferStrategy bs = getBufferStrategy();
do {
Graphics g = bs.getDrawGraphics();
g.setColor(Color.green);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.red);
g.drawString("Rendering test", 20, 20);
g.drawImage(bi, 50, 50, null);
g.dispose();
bs.show();
} while (bs.contentsLost()||bs.contentsRestored());
}
/**
* Lets the manager know that this buffer strategy is no longer interested
* in being v-synced.
*/
public static synchronized void releaseVsync(BufferStrategy bs) {
VSyncedBSManager bsm = getInstance(false);
if (bsm != null) {
bsm.relinquishVsync(bs);
}
}
@Override
public synchronized boolean checkAllowed(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy current = strategy.get();
if (current != null) {
return (current == bs);
}
}
strategy = new WeakReference<BufferStrategy>(bs);
return true;
}
public void render() {
ImageCapabilities imgBackBufCap = new ImageCapabilities(true);
ImageCapabilities imgFrontBufCap = new ImageCapabilities(true);
BufferCapabilities bufCap =
new BufferCapabilities(imgFrontBufCap,
imgBackBufCap, BufferCapabilities.FlipContents.COPIED);
try {
createBufferStrategy(2, bufCap);
} catch (AWTException ex) {
createBufferStrategy(2);
}
BufferStrategy bs = getBufferStrategy();
do {
Graphics g = bs.getDrawGraphics();
g.setColor(Color.green);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.red);
g.drawString("Rendering test", 20, 20);
g.drawImage(bi, 50, 50, null);
g.dispose();
bs.show();
} while (bs.contentsLost()||bs.contentsRestored());
}
@Override
public synchronized boolean checkAllowed(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy current = strategy.get();
if (current != null) {
return (current == bs);
}
}
strategy = new WeakReference<BufferStrategy>(bs);
return true;
}
public GamePanel(BufferStrategy bs, int width, int height) {
GamePanel.width = width;
GamePanel.height = height;
this.bs = bs;
setPreferredSize(new Dimension(width, height));
setFocusable(true);
requestFocus();
}
@Override
public synchronized void relinquishVsync(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy b = strategy.get();
if (b == bs) {
strategy.clear();
strategy = null;
}
}
}
@Override
public synchronized void relinquishVsync(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy b = strategy.get();
if (b == bs) {
strategy.clear();
strategy = null;
}
}
}
@Override
public synchronized boolean checkAllowed(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy current = strategy.get();
if (current != null) {
return (current == bs);
}
}
strategy = new WeakReference<BufferStrategy>(bs);
return true;
}
@Override
public synchronized void relinquishVsync(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy b = strategy.get();
if (b == bs) {
strategy.clear();
strategy = null;
}
}
}
public void render() {
ImageCapabilities imgBackBufCap = new ImageCapabilities(true);
ImageCapabilities imgFrontBufCap = new ImageCapabilities(true);
BufferCapabilities bufCap =
new BufferCapabilities(imgFrontBufCap,
imgBackBufCap, BufferCapabilities.FlipContents.COPIED);
try {
createBufferStrategy(2, bufCap);
} catch (AWTException ex) {
createBufferStrategy(2);
}
BufferStrategy bs = getBufferStrategy();
do {
Graphics g = bs.getDrawGraphics();
g.setColor(Color.green);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.red);
g.drawString("Rendering test", 20, 20);
g.drawImage(bi, 50, 50, null);
g.dispose();
bs.show();
} while (bs.contentsLost()||bs.contentsRestored());
}
/**
* Lets the manager know that this buffer strategy is no longer interested
* in being v-synced.
*/
public static synchronized void releaseVsync(BufferStrategy bs) {
VSyncedBSManager bsm = getInstance(false);
if (bsm != null) {
bsm.relinquishVsync(bs);
}
}
@Override
public synchronized boolean checkAllowed(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy current = strategy.get();
if (current != null) {
return (current == bs);
}
}
strategy = new WeakReference<BufferStrategy>(bs);
return true;
}
@Override
public synchronized void relinquishVsync(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy b = strategy.get();
if (b == bs) {
strategy.clear();
strategy = null;
}
}
}
@Override
public synchronized void relinquishVsync(BufferStrategy bs) {
if (strategy != null) {
BufferStrategy b = strategy.get();
if (b == bs) {
strategy.clear();
strategy = null;
}
}
}