You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
12 KiB
Java

package cancergrowth;
/**
* File Name: CellPanel.java
* Created: Oct 02, 2017
* Author: Cole Deck and Phillip Augustynowicz
*
* Additional comments can be found in the document submitted by Phillip.
*/
import java.util.*; // Import libraries we need
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
public class CellPanel extends javax.swing.JPanel implements ActionListener, ChangeListener
{
private Timer swingTimer; // Declare and instantiate variables
private int delay;
private boolean growing = false;
private int count = 0;
private boolean kill = false;
private boolean dontkill = false;
private int pacmanx, pacmany;
private int cycle;
private boolean cycledir = true;
Player player;
private boolean age = true;
ArrayList<Cell> cells = new ArrayList<Cell>(100000000); // Limit us at 100,000,000 cells (if we ever get there)
public CellPanel()
{
super();
this.initComponents();
//Cell t1 = new Cell(300, 100, Color.RED);
//choice1.addItem(t1);
//cells.add(t1);
Cell t2 = new Cell(475, 475); // Create the initial cell (would be inserted into blood stream)
//choice1.addItem(t2);
cells.add(t2); // add our cell to the ArrayList of cells
//choice2.addItem(t2);
//choice1.setRenderer(new ComboBoxRenderer());
//choice2.setRenderer(new ComboBoxRenderer());
speed.setValue(250);
delay = speed.getValue();
swingTimer = new Timer(delay, this); // Use a timer to time the simulation
//swingTimer.start();
pacmanx = 1050;
pacmany = -20;
}
@Override
public void actionPerformed(ActionEvent e)
{ // Runs whenever the timer runs out (and starts over)
if (swingTimer.isRunning())
{
speed.setEnabled(true);
//go.setText("Play ");
//swingTimer.stop();
}
else
{
speed.setEnabled(false);
swingTimer.start();
}
for (int k = 0; k < cells.size(); k++)
{
Cell t = cells.get(k);
Cell rec = t.performTime(!kill); // Perform simulation for every cell using a loop
if (t != rec) {
cells.add(rec); // If performing the simulation results in a new daughter cell, add it to the list
}
if(t.splits > 10) { // IF the cels splits too much, it dies
cells.remove(t);
k--;
}
}
if (age && (cells.size() > 1000 || kill)) {
int y = (int) (Math.random() * (Math.abs(cells.size() - 10)) + 10);
int z = 0;
while (cells.size() > 100 && z < 10) {
cells.remove(y - z);
z ++;
}
}
//growing = true;
if(!kill || dontkill) count ++; // Count how many hours have passed
super.repaint(); // Redraw the screen so we can see the progress
}
@Override
public void stateChanged(ChangeEvent ce)
{
delay = speed.getValue();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int g1 = 0;
int s = 0;
int g2 = 0;
int m = 0;
int z = 0;
int x = cells.size() - 1;
for (int k = x; k >= 0; k--) // Use a loop to draw each cell
{
Cell t = cells.get(k);
/*if(k > cells.size() / 4) {
//g.setColor(t.getColor());
g.fillOval(t.getX(), t.getY(), 50, 50);
} else*/
//if (cells.size() > 10000000) { // we can simulate until 10000000 first to see what happens isntead of waiting
if(kill && !dontkill && (Math.abs(pacmanx + 40 - t.getX()) < 50) && (Math.abs(pacmany + 25 - t.getY()) < 75)) {
cells.remove(t); // If pacman is here, have him eat the cell
}
else g.drawImage(t.getImage(), t.getX(), t.getY(), this); // otherwise, draw the cell
if(t.currentStage == 0) { // record which phase the cell is in to calculate cyclin levels
g1 ++;
}
if(t.currentStage == 1) {
s ++;
}
if(t.currentStage == 2) {
g2 ++;
}
if(t.currentStage == 3) {
m ++;
}
//swingTimer.stop();
//}
}
if(kill && !dontkill) { // draw pacman if he needs to eat
g.setColor(Color.YELLOW);
g.fillArc(pacmanx, pacmany, 100, 100, 220 - cycle * 2, 280 + cycle * 4);
if (pacmanx > -100) {
pacmanx -= 10; // move pacman
} else {
pacmanx = 1050;
pacmany += 100;
}
if (cycledir) cycle ++;
else cycle --; // Open and close his mouth
if (cycle == 21) cycledir = false;
if (cycle == 0) cycledir = true;
}
if(pacmany > 1050) {
while (!cells.isEmpty()) {
cells.remove(0); // make sure we got rid of every cell with pacman
}
kill = false; // turn off pacman
}
g1 *= 99;
s *= 99;
g2 *= 99;
m *= 99;
jLabel1.setText("Delay: " + delay + "ms. Hours since start: " + count + ". Cell Count: " + cells.size());
// Calculate and print cyclin levels:
jLabel2.setText("Cyclin G1: " + g1 + ". Cyclin S: " + s + ". Cyclin G2: " + g2 + ". Cyclin M: " + m + ".");
System.out.println("Cell Count: " + cells.size());
}
/** This method is called from within the constructor to initialize the form.
*
* WARNING: Do NOT modify this code by hand. The content of this method is
* always regenerated by the Form Editor. Ignore this part! this just creates the buttons and slider, keep scrolling
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents()
{
speed = new javax.swing.JSlider();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
setToolTipText("");
speed.setMaximum(1000);
speed.setMinimum(1);
speed.setPaintLabels(true);
speed.setPaintTicks(true);
speed.setSnapToTicks(true);
speed.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseReleased(java.awt.event.MouseEvent evt)
{
speedMouseReleased(evt);
}
});
jLabel1.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
jLabel1.setText("Delay:");
jButton1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
jButton1.setText("Start");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton1ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
jButton2.setText("Kill");
jButton2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton2ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
jButton3.setText("Stable");
jButton3.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton3ActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
jLabel2.setText("jLabel2");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 789, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(0, 199, Short.MAX_VALUE))
.add(layout.createSequentialGroup()
.add(speed, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(95, 95, 95)
.add(jButton3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 63, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton1)))
.add(18, 18, 18))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jLabel1)
.add(jButton2)
.add(jButton3))
.add(org.jdesktop.layout.GroupLayout.LEADING, speed, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 934, Short.MAX_VALUE)
.add(jLabel2)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void speedMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_speedMouseReleased
{//GEN-HEADEREND:event_speedMouseReleased
delay = speed.getValue(); // if the slider bar is moved, update the timing
//swingTimer.setDelay(1000);
super.repaint();
}//GEN-LAST:event_speedMouseReleased
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
{//GEN-HEADEREND:event_jButton1ActionPerformed
swingTimer.setDelay(delay); // When we press start, start the simulation
swingTimer.start();
kill = false;
dontkill = false;
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed
{//GEN-HEADEREND:event_jButton2ActionPerformed
if(dontkill) { // if we are currently in "stable" mode, then kill them
dontkill = false;
kill = true;
swingTimer.setDelay(10);
try {
FileInputStream fileInputStream = new FileInputStream("lib/pacman.mp3");
Player player = new Player(fileInputStream); // play pacman sound effect
player.play();
} catch (FileNotFoundException e) {
e.printStackTrace(); // catch any errors with the mp3
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
else if(kill) { // if we press the kill button while growing normally, then begin the killing
kill = false;
swingTimer.setDelay(delay);
}
else {
kill = true;
swingTimer.setDelay(10);
try {
FileInputStream fileInputStream = new FileInputStream("lib/pacman.mp3");
Player player = new Player(fileInputStream); // play pacman sound effect
player.play();
} catch (FileNotFoundException e) {
e.printStackTrace(); // catch any errors with the mp3
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton3ActionPerformed
{//GEN-HEADEREND:event_jButton3ActionPerformed
// enter stable mode
kill = true; // bad variable names, we're not actually killing them
dontkill = true; // remind the program NOT to kill them even though kill == true
swingTimer.setDelay(100);
}//GEN-LAST:event_jButton3ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JSlider speed;
// End of variables declaration//GEN-END:variables
}