Using processing PGraphics in Java AWT objects.

Hello,

I am working on a project of game framework that works with 'pure' Java AWT objects and I would like to use processing PGraphics instead of java.awt.Graphics.

Basically I am using awt.Frame (not JFrame) with awt.Canvas, I use active rendering with separate 'render thread' and own BufferStrategy is created using Canvas. Point of using AWT instead of PApplet is to keep control over how my main loop is handled.

However, I would like to use PGraphics as main way of rendering objects :) BUT PGraphics is not derived from java.awt.Graphics :( ... Below is a bit of code showing how my loop works:

public void run() {
  while(running) {
       loop();
  } 
}

void loop() {
  processInput(); // handle mouse and keyboard
  updateObjects(); // update objects 
  renderFrame(); // render objects on screen
}

void renderFrame() {
        do {
            do {
                Graphics g = null; // can i use PGraphics instead?? how?
                try {
                    g = strategy.getDrawGraphics();
                    g.clearRect(0,0,getWidth(), getHeight());
                    render(g);
                } finally {
                    if(g != null) {
                        g.dispose();
                    }
                }
            } while(strategy.contentsRestored());
            strategy.show();
        } while(strategy.contentsLost());
    }

protected abstract void render(Graphics g) ; // used in subclasses as processing draw() equivalent.

Can you please advise how I could use processing PGraphics here? or perhaps embedding PApplet instead of Canvas would be better solution??

Many thanks for your help.

Tagged:
Sign In or Register to comment.