/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jgrow;
import java.awt.Image;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.MemoryImageSource;
/**
*
* @author maryam
*/
public class AnimationController implements Runnable {
private MemoryImageSource source;
private PanelSimulation simulation;
public Simulator simulator;
public InfoTiles tiles;
public InfoBonds bonds;
public PanelInformation inf;
public int boardSize = 256;
public boolean isRunning, isPaused, isStep = false;
private int animationDelay = 12, pauseDelay = 20;
public int[] state;
public Image animationImage;
private ColorModel cm = new DirectColorModel(24, 0x0000FF, 0x00FF00, 0xFF0000);
public AnimationController(PanelSimulation simPanel, InfoTiles ts, InfoBonds bs, PanelInformation inff) {
state = new int[boardSize * boardSize];
isRunning = false;
isPaused = false;
isStep = false;
simulation = simPanel;
tiles = ts;
bonds = bs;
inf = inff;
source = new MemoryImageSource(boardSize, boardSize, cm, state, 0, boardSize);
source.setAnimated(true);
source.setFullBufferUpdates(true);
animationImage = simulation.createImage(source);
for (int i = 0; i < boardSize * boardSize; i++) {
state[i] = 0;
}
source.newPixels();
}
public void resize(int b) {
if (isRunning) {
return;
}
boardSize = b;
state = new int[b * b];
}
public void updateVars() {
if (simulator != null) {
simulator.updateVars();
}
}
public void setStability(boolean st) {
if (simulator != null) {
simulator.setStability(st);
}
source.newPixels();
}
public String infoBoard(int x, int y) {
if (simulator != null) {
return simulator.infoBoard(x, y);
}
return "";
}
public void pauseSimulation() {
isPaused = true;
}
public void stepSimulation() {
isStep = true;
}
public void resumeSimulation() {
isPaused = false;
}
public void stopSimulation() {
isRunning = false;
}
public void placeSeedTile(int x, int y, int se) {
simulator.placeSeedTile(x, y, se);
source.newPixels(0, 0, boardSize, boardSize);
}
public void removeSeedTile(int x, int y) {
simulator.removeSeedTile(x, y);
source.newPixels(0, 0, boardSize, boardSize);
}
public boolean hasTile(int x, int y) {
if (simulator != null) {
return simulator.hasTile(x, y);
}
return false;
}
public void runSimulation() {
if(isRunning && isPaused) {
isPaused = false;
return;
}
source = new MemoryImageSource(boardSize, boardSize, cm, state, 0, boardSize);
source.setAnimated(true);
source.setFullBufferUpdates(true);
animationImage = simulation.createImage(source);
for (int i = 0; i < boardSize * boardSize; i++) {
state[i] = 0;
}
source.newPixels();
isRunning = true;
isPaused = false;
isStep = false;
simulator = new Simulator(boardSize, state, tiles, bonds, inf); // <- he needs lots of info
Thread me = new Thread(this);
me.start();
}
public void run() {
Thread me = Thread.currentThread();
me.setPriority(Thread.MIN_PRIORITY);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
return;
}
while (isRunning) {
if (isPaused) { //&& !isStep) {
try {
Thread.sleep(pauseDelay);
} catch (InterruptedException e) {
return;
}
} else {
//if(isStep) isStep = false;
try {
Thread.sleep(animationDelay);
} catch (InterruptedException e) {
return;
}
simulator.nextRound();
source.newPixels();
//simulation.roundCompleted();
}
}
}
}
# |
Change |
User |
Description |
Committed |
|
#2
|
7857 |
renat_bekbolatov |
sync |
|
|
#1
|
7851 |
renat_bekbolatov |
new files |
|
|