Hi!
This is my first try at writing a game in Java. I've tried to set up a buffering strategy for my JFrame and then have an animation thread call repaint on the frame every 20 milliseconds. The contents of the window flickers heavily (much more than if I use an BufferedImage as back buffer for example).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| package wizards;
import java.awt.Graphics; import java.awt.image.BufferStrategy; import javax.swing.JFrame;
import wizards.gui.AnimationThread; import wizards.gui.ImageLibrary; import wizards.map.Map; import wizards.map.Scenery;
public class Wizards extends JFrame { public static int MAP_WIDTH = 15; public static int MAP_HEIGHT = 15; public static int TILE_SIZE = 32; private Map map; private BufferStrategy strategy; public Wizards() { super("Wizards"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(MAP_WIDTH * TILE_SIZE, MAP_HEIGHT * TILE_SIZE); setVisible(true);
createBufferStrategy(2); strategy = getBufferStrategy(); new AnimationThread(this).start(); } public void paint(Graphics g) { super.paint(g); Graphics offscreenGraphics = strategy.getDrawGraphics(); map.draw(offscreenGraphics); strategy.show(); } public static void main(String[] args) { new Wizards(); } }
package wizards.gui;
import java.awt.Component;
public class AnimationThread extends Thread { private Component target; public AnimationThread(Component target) { this.target = target; } public void run() { while(Thread.currentThread() == this) { target.repaint(); try { Thread.sleep(20); } catch (InterruptedException e) {} } } }
|