Advertisement
simonbt

Cookie Clicker Main Class

Feb 6th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.06 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.EventQueue;
  3. import java.awt.Font;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.IOException;
  7. import java.net.CookieManager;
  8. import java.util.Random;
  9. import java.util.Timer;
  10. import java.util.TimerTask;
  11.  
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.SwingConstants;
  19.  
  20. import qc.simonbt.cookie.cookieManager;
  21. import qc.simonbt.open.openFile;
  22. import qc.simonbt.save.SaveData;
  23.  
  24. public class CookieClicker {
  25.  
  26.     private JFrame frame;
  27.     private int cookie;
  28.     protected String open;
  29.     public static int xSize;
  30.     private static  int xCoord;
  31.     private int multipliers = 1;
  32.    
  33.  
  34.     /**
  35.      * Launch the application.
  36.      */
  37.     public static void main(String[] args) {
  38.         EventQueue.invokeLater(new Runnable() {
  39.             public void run() {
  40.                 new cookieManager();
  41.                 try {
  42.                     CookieClicker window = new CookieClicker();
  43.                     window.frame.setVisible(true);
  44.                 } catch (Exception e) {
  45.                     e.printStackTrace();
  46.                 }
  47.             }
  48.         });
  49.     }
  50.  
  51.     /**
  52.      * Create the application.
  53.      */
  54.     public CookieClicker() {
  55.         initialize();
  56.     }
  57.  
  58.     /**
  59.      * Initialize the contents of the frame.
  60.      */
  61.     private void initialize() {
  62.         frame = new JFrame();
  63.         frame.getContentPane().setBackground(new Color(189, 183, 107));
  64.         frame.setBounds(100, 100, 629, 465);
  65.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66.         frame.getContentPane().setLayout(null);
  67.         frame.setResizable(false);
  68.         xCoord = (int) frame.getAlignmentX();
  69.         xSize = (int) frame.getWidth();
  70.        
  71.        
  72.        
  73.         // Title Label
  74.         JLabel lblClickerHero = new JLabel("Cookie Clicker");
  75.         lblClickerHero.setFont(new Font("Nanum Brush Script", Font.BOLD, 36));
  76.         lblClickerHero.setForeground(new Color(128, 0, 0));
  77.         lblClickerHero.setHorizontalAlignment(SwingConstants.CENTER);
  78.         lblClickerHero.setBounds(201, 6, 198, 40);
  79.         frame.getContentPane().add(lblClickerHero);
  80.        
  81.         // Information Button
  82.         JButton btnNewButton = new JButton("?");
  83.         btnNewButton.setBounds(607, 417, 16, 20);
  84.         frame.getContentPane().add(btnNewButton);
  85.        
  86.        
  87.        
  88.         // Cookie Counter Label
  89.         JLabel lblCookie = new JLabel("Cookies: 0");
  90.         lblCookie.setForeground(new Color(128, 0, 0));
  91.         lblCookie.setFont(new Font("Lucida Grande", Font.BOLD, 18));
  92.         lblCookie.setBounds(201, 87, 198, 29);
  93.         frame.getContentPane().add(lblCookie);
  94.         // Create the Main Menu Frame
  95.         // Buy Button
  96.         // Buy Menu Button
  97.                 JButton btnBuyMenu = new JButton("Buy Menu");
  98.                 btnBuyMenu.setBounds(142, 413, 328, 29);
  99.                 frame.getContentPane().add(btnBuyMenu);
  100.                 btnBuyMenu.addActionListener(new ActionListener() {
  101.                     int x = xCoord + xSize;
  102.                     public void actionPerformed (ActionEvent e) {  
  103.                         JFrame frame2 = new JFrame("Buy Menu");
  104.                         frame2.setVisible(true);
  105.                         frame2.getContentPane().setLayout(null);
  106.                         frame2.setBounds(x, 100, 200, 400);
  107.                         JLabel label = new JLabel("Buy Menu");
  108.                         JPanel panel = new JPanel();
  109.                         JLabel lab = new JLabel("Auto click");
  110.                         lab.setBounds(5, 20, 90, 30);
  111.                         JButton btn1 = new JButton("200");
  112.                         btn1.setBounds(100, 20, 95, 30);
  113.                         btn1.addActionListener(new ActionListener() {
  114.                             // Button auto click 200 cookies
  115.                             @Override
  116.                             public void actionPerformed(ActionEvent e) {
  117.                                 if(!(cookie < 200 )){
  118.                                     cookie = new cookieManager().cookieSub(cookie, 200);
  119.                                     lblCookie.setText( "Cookies: "+ cookie);
  120.                                     Timer t = new Timer();
  121.                                     t.schedule(new TimerTask() {
  122.                                         @Override
  123.                                         public void run() {
  124.                                            cookie = new cookieManager().cookieAdd(cookie, 1);
  125.                                            lblCookie.setText("Cookies: " +cookie);
  126.                                         }
  127.                                     }, 0, 1000);
  128.                                 }else{
  129.                                         final ImageIcon icon = new ImageIcon("/Users/sbtherrien/Desktop/cookie2.png");
  130.                                         JOptionPane.showMessageDialog(frame, "You need more money to buy this upgrade!", "Not enought cookie", 1, icon);
  131.                                         System.out.println("Error: Not enought money");
  132.                                     return;
  133.                                 }
  134.                                
  135.                                
  136.                             }
  137.                         });
  138.                        
  139.                         // Boost x2 Button
  140.                         JButton btnx2 = new JButton("100");
  141.                         btnx2.setBounds(100, 55, 95, 30);
  142.                         btnx2.addActionListener(new ActionListener() {
  143.                             // Button x2 Multipliers
  144.                             @Override
  145.                             public void actionPerformed(ActionEvent e) {
  146.                                 if(!(cookie < 100)){
  147.                                 cookie = new cookieManager().cookieSub(cookie, 100);
  148.                                 multipliers = 2;
  149.                                 }else{
  150.                                     final ImageIcon icon = new ImageIcon("/Users/sbtherrien/Desktop/cookie2.png");
  151.                                     JOptionPane.showMessageDialog(frame, "You need more money to buy this upgrade!", "Not enought cookie", 1, icon);
  152.                                     System.out.println("Error: Not enought money");
  153.                                 }
  154.                             }
  155.                         });
  156.                        
  157.                         // Boost x2 Label
  158.                         JLabel labelx2 = new JLabel("x2 Cookie");
  159.                         labelx2.setBounds(5, 55, 95, 30);
  160.                        
  161.                      // Boost x5 Button
  162.                         JButton btnx5 = new JButton("1000");
  163.                         btnx5.setBounds(100, 90, 95, 30);
  164.                         btnx5.addActionListener(new ActionListener() {
  165.                             // Button x2 Multipliers
  166.                             @Override
  167.                             public void actionPerformed(ActionEvent e) {
  168.                                 if(!(cookie < 1000)){
  169.                                 cookie = new cookieManager().cookieSub(cookie, 1000);
  170.                                 multipliers = 5;
  171.                                 }else{
  172.                                     final ImageIcon icon = new ImageIcon("/Users/sbtherrien/Desktop/cookie2.png");
  173.                                     JOptionPane.showMessageDialog(frame, "You need more money to buy this upgrade!", "Not enought cookie", 1, icon);
  174.                                     System.out.println("Error: Not enought money");
  175.                                 }
  176.                             }
  177.                         });
  178.                        
  179.                         // Boost x5 Label
  180.                         JLabel labelx5 = new JLabel("x5 Cookie");
  181.                         labelx5.setBounds(5, 90, 95, 30);
  182.  
  183.                         // Bonus bg button
  184.                         JButton bonusbg = new JButton("100");
  185.                         bonusbg.setBounds(100, 125, 95, 30);
  186.                         bonusbg.addActionListener(new ActionListener() {
  187.                             // Button x2 Multipliers
  188.                             @Override
  189.                             public void actionPerformed(ActionEvent e) {
  190.                                 if(!(cookie < 10)){
  191.                                     cookie = new cookieManager().cookieSub(cookie, 10);
  192.                                     lblCookie.setForeground((Color)new cookieManager().randomColor());
  193.                                 }else{
  194.                                     final ImageIcon icon = new ImageIcon("/Users/sbtherrien/Desktop/cookie2.png");
  195.                                     JOptionPane.showMessageDialog(frame, "You need more money to buy this upgrade!", "Not enought cookie", 1, icon);
  196.                                     System.out.println("Error: Not enought money");
  197.                                 }
  198.                             }
  199.                         });
  200.                        
  201.                         // Boost x2 Label
  202.                         JLabel labelbg = new JLabel("Bonus Background");
  203.                         labelbg.setBounds(5, 125, 95, 30);
  204.  
  205.                         frame2.getContentPane().add(bonusbg);
  206.                         frame2.getContentPane().add(labelbg);
  207.                         frame2.getContentPane().add(btnx5);
  208.                         frame2.getContentPane().add(labelx5);
  209.                         frame2.getContentPane().add(labelx2);
  210.                         frame2.getContentPane().add(panel);
  211.                         panel.add(label);
  212.                         frame2.getContentPane().add(btnx2);
  213.                         frame2.getContentPane().add(lab);
  214.                         frame2.getContentPane().add(btn1);
  215.                       }
  216.                 });
  217.                
  218.         // Cookie Button
  219.                 JButton btnClick = new JButton("Cookie");
  220.                 btnClick.setIcon(new ImageIcon("/Users/sbtherrien/Desktop/cookie.png"));
  221.                 btnClick.setFont(new Font("Lucida Grande", Font.BOLD, 35));
  222.                 btnClick.setForeground(new Color(128, 0, 0));
  223.                 btnClick.setBounds(201, 128, 198, 178);
  224.                 btnClick.addActionListener(new ActionListener() {
  225.                     @Override
  226.                     public void actionPerformed(ActionEvent e) {
  227.                         cookie = cookie + multipliers; 
  228.                         lblCookie.setText("Cookies: " + cookie);
  229.                         new SaveData().Save(cookie + "");
  230.                     }
  231.                 });
  232.                 frame.getContentPane().add(btnClick);
  233.                 JButton btnopen = new JButton("Open game");
  234.                 btnopen.setBounds(10, 410, 80, 30);
  235.                 btnopen.addActionListener(new ActionListener() {
  236.                    
  237.                     @Override
  238.                     public void actionPerformed(ActionEvent e) {
  239.                         try {
  240.                             open = new openFile().readFile();
  241.                         } catch (IOException e1) {
  242.                             // TODO Auto-generated catch block
  243.                             e1.printStackTrace();
  244.                         }
  245.                         lblCookie.setText("Cookies: " + cookie);
  246.                        
  247.                     }
  248.                 });
  249.                 frame.getContentPane().add(btnopen);
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement