Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-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
Showing
27 changed files
with
454 additions
and
322 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
drools-examples/src/main/java/org/drools/games/GameFrame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
61
drools-examples/src/main/java/org/drools/games/GamePanel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.