Podwójne buforowanie

0

Witam
Chciałbym w moim programie zastosować podwójne buforowanie lecz przy kompilacji wyskakuje mi bład i wskazuje na
buffer = (Graphics2D)image.getGraphics(); Błąd wyskakuje w konstruktorze.

Jeżeli ktoś mógłby poprawić bylbym wdzięczny
Oto cały kod mojej aplikacji

Klasa Main

public class Main {
	
	public static void main (String args[])
	{
		EventQueue.invokeLater(new Runnable(){
			
			public void run()
			{
			Ramka frame = new Ramka();
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);
			}
		});
	}

}
 

Klasa Ramka

public class Ramka extends JFrame
{
	Obraz display;
	BufferStrategy strategia;
	
	public Ramka()
	{
		display = new Obraz();
		add(display);
		display.setVisible(true);
	
}}
 

Klasa Obraz - tu występuje błąd, do rysowania

public class Obraz extends JPanel {
	Image image;
	Graphics2D buffer;
	Graphics2D device;
	public Obraz()
	{
		int widht = getWidth();
		int height = getHeight();
		image = createImage(widht, height);
		buffer = (Graphics2D)image.getGraphics();
		buffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		RenderingHints.VALUE_ANTIALIAS_ON);
		device = (Graphics2D)getGraphics();
		device.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
		RenderingHints.VALUE_ANTIALIAS_ON);
	}
	
	

	public void rysuj()
	{
		device.drawImage(image, 0, 0, null);
		buffer.clearRect(0, 0, getWidth(), getHeight());
	}
	

} 
1

component.createImage(w,h) tworzy obrazek z GraphicsConfiguration - ale jeśli komponent jest niewidoczny, to nie ma żadnej konfiguracji.

Jeśli bardzo chcesz stworzyć obrazki zanim komponenty będą widoczne, mały przykład tu:

		GraphicsEnvironment enviroment = GraphicsEnvironment.getLocalGraphicsEnvironment();
		// domyślne urządzenie
		GraphicsDevice device = enviroment.getDefaultScreenDevice();
		// domyślna konfiguracja
		GraphicsConfiguration gc = device.getDefaultConfiguration();
		
		// tworzy przezroczysty obrazek
		gc.createCompatibleImage(200,200, Transparency.TRANSLUCENT);
0

poprawiłem lecz niestety mi nic nie rysuje na panelu. moze ktos poprawic lub wskazac błąd?

public class Main {
    public static void main (String args[])
    {
        EventQueue.invokeLater(new Runnable()
        {
            
            public  void run()
            {
                JJFrame frame = new JJFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                frame.start();
            }
            
        });
        
    }
}
 public class JJFrame extends JFrame  {

    Display anim;


     public void start(){
             anim = new Display();  
             getContentPane().add(anim);
         setLocation(100, 100);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         pack();
         show();
         anim.startAnimation();
        
        draw();
        }
     
     public void draw() {
         anim.draw();
         anim.draw2();
     }

        public Dimension getPreferredSize(){
            return new Dimension(300, 300);
        }
}
class Display extends JPanel 
    
{
 
    public Dimension getPreferredSize(){
        return new Dimension(300, 300);
    }
    
        // bufor
    Image image;
        // wykreslacz ekranowy
    Graphics2D device;
        // wykreslacz bufora
    Graphics2D buffer;
    
        // przygotowanie wykreslaczy
        // uruchomienie watkow animacyjnych i zegara
    void startAnimation(){
        int width = getWidth();
        int height = getHeight();
        
        image = createImage(width, height);
        buffer = (Graphics2D)image.getGraphics();
        buffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                                RenderingHints.VALUE_ANTIALIAS_ON);
        device = (Graphics2D)getGraphics();
        device.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                                RenderingHints.VALUE_ANTIALIAS_ON);
       
    }
   

    public void draw()
    {
        buffer.drawString("xxxxx",30,30);
    }
    
    public void draw2()
    {
         device.drawImage(image, 0, 0, null);
       //  buffer.clearRect(0, 0, getWidth(), getHeight());
    }
  
} 

pozdrawiam

0

Ale który fragment kodu ma rysować? Rysuje metoda paintComponent(Graphics g) (ewentualnie paint). U Ciebie takich metod nie ma.

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