vendredi 24 avril 2015

Double Buffering angewendet?

Also meine Frage ist, ob hier das Double Buffering angewendet wurde?

Java Code:

  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.Image;
  6. import java.awt.RenderingHints;
  7. import java.awt.event.KeyEvent;
  8.  
  9. import javax.swing.JPanel;
  10.  
  11.  
  12. public class Fenster extends JPanel implements Runnable
  13. {
  14. // Variablen
  15.  
  16. public static final int BREITE = 800;
  17. public static final int HÖHE = 400;
  18.  
  19. private static float timeSinceLastFrame;
  20. private static int FPS = 30;
  21.  
  22. private boolean running = false;
  23. private Thread thread;
  24.  
  25. private Image image;
  26. private Graphics2D g;
  27.  
  28. Spieler spieler = new Spieler(BREITE, HÖHE);
  29.  
  30. // Konstruktor
  31.  
  32. public Fenster()
  33. {
  34. super();
  35. setPreferredSize(new Dimension(BREITE, HÖHE));
  36. setFocusable(true);
  37. requestFocus();
  38.  
  39.  
  40. }
  41.  
  42. public void addNotify()
  43. {
  44. super.addNotify();
  45.  
  46. if(thread == null)
  47. {
  48. thread = new Thread(this);
  49. }
  50.  
  51. thread.start();
  52. System.out.println("Thread gestartet");
  53.  
  54. addKeyListener(new Tastatur());
  55. }
  56.  
  57. // Hauptspiel
  58.  
  59. @Override
  60. public void run()
  61. {
  62.  
  63. running = true;
  64. long lastFrame = System.currentTimeMillis();
  65.  
  66. while(running)
  67. {
  68. long thisFrame = System.currentTimeMillis();
  69. timeSinceLastFrame = (float) (thisFrame - lastFrame)/1000;
  70. lastFrame = thisFrame;
  71.  
  72. Update();
  73. Zeichnen();
  74. Rendern();
  75.  
  76. try
  77. {
  78. Thread.sleep(1000 / FPS);
  79. }
  80. {
  81. e.printStackTrace();
  82. }
  83.  
  84. }
  85.  
  86. System.exit(0);
  87.  
  88. } // Hauptspiel Ende
  89.  
  90.  
  91. private void Zeichnen()
  92. {
  93. image = createImage(BREITE, HÖHE);
  94. g = (Graphics2D) image.getGraphics();
  95.  
  96. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  97.  
  98. // Zeichnen
  99.  
  100. g.setColor(Color.GREEN);
  101. g.fillRect(0, 0, BREITE, HÖHE);
  102.  
  103. spieler.Zeichnen(g);
  104. } // Zeichnen Ende
  105.  
  106.  
  107.  
  108. private void Rendern()
  109. {
  110. Graphics g2 = this.getGraphics();
  111. g2.drawImage(image, 0, 0, null);
  112. g2.dispose();
  113. }
  114.  
  115. private void Update()
  116. {
  117. if(Tastatur.Knopfgedrückt(KeyEvent.VK_ESCAPE))running = false;
  118.  
  119. spieler.Update(timeSinceLastFrame);
  120. }
  121.  
  122. }


Double Buffering angewendet?

0 commentaires:

Enregistrer un commentaire