Nose como tengo que hacer para añadir mi sprite al doble buffer para que no se note este parpadeo. El codigo es este:
Código:
y el meto draw del sprite es el siguiente:public class VPrincipal extends JFrame implements Runnable{ Thread miThread1= new Thread(); Thread miThread2=new Thread(); private static String mapFileName = "mapa.FMP"; public static int windowWidth = 820; // dimensions in pixels public static int windowHeight = 556; private static int speedX = 2; // no of pixels to move the map each frame private static int speedY = 2; private static int refreshRate = 30; // no of milli-seconds that should pass between each frame, 33 ~ 30Hz private static boolean running = false; // is set to true if the map is moving private Image doubleBuffer; private MapViewer mapViewer; private LayerViewer layerViewer; //private KeyboardInput keyboardInput = new KeyboardInput(); private static Class renderType = JDK12Renderer.class; public VPrincipal(){ this.addKeyListener(new KeyListener() { public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { if (!miThread1.isAlive() && !miThread2.isAlive()){ int codigoTecla= e.getKeyCode(); if (codigoTecla>=37 && codigoTecla<=40){ HiloAnimarHeroe hiloAnda = new HiloAnimarHeroe(codigoTecla, miThread2, mapViewer, layerViewer); miThread1 = new Thread(hiloAnda); miThread1.start(); } else if(codigoTecla==32){ /*HiloAtaqueHeroe hiloAtque = new HiloAtaqueHeroe(pPrincipal); miThread1 = new Thread(hiloAtque); miThread1.start(); * */ } else if(codigoTecla==10){ FuncionesNPC.hayNPC(FuncionesHeroe.getHeroe()); } } } public void keyReleased(KeyEvent e) { } }); } public void initialise(){ BufferedInputStream inputStream=null; try { inputStream = new BufferedInputStream(new FileInputStream(mapFileName)); } catch (FileNotFoundException ex) { java.util.logging.Logger.getLogger(VPrincipal.class.getName()).log(Level.SEVERE, null, ex); } // create the viewers, making them render with the given JDK Map map = MapLoader.loadMap(inputStream); Renderer r = null; try { try { r = ((Renderer) renderType.getConstructor(new Class[] {Map.class}).newInstance(new Object[] {map})); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(VPrincipal.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(VPrincipal.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { java.util.logging.Logger.getLogger(VPrincipal.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { java.util.logging.Logger.getLogger(VPrincipal.class.getName()).log(Level.SEVERE, null, ex); } } catch (NoSuchMethodException ex) { java.util.logging.Logger.getLogger(VPrincipal.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { java.util.logging.Logger.getLogger(VPrincipal.class.getName()).log(Level.SEVERE, null, ex); } mapViewer = new MapViewer(map, r, windowWidth, windowHeight, 0, 0); layerViewer = mapViewer.getLayerViewers()[0]; layerViewer.addPixelX(32*6); layerViewer.addPixelY(32*4); try { inputStream.close(); } catch (IOException ioe) { /**/ } // create double buffer doubleBuffer = super.createImage(windowWidth, windowHeight); } public void run() { running = true; // the game loop while (running) { long startTime = System.currentTimeMillis(); // scroll the map... // animate the map... mapViewer.updateAnimBlocks(); // ...and draw it in the buffer /* he probado tanto con: Graphics2D gfx = (Graphics2D)doubleBuffer.getGraphics(); y con: gfx.drawImage(FuncionesHeroe.getHeroe().imagen(),50,50,null); */ Graphics2D gfx = (Graphics2D)doubleBuffer.getGraphics(); //gfx.drawImage(FuncionesHeroe.getHeroe().imagen(),50,50,null); layerViewer.draw(gfx, false); gfx.setColor(Color.RED); gfx.drawString("X: " + layerViewer.getPixelX() + " Y: " + layerViewer.getPixelY(), 10, 40); gfx.dispose(); // render the buffer to the screen super.repaint(); // rest for the remainder of the jiffy long timeTaken = System.currentTimeMillis() - startTime; if (timeTaken < refreshRate) { try { // Sleep little one, sleep! Thread.sleep(refreshRate - timeTaken); } catch (InterruptedException ie) { /**/ } } else { // give other threads a chance to catch up Thread.yield(); } } } public void paint(Graphics g) { if (doubleBuffer != null) { // take into account the window border Insets insets = super.getInsets(); g.drawImage(doubleBuffer, insets.left, insets.top, null); FuncionesHeroe.getHeroe().draw(g); //devuelve el Heroe (Sprite) y llama a su metodo } } public void update(Graphics g) { // this prevents the screen being white-washed paint(g); } }
Código:
public void draw(Graphics g) { g.drawImage(((Image)vSprites.get(frame)),posx,posy,null); }
Si alguien tiene una pequeña idea de como solucionar esto me seria de gran ayuda.
Gracias,
Dani.