Re: Requesting tips, comm
To: comp.lang.java.gui
Oliver Wong wrote:
"Oliver Wong" <owong@castortech.com> wrote in message
news:soxMh.490$NM.6958@wagner.videotron.net...
[most of the code snipped]
/*
* This while loop is the main game loop. It basically iterates through
* 3 stages forever: getting the player input, reacting to it, and
* drawing the results on screen.
*/
while (!timeToQuit) {
getPlayerInput();
processGameLogic();
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
if (mainWindow != null) {
BufferStrategy strategy = mainWindow
.getBufferStrategy();
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
Insets insets = mainWindow.getInsets();
g.translate(insets.left, insets.top);
g.setTransform(AffineTransform
.getScaleInstance(
(double) (mainWindow.getWidth() - insets.right)
/ (double) DEFAULT_RENDERING_WIDTH,
(double) (mainWindow
.getHeight() - insets.bottom)
/ (double) DEFAULT_RENDERING_HEIGHT));
updateScreen(g);
Oops, I should probably call g.dispose() here.
strategy.show();
}
}
});
Thread.sleep(1);
}
- Oliver
Oliver:
For active rendering I would take your rendering out of the EDT
altogether. The problem that you are going to run into is that
eventually too much will be happening on the EDT and you won't be able
to do any user input processing or that it will delay your rendering.
You only need to create the BufferStrategy once not every time in the
rendering loop. I like to use java.util.Timer to drive the loop but you
can use Thread.sleep(). As Daniel said I would do my user input event
driven. You can use a Frame or JFrame but you have to deal with the
insets and title bar your self. If you use an undecorated frame you
might as well use a window.
Below is how I would do it. See if you like any of it.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
public class test4 extends JWindow {
java.util.Timer timer;
BufferStrategy bs;
int x,y,deltaX=3,deltaY=3;
public test4() {
// you want to do all drawing
setIgnoreRepaint(true);
addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent we) {
// don't create the buffer strategy
// until window is visible
test4.this.createBufferStrategy(2);
bs = getBufferStrategy();
// schedule animation
timer.schedule(new AnimationTask(),0,30);
}
});
timer = new java.util.Timer();
setSize(400,300);
setVisible(true);
}
class AnimationTask extends TimerTask {
public void run() {
x += deltaX;
y += deltaY;
if (x > getWidth() - 30 || x < 0)
deltaX = -deltaX;
if (y > getHeight() - 30 || y < 0)
deltaY = -deltaY;
render();
}
}
void render() {
do {
do {
// get new draw graphics every time
Graphics g = bs.getDrawGraphics();
// draw
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.BLUE);
g.fillOval(x,y,30,30);
// dispose of the draw graphics
g.dispose();
} while (bs.contentsRestored()) ;
// blit/flip buffers
bs.show();
} while (bs.contentsLost()) ;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
test4 t4 = new test4();
t4.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.exit(0);
}
});
}
};
EventQueue.invokeLater(r);
}
}
--
Knute Johnson
email s/nospam/knute/
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24