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.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; private int delay; private boolean growing = false; private int count = 0; private boolean kill = false; private int pacmanx, pacmany; private int cycle; private boolean cycledir = true; Player player; ArrayList cells = new ArrayList(100000000); 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); //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(); pacmanx = 1050; pacmany = -20; } @Override public void actionPerformed(ActionEvent e) { 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(!kill); if (t != rec) { cells.add(rec); } //g2.drawImage(t.getImage(), t.getX(), t.getY(), this); } //growing = true; if(!kill) 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 (cells.size() > 10000000) { if(kill && (Math.abs(pacmanx + 40 - t.getX()) < 50) && (Math.abs(pacmany + 25 - t.getY()) < 75)) { cells.remove(t); } else g.drawImage(t.getImage(), t.getX(), t.getY(), this); //swingTimer.stop(); //} } if(kill) { //swingTimer.setDelay(100); g.setColor(Color.YELLOW); g.fillArc(pacmanx, pacmany, 100, 100, 220 - cycle * 2, 280 + cycle * 4); if (pacmanx > -100) { pacmanx -= 10; } else { pacmanx = 1050; pacmany += 100; } if (cycledir) cycle ++; else cycle --; if (cycle == 21) cycledir = false; if (cycle == 0) cycledir = true; } if(pacmany > 1050) { while (!cells.isEmpty()) { cells.remove(0); } kill = false; } jLabel1.setText("Delay: " + delay + "ms. Hours since start: " + count + ". Cell Count: " + cells.size()); System.out.println("Cell Count: " + cells.size()); //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(); jButton2 = 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); } }); 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); } }); 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, 593, Short.MAX_VALUE) .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.PREFERRED_SIZE, 0, Short.MAX_VALUE) .add(jLabel1) .add(jButton2)) .add(org.jdesktop.layout.GroupLayout.LEADING, speed, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(959, 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 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed {//GEN-HEADEREND:event_jButton2ActionPerformed // TODO add your handling code here: if(kill) { kill = false; swingTimer.setDelay(delay); } else { kill = true; swingTimer.setDelay(10); try { FileInputStream fileInputStream = new FileInputStream("lib/pacman.mp3"); Player player = new Player(fileInputStream); player.play(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JavaLayerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }//GEN-LAST:event_jButton2ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JSlider speed; // End of variables declaration//GEN-END:variables }