BufferStrategy im JApplet

System.exit(0)

Aktives Mitglied
Hallo,

ich bastele gerade an einem Spiel.
Da es auf Grund der vielen Sprites zum Flackern kommt, wollte ich mit BufferStrategy ein Page-Flipping machen.

Nur leider scheinen meine Bemühungen nicht von Erfolg gekrönt zu sein.

Der folgende Code ist mein Testcode.
Er erzeugt aber keine neuen Bilder und ich weiß nicht mehr weiter, wo der Fehler noch liegen könnte.

Vielleicht kann mir ja jemand auf die Sprünge helfen?

Danke

mfg

System.exit(0)

Java:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package applet_test;

import java.applet.Applet;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;



/**
 *
 * @author Markus
 */
public class MyApplet extends JApplet implements Runnable

{

    private Canvas DrawArea;
    BufferStrategy BF;
    private int index;

    public void init()
    {

        this.setSize(800,600);
        this.setVisible(true);
        DrawArea = new Canvas();
        DrawArea.setSize(800,600);
        DrawArea.setLocation(0,0);
        DrawArea.setVisible(true);
        this.add(DrawArea);
        
        DrawArea.createBufferStrategy(2);

        BF = DrawArea.getBufferStrategy();
        index = 0;
        Thread myT = new Thread(this);
        myT.start();
    }


    public void paint (Graphics G)
    {
        drawStuff(G);
        
    }

    public void drawStuff(Graphics DA)
    {
        System.out.println("bla");
        DrawArea.setBackground(Color.black);
        switch(index % 4)
        {
            case 0: DA.setColor(Color.yellow);
            case 1: DA.setColor(Color.green);
            case 2: DA.setColor(Color.red);
            case 3: DA.setColor(Color.blue);
        }
        DA.fillRect(0,0,800,600);
        for (int i = 0; i <50; i ++)
        {
            for (int j = 0; j < 50; i++)
            DA.fillOval(i*10, j*10, 6,    3);
        }
        BF.show();

    }

    public void run() 
    {
        
        index = 0;
        for (int i= 0;i < 20; i++)
        {
             System.out.println("test" + i);
            index ++;
            Graphics G = BF.getDrawGraphics();
            drawStuff(G);
            System.out.println("test" + i);
            try {
                Thread.sleep(250);
            } catch (InterruptedException ex) {
                Logger.getLogger(MyApplet.class.getName()).log(Level.SEVERE, null, ex);
            }
     
        }
    }


}
 

sambalmueslie

Bekanntes Mitglied
Wie wäre es mit dem Copy-Paste Fehler in Zeile 72???

so hier klappt es bestimmt viel besser :)

Java:
for (int i = 0; i <50; i ++)
{
            for (int j = 0; j < 50; j++)
            DA.fillOval(i*10, j*10, 6,    3);
}

Gruß Oli
 

sambalmueslie

Bekanntes Mitglied
so und dann die restlichen Fehler :)


1) Das verwenden von break in einem Switch soll in manchen Fällen wunder wirken :)

Java:
public void drawStuff(final Graphics DA) {
		DrawArea.setBackground(Color.black);
		switch (index % 4) {
		case 0:
			DA.setColor(Color.yellow);
			break;
		case 1:
			DA.setColor(Color.green);
			break;
		case 2:
			DA.setColor(Color.red);
			break;
		case 3:
			DA.setColor(Color.blue);
			break;
		}
		DA.fillRect(0, 0, 800, 600);
		DA.setColor(Color.black);
		for (int i = 0; i < 50; i++) {
			for (int j = 0; j < 50; j++) {
				DA.fillOval(i * 10, j * 10, 6, 3);
			}
		}

	}


Kein Fehler aber einfach mal an die Doku ( BufferStrategy (Java Platform SE 6) ) gehalten :
Java:
public void run() {
		index = 0;
		for (int i = 0; i < 20; i++) {
			index++;
			// Render single frame
			do {
				// The following loop ensures that the contents of the drawing
				// buffer
				// are consistent in case the underlying surface was recreated
				do {
					// Get a new graphics context every time through the loop
					// to make sure the strategy is validated
					final Graphics graphics = BF.getDrawGraphics();

					// Render to graphics
					drawStuff(graphics);

					// Dispose the graphics
					graphics.dispose();

					// Repeat the rendering if the drawing buffer contents
					// were restored
				} while (BF.contentsRestored());

				// Display the buffer
				BF.show();

				// Repeat the rendering if the drawing buffer was lost
			} while (BF.contentsLost());
			try {
				Thread.sleep(250);
			} catch (final InterruptedException ex) {
				Logger.getLogger(MyApplet.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}

	}

So jetzt macht es Augenkrebs, das wolltest du doch sicher haben :)

Gruß Oli
 

System.exit(0)

Aktives Mitglied
Hallo Sambalmuesli,

vielen Dank.

Copy and Paste ist manchmal ein Killer :) Ich glaube, ich werde auf i und k umsteigen. Die kann man nicht so leicht verwechseln.

Genau den Augenkrebs wollte ich, um zu testen, ob es funktioniert. Jetzt wird das Ganze natürlich sinnvoll eingesetzt werden.
 

Ähnliche Java Themen

Neue Themen


Oben