Skip to content

Commit

Permalink
DROOLS-567 Improve Examples
Browse files Browse the repository at this point in the history
-Change back to use BufferedImage.
-Improve the synchronisation of paint().
-Make pong, wumpus and invaders all share more code.
-Update TextAdventure for lock handling, attacking and general improvements.
  • Loading branch information
Mark Proctor committed Jul 28, 2014
1 parent f188657 commit 3dd405f
Show file tree
Hide file tree
Showing 27 changed files with 454 additions and 322 deletions.
39 changes: 39 additions & 0 deletions drools-examples/src/main/java/org/drools/games/GameFrame.java
@@ -0,0 +1,39 @@
package org.drools.games;

import javax.swing.*;
import java.awt.*;

public class GameFrame extends JFrame {
private final Object redrawLock = new Object();

public GameFrame() throws HeadlessException {
}

public GameFrame(String title) throws HeadlessException {
super(title);
}

@Override
public void paint(Graphics g) {
// this will iterate the children, calling paintComponent
super.paint(g);
Toolkit.getDefaultToolkit().sync();
resume(); // all the children are redrawn, so resume
}

public void waitForPaint() {
try {
synchronized (redrawLock) {
repaint();
redrawLock.wait();
}
} catch (InterruptedException e) {
}
}

private void resume() {
synchronized (redrawLock) {
redrawLock.notify();
}
}
}
61 changes: 61 additions & 0 deletions drools-examples/src/main/java/org/drools/games/GamePanel.java
@@ -0,0 +1,61 @@
package org.drools.games;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class GamePanel extends JPanel {
private BufferedImage backbuffer;
private Graphics2D g2d;
private String name;
private Color color;

public GamePanel(String name, Color color) {
this.name = name;
this.color = color;
}


public BufferedImage getBufferedImage() {
if (backbuffer == null) {
backbuffer = new BufferedImage(getWidth(), getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g = getGraphics2D();
g.setColor( color ); // background
g.fillRect( 0, 0, getWidth(), getHeight() );
disposeGraphics2D();
}
return backbuffer;
}

public Graphics2D getGraphics2D() {
if ( g2d == null ) {
g2d = (Graphics2D) backbuffer.getGraphics();
}
return g2d;
}

public void disposeGraphics2D() {
if ( g2d != null ) {
g2d.dispose();
g2d = null;
}
}

private long time;
private int frameCount;
public synchronized void paintComponent(Graphics g) {
// track approx frames per second
long currentTime = System.currentTimeMillis();
if ( currentTime-time >= 10000 || time == 0) {
System.out.println( "fps(" + name + ") :" + (frameCount/10) );
frameCount = 0;
time = currentTime;
}
frameCount++;

// paint the buffered image to the graphics
g.drawImage(backbuffer, 0, 0, this);
Toolkit.getDefaultToolkit().sync();
}
}
127 changes: 72 additions & 55 deletions drools-examples/src/main/java/org/drools/games/GameUI.java
Expand Up @@ -12,8 +12,8 @@

public class GameUI {
private GameConfiguration conf;

private Canvas canvas;
private GameFrame frame;
private MyJPanel panel;

KieSession ksession;

Expand All @@ -22,69 +22,42 @@ public GameUI(KieSession ksession, GameConfiguration conf) {
this.conf = conf;
}

public Graphics getGraphics() {
return canvas.getBufferStrategy().getDrawGraphics();
}

public Canvas getCanvas() {
return canvas;
}

/**
* Initialize the contents of the frame.
*/
public void init() {
canvas = new Canvas();
canvas.setBackground(Color.BLACK);
canvas.setSize(new Dimension(conf.getWindowWidth(), conf.getWindowHeight()));

KeyListener klistener = new GameKeyListener( ksession.getEntryPoint( "KeyPressedStream" ), ksession.getEntryPoint( "KeyReleasedStream" ) );
canvas.addKeyListener(klistener);

canvas.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
canvas.requestFocus();
}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {
frame = new GameFrame();
frame.setDefaultCloseOperation(conf.isExitOnClose() ? JFrame.EXIT_ON_CLOSE : JFrame.DISPOSE_ON_CLOSE);
frame.setResizable( false );
frame.setBackground(Color.BLACK);
frame.getContentPane().setBackground(Color.BLACK);
frame.setSize(new Dimension(conf.getWindowWidth(), conf.getWindowHeight()));

}
panel = new MyJPanel("", Color.BLACK);
frame.add( panel );
panel.init();
panel.getBufferedImage();

@Override
public void mouseExited(MouseEvent e) {
frame.setLocationRelativeTo(null); // Center in screen
frame.pack();
frame.setVisible( true );
}

}
});

public JPanel getCanvas() {
return panel;
}

JFrame frame = new JFrame();
frame.setResizable(false);
frame.setDefaultCloseOperation(conf.isExitOnClose() ? JFrame.EXIT_ON_CLOSE : JFrame.DISPOSE_ON_CLOSE);
frame.setSize(new Dimension(conf.getWindowWidth(), conf.getWindowHeight()));
frame.setBackground(Color.BLACK);
frame.add(canvas);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null); // Center in screen
public Graphics getGraphics() {
return panel.getGraphics2D();
}

canvas.createBufferStrategy(2);
canvas.requestFocus();
public void repaint() {
panel.disposeGraphics2D();
frame.waitForPaint();
}

public static class GameKeyListener implements KeyListener {

EntryPoint keyPressedEntryPoint;
EntryPoint keyReleasedEntryPoint;

Expand All @@ -106,8 +79,52 @@ public void keyReleased(KeyEvent e) {
}
}

public synchronized void show() {
canvas.getBufferStrategy().show();
Toolkit.getDefaultToolkit().sync();
public class MyJPanel extends GamePanel {

public MyJPanel(String name, Color color) {
super(name, color);
}

public void init() {
KeyListener klistener = new GameKeyListener( ksession.getEntryPoint( "KeyPressedStream" ), ksession.getEntryPoint( "KeyReleasedStream" ) );
addKeyListener(klistener);

addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
requestFocus();
}

@Override
public void mousePressed(MouseEvent e) {

}

@Override
public void mouseReleased(MouseEvent e) {

}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}
});

setPreferredSize(new Dimension(conf.getWindowWidth(), conf.getWindowHeight()));
setSize(new Dimension(conf.getWindowWidth(), conf.getWindowHeight()));
setBackground(Color.BLACK);
setDoubleBuffered(true);


setFocusable(true);
requestFocus();
}

}
}

0 comments on commit 3dd405f

Please sign in to comment.