add cellpanel comments

master
shark 6 years ago
parent 2d7bfd6344
commit c57fe1d821

@ -5,7 +5,7 @@ package cancergrowth;
* Created: Oct 02, 2017
* Author:
*/
import java.util.*;
import java.util.*; // Import libraries we need
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
@ -15,13 +15,12 @@ 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;
private Timer swingTimer; // Declare and instantiate variables
private int delay;
private boolean growing = false;
private int count = 0;
@ -30,7 +29,8 @@ public class CellPanel extends javax.swing.JPanel implements ActionListener, Cha
private int cycle;
private boolean cycledir = true;
Player player;
ArrayList<Cell> cells = new ArrayList<Cell>(100000000);
ArrayList<Cell> cells = new ArrayList<Cell>(100000000); // Limit us at 10,000,000 cells
public CellPanel()
{
@ -41,16 +41,16 @@ public class CellPanel extends javax.swing.JPanel implements ActionListener, Cha
//Cell t1 = new Cell(300, 100, Color.RED);
//choice1.addItem(t1);
//cells.add(t1);
Cell t2 = new Cell(475, 475);
Cell t2 = new Cell(475, 475); // Create the initial cell (would be inserted into blood stream)
//choice1.addItem(t2);
cells.add(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);
swingTimer = new Timer(delay, this); // Use a timer to time the simulation
//swingTimer.start();
pacmanx = 1050;
pacmany = -20;
@ -59,7 +59,7 @@ public class CellPanel extends javax.swing.JPanel implements ActionListener, Cha
@Override
public void actionPerformed(ActionEvent e)
{
{ // Runs whenever the timer runs out (and starts over)
if (swingTimer.isRunning())
{
speed.setEnabled(true);
@ -72,21 +72,17 @@ public class CellPanel extends javax.swing.JPanel implements ActionListener, Cha
swingTimer.start();
}
//canvas = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);
//g2 = canvas.getGraphics();
for (int k = 0; k < cells.size(); k++)
{
Cell t = cells.get(k);
Cell rec = t.performTime(!kill);
Cell rec = t.performTime(!kill); // Perform simulation for every cell using a loop
if (t != rec) {
cells.add(rec);
cells.add(rec); // If performing the simulation results in a new daughter cell, add it to the list
}
//g2.drawImage(t.getImage(), t.getX(), t.getY(), this);
}
//growing = true;
if(!kill) count ++;
//System.out.println("ACTION PERFORMED");
super.repaint();
if(!kill) count ++; // Count how many hours have passed
super.repaint(); // Redraw the screen so we can see the progress
}
@Override
@ -99,47 +95,44 @@ public class CellPanel extends javax.swing.JPanel implements ActionListener, Cha
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//g.setColor(Color.BLACK);
int x = cells.size() - 1;
//g.drawImage(canvas, 0, 0, this);
for (int k = x; k >= 0; k--)
{
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) {
//if (cells.size() > 10000000) { // we can simulate until 10000000 first to see what happens isntead of waiting
if(kill && (Math.abs(pacmanx + 40 - t.getX()) < 50) && (Math.abs(pacmany + 25 - t.getY()) < 75)) {
cells.remove(t);
cells.remove(t); // If pacman is here, have him eat the cell
}
else g.drawImage(t.getImage(), t.getX(), t.getY(), this);
else g.drawImage(t.getImage(), t.getX(), t.getY(), this); // otherwise, draw the cell
//swingTimer.stop();
//}
}
if(kill) {
//swingTimer.setDelay(100);
if(kill) { // draw pacman if he needs to be drawn
g.setColor(Color.YELLOW);
g.fillArc(pacmanx, pacmany, 100, 100, 220 - cycle * 2, 280 + cycle * 4);
g.fillArc(pacmanx, pacmany, 100, 100, 220 - cycle * 2, 280 + cycle * 4);
if (pacmanx > -100) {
pacmanx -= 10;
pacmanx -= 10; // move pacman
} else {
pacmanx = 1050;
pacmany += 100;
}
if (cycledir) cycle ++;
else 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);
cells.remove(0); // make sure we got rid of every cell with pacman
}
kill = false;
kill = false; // turn off pacman
}
jLabel1.setText("Delay: " + delay + "ms. Hours since start: " + count + ". Cell Count: " + cells.size());
System.out.println("Cell Count: " + cells.size());
@ -151,7 +144,7 @@ public class CellPanel extends javax.swing.JPanel implements ActionListener, Cha
/** 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.
* always regenerated by the Form Editor. Ignore this part! this just creates the buttons and slider
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
@ -230,23 +223,20 @@ public class CellPanel extends javax.swing.JPanel implements ActionListener, Cha
private void speedMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_speedMouseReleased
{//GEN-HEADEREND:event_speedMouseReleased
// TODO add your handling code here:
delay = speed.getValue();
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
// TODO add your handling code here:
swingTimer.setDelay(delay);
swingTimer.setDelay(delay); // When we press start, start the simulation
swingTimer.start();
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed
{//GEN-HEADEREND:event_jButton2ActionPerformed
// TODO add your handling code here:
if(kill) {
if(kill) { // if we press the kill button, then begin the killing
kill = false;
swingTimer.setDelay(delay);
}
@ -256,13 +246,11 @@ public class CellPanel extends javax.swing.JPanel implements ActionListener, Cha
swingTimer.setDelay(10);
try {
FileInputStream fileInputStream = new FileInputStream("lib/pacman.mp3");
Player player = new Player(fileInputStream);
Player player = new Player(fileInputStream); // play pacman sound effect
player.play();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.printStackTrace(); // catch any errors with the mp3
} catch (JavaLayerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Loading…
Cancel
Save