package cancergrowth; /** * File Name: CellPanel.java * Created: Oct 02, 2017 * Author: */ import java.util.*; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import javax.swing.Timer; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class CellPanel extends javax.swing.JPanel implements ActionListener, ChangeListener { private Timer swingTimer; //private JButton go; //private JSlider speed; private int delay; private boolean growing = false; int count = 0; long time0 = System.nanoTime(); long time1 = time0; private boolean draw = false; ArrayList cells = new ArrayList(); Graphics g2; Image canvas; 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, new Color(255, 0, 0, 100)); //choice1.addItem(t2); cells.add(t2); //choice2.addItem(t2); //choice1.setRenderer(new ComboBoxRenderer()); //choice2.setRenderer(new ComboBoxRenderer()); speed.setValue(250); delay = speed.getValue(); swingTimer = new Timer(delay, this); //swingTimer.start(); } @Override public void actionPerformed(ActionEvent e) { time1 = System.nanoTime() - time0; time0 = System.nanoTime(); if (swingTimer.isRunning()) { speed.setEnabled(true); //go.setText("Play "); //swingTimer.stop(); } else { speed.setEnabled(false); 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(); if (t != rec) { cells.add(rec); } //g2.drawImage(t.getImage(), t.getX(), t.getY(), this); } growing = true; count ++; //System.out.println("ACTION PERFORMED"); super.repaint(); } @Override public void stateChanged(ChangeEvent ce) { delay = speed.getValue(); } @Override 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--) { Cell t = cells.get(k); /*if(k > cells.size() / 4) { g.setColor(t.getColor()); g.fillOval(t.getX(), t.getY(), 50, 50); } else*/ //if (draw || cells.size() > 100000000) { g.drawImage(t.getImage(), t.getX(), t.getY(), this); //swingTimer.stop(); //} } jLabel1.setText("Delay: " + delay + "ms. Hours since start: " + count + ". Cell Count: " + cells.size()); //System.out.println("Speed: " + (int) (delay / (time1 / 100000000.0)) + "%"); growing = false; } /** 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. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { speed = new javax.swing.JSlider(); jLabel1 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); 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); } }); 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(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) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 662, Short.MAX_VALUE) .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.PREFERRED_SIZE, 0, Short.MAX_VALUE) .add(jLabel1)) .add(org.jdesktop.layout.GroupLayout.LEADING, speed, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(967, Short.MAX_VALUE)) ); }// //GEN-END:initComponents 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(); //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.start(); }//GEN-LAST:event_jButton1ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JSlider speed; // End of variables declaration//GEN-END:variables }