java podwojne buforowanie

0

Dzień dobry!

 package gramy;


import java.awt.Canvas;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.HashMap;

import javax.imageio.ImageIO;
import javax.swing.Timer;

public class AdditionalGamePicture extends Canvas implements ActionListener{
	public static int x = 350,y = 80 ,velocityX = 1 , velocityY =1 ;
	public static Image monster ,player ;
	public HashMap sprites ;
	
	
	Timer timer = new Timer(10,new ActionListener(){
		public void actionPerformed(ActionEvent evt){
			if(x>1185) velocityX = -velocityX ;
			if(x<300) velocityX = -velocityX ;
			if(y>585) velocityY = -velocityY ;
			if(y<50) velocityY = -velocityY ;
			
			x += velocityX;
			y += velocityY;
			repaint();
			
			
		}	
	});
			
	
	
	
		public  void actionPerformed(ActionEvent e){	
			Object source = e.getSource();
			
			if(source == AdditionalGameSource.button){			
				
			}
		
	};
	
	
	
	
	public AdditionalGamePicture(){
		
		sprites = new HashMap();
		
		timer.start();
		
		setBounds(1,1,Stage.WIDTH,Stage.HEIGHT);
		setVisible(true);
	}
	
	
	
	
	public void paint(Graphics g){
		
		 drawAll(g);
	
	}
	public void drawAll(Graphics g){
		drawMonster(g);
		 drawPlayer(g);
		// drawBoard(g);
	}
	
	public void drawMonster(Graphics g ){
		g.drawImage(getSprite("obcy.gif"), x , y, this);
	}
	public void drawPlayer(Graphics g ){
		g.drawImage(getSprite("statek.gif"), 1000 , 300, this);
	}

	public void drawBoard(Graphics g){
		g.drawLine(300,50, 1200, 50);
		g.drawLine(1200,50, 1200, 600);
		g.drawLine(1200,600, 300, 600);
		g.drawLine(300,600, 300, 50);
		
	}
	
	public  BufferedImage loadImage(String sciezka) {
		URL url=null;
		try {
		url = getClass().getClassLoader().getResource(sciezka);
		return ImageIO.read(url);
		} catch (Exception e) {
		System.out.println("error 0002"+ sciezka);
		System.exit(0);
		return null;
		}
		}
	
	public BufferedImage getSprite(String sciezka) {
		BufferedImage img = (BufferedImage)sprites.get(sciezka);
		if (img == null) {
		img = loadImage(sciezka);
		sprites.put(sciezka,img);
		}
		return img;
		}

	
}

Oto mój kod który wyświetla na ekranie latającą piłeczkę. Niestety ona strasznie miga- starałem sie w tym kodzie zastosować podwójne bufforowanie niestety ono nie działa mogły ktoś wskazać mi błąd ?

0

1.Te twoje helpx i helpy oznaczają wektor prędkości, zatem jak już to velocityX oraz velocityY.
2.Nigdzie tutaj nie masz podwójnego buforowania.
3.timer.start(); istnieje jakikolwiek sensowny powód, dla którego robisz to w wątku?

0

Poprawiłem nazwy i rzeczywiście timer.start(); był w nieodpowiednim miejscu.
Co do podwójnego buforowania to nie mam pomysłu jak go zaimplementować by spełniał swoją funkcje.

0

I pewnie jeszcze powiesz, że czytałeś w internecie i nie ma nic o podwójnym buforowaniu w Javie, prawda?

0

Czytałem o podwójnym buforowaniu ale nadal nie potrafie sbie poradzic z tym problemem.Aktualnie moj kod wyglada tak :

package gramy;


import java.awt.Canvas;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.net.URL;


import javax.imageio.ImageIO;
import javax.swing.Timer;

public class AdditionalGamePicture extends Canvas implements ActionListener , KeyListener{
	public static int x = 350,y = 80 ,velocityX = 1 , velocityY =1  , xx = 1000 , yy = 500;
	public static int xr  , yr   ;
	public static Image monster ,player , rocket ;
	private int bufferWidth ;
	private int bufferHeight ;
	private Image bufferImage,obcy;
	private Graphics bufferGraphics;
	
	
	
	
	
