0

Pre note: I can not include my engine right now. I have my own that I developed that works, and I've used some fr tutorials. The draw method is being invoked 60 times a second.

So I have my own Java Jframe (I created one and set its name and that's all) and I use frame.getGraphics() to get the Graphics object.

In my method that's being called 60 times persecond I increment an int which I then use to draw an image. Basically each second I increment the x value of a quick graphics.fillRect().

The rectangle is drawn but it is very laggy and not smooth.

Are there any extra steps that I need to do to make sure I have a smooth jFrame that can draw many images per second?

tshepang
  • 12,111
  • 21
  • 91
  • 136
SirTrashyton
  • 173
  • 2
  • 3
  • 12

1 Answers1

0

I recommend adding a canvas to your frame, then using the canvas's BufferStrategy.

class Game extends Canvas {

     public static void main(String[] args) {
          Game game = new Game()
          //init frame and add canvas

          while(true) {
               render();
          }
     }

     public void render() {
          BufferStrategy bs = getBufferStrategy();
          if(bs == null) {
               createBufferStrategy(3); //triple buffering
               return;
          }

          Graphics g = bs.getDrawGraphics();

          //do what you want with Graphics g

         g.dispose();
         bs.show();
     }
}
Vince
  • 14,470
  • 7
  • 39
  • 84
  • I understand this but how do I connect the frame to the bs? Basically you create a new bs but how it's graphics know it's the frames graphics? – SirTrashyton Apr 21 '14 at 05:59
  • It's the graphics from your video card. What are you talking about? When you have a frame, there are 2 parts: 1. The frame border which contains the title, minimize button and X button 2. The drawable are where you put components and draw images. Instead of using the frame's contentpane to handle the graphics (not even sure if it supports buffer strategy), we are using a lightweight drawable area that focuses purely on graphical painting rather than compoments. We add the canvas to the frame, and use the canvas to draw on. What's the issue? – Vince Apr 21 '14 at 15:48