	Timer timer = new Timer(1,new ActionListener(){
		public void actionPerformed(ActionEvent evt){
			if(x>1185) velocityX = -velocityX ;
			if(x<300) velocityX = -velocityX ;
			if(y>585) velocityY = -velocityY ;
			if(y<50) velocityY = -velocityY ;
			
			if( yr > 50 ){
				yr-- ;
			}else{
				//niszczenie kuli
				yr = -500;
			}
				x += velocityX;
				y += velocityY;
			repaint();
			
			
		}	
	});
			
	
	
	
		public  void actionPerformed(ActionEvent e){	
			Object source = e.getSource();
			
			if(source == AdditionalGameSource.button){			
				
			}
		
	};
	
	
	
	
	public AdditionalGamePicture(){

		
		timer.start();
		
		obcy = loadImage("obcy.gif");
		setBounds(1,1,Stage.WIDTH,Stage.HEIGHT);
		setVisible(true);
		addKeyListener(this);
		
		 
		
		
	}
	
	public void paint(Graphics g){
		
		
		 if(bufferWidth!=getSize().width || bufferHeight!=getSize().height || bufferImage==null || bufferGraphics==null)
			        resetBuffer();
		 
		 if(bufferGraphics!=null){
		        //this clears the offscreen image, not the onscreen one
		        bufferGraphics.clearRect(0,0,bufferWidth,bufferHeight);

		        //calls the paintbuffer method with 
		        //the offscreen graphics as a param
		        paintBuffer(bufferGraphics);

		        //we finaly paint the offscreen image on to the onscreen image
		        g.drawImage(bufferImage,0,0,this);
		    }
	}
	
	public void paintBuffer(Graphics g){
	    //in classes extended from this one, add something to paint here!
	    //always remember, g is the offscreen graphics
		
		g.drawImage(obcy, x , y, this);
	//	g.drawImage(loadImage("statek.gif"), xx , yy, this);
	//	g.drawImage(rocket, xr, yr, this);
	//	g.drawLine(300,50, 1200, 50);
	//	g.drawLine(1200,50, 1200, 600);
	//	g.drawLine(1200,600, 300, 600);
	//	g.drawLine(300,600, 300, 50);
		
	}
	
	private void resetBuffer(){
	    // always keep track of the image size
	   bufferWidth=getSize().width;
	    bufferHeight=getSize().height;
	    
	    //    clean up the previous image
	    if(bufferGraphics!=null){
	        bufferGraphics.dispose();
	        bufferGraphics=null;
	        
	    }
	    if(bufferImage!=null){
	        bufferImage.flush();
	        bufferImage=null;
	    }
	    System.gc();

	    //    create the new image with the size of the panel
	    bufferImage=createImage(bufferWidth,bufferHeight);
	    bufferGraphics=bufferImage.getGraphics();
	}
	
	public  BufferedImage loadImage(String sciezka) {
		URL url=null;
		try {
		url = getClass().getClassLoader().getResource(sciezka);
		return ImageIO.read(url);
		} catch (Exception e) {
		System.out.println("error 0002"+ sciezka);
		System.exit(0);
		return null;
		}
		}




	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.getKeyCode() == 39 && xx < 1185){
			xx += 5 ;
		}
		if(e.getKeyCode() == 37 && xx > 300){
			xx -= 5 ;
		}
		if(e.getKeyCode() == 38 && xx >300){
			rocket = loadImage("rakieta.gif");
			
			xr = xx ;
			yr = yy ;
		}
		
	}



// tu
	@Override
	public void keyReleased(KeyEvent arg0) {
		
		
	}




	@Override
	public void keyTyped(KeyEvent arg0) {
		
	}
	
	

	
}
 

1 użytkowników online, w tym zalogowanych: 0, gości: 1