Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.85 KB | None | 0 0
  1. package income;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Canvas;
  5. import java.awt.Color;
  6. import java.awt.Dimension;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.io.PrintStream;
  10. import java.util.Random;
  11.  
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JOptionPane;
  16. import javax.swing.JPanel;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.JTextArea;
  19. import javax.swing.ScrollPaneConstants;
  20. import javax.swing.Timer;
  21.  
  22.  
  23.     public class Engine extends Canvas implements Runnable  {
  24.        
  25.         /**
  26.          *
  27.          */
  28.        
  29.         private static final long serialVersionUID = 1L;
  30.         public boolean running = false;
  31.         public static int money; //global amount of money.
  32.         public static int printers;//global amount of printers.
  33.         public static int time;//global game time//MUST BE GLOBAL
  34.         public float tax;
  35.         public float printertax;
  36.         public int printerspeed = 1000;
  37.        
  38.         public Engine(){
  39.         JFrame frame = new JFrame();
  40.        
  41.         frame.setVisible(true); //To prevent frame disappearing bug.
  42.        
  43.         JButton button = new JButton("Click me for money!");
  44.         JButton consolebutton = new JButton("Console");
  45.         JPanel panel = new JPanel();
  46.         JLabel cash = new JLabel();
  47.         JLabel machines = new JLabel();
  48.         JLabel gameclock = new JLabel();
  49.         JLabel speedratio = new JLabel();
  50.         JButton machine = new JButton("Tier 1 Printer");
  51.         JButton machine2 = new JButton("Tier 2 Printer");
  52.         JButton machine3 = new JButton("Tier 3 Printer");
  53.         JButton upgrade = new JButton("Upgrade Printers");
  54.        
  55.         button.setVisible(true);
  56.         frame.setResizable(false);
  57.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58.         frame.setTitle("Clicker");
  59.         frame.setSize(400, 400);
  60.         frame.setLayout(new BorderLayout());
  61.         frame.add(this, BorderLayout.CENTER);
  62.         frame.add(panel);
  63.        
  64.         panel.add(machines);
  65.         panel.add(speedratio);
  66.         panel.add(cash);       
  67.         panel.add(button);
  68.         panel.add(gameclock);
  69.         panel.add(consolebutton);
  70.        
  71.         Thread Console = new Thread(new Runnable() {
  72.             public void run() {
  73.                 System.out.println("Console Initialized!");
  74.                 System.out.println("Redirecting Stream to Console..");
  75.                
  76.                 JFrame frame = new JFrame();
  77.                 frame.setVisible(true); //To prevent frame disappearing bug.
  78.                
  79.                 JTextArea textArea = new JTextArea(50, 10);
  80.                 JScrollPane scrollableTextArea = new JScrollPane(textArea);
  81.                 PrintStream printStream = new PrintStream(new CustomOutputStream(textArea));//printStream redirect from CustomOutputStream Class
  82.                 System.setOut(printStream);
  83.                 System.setErr(printStream);
  84.  
  85.                 textArea.setLineWrap(true);//horizontal text scrolling
  86.                 scrollableTextArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);//vertical scrollbar
  87.                 scrollableTextArea.setPreferredSize(new Dimension(380,360));//Window Size
  88.                
  89.                 JPanel panel = new JPanel();
  90.                
  91.                 frame.setResizable(false);
  92.                 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  93.                 frame.setTitle("Clicker");
  94.                 frame.setSize(400, 400);
  95.                 frame.setLayout(new BorderLayout());
  96.                 frame.add(panel);
  97.                
  98.                 panel.add(scrollableTextArea);
  99.             }
  100.         });
  101.        
  102.         Thread consolebuttonthread = new Thread(new Runnable()  {
  103.             public void run() {
  104.                 System.out.println("Cash Button Thread Started!");
  105.                 consolebutton.addActionListener(new ActionListener(){
  106.                     public void actionPerformed(ActionEvent e){
  107.                         Console.start();
  108.                         panel.remove(consolebutton);
  109.                     }
  110.                 });
  111.             }  
  112.             }
  113.         );consolebuttonthread.start();
  114.        
  115.         Thread gameclockthread = new Thread(new Runnable() {
  116.             public void run() {
  117.                 System.out.println("Game Clock Thread Started!");
  118.                 Timer gameclocktimer = new Timer(60000, new ActionListener() {
  119.                     public void actionPerformed(ActionEvent e) {
  120.                         time += 1;
  121.                     }
  122.                 });
  123.                 gameclocktimer.start();
  124.             }
  125.         });
  126.         gameclockthread.start();
  127.        
  128.         Thread syncthread = new Thread(new Runnable() {//adds a thread that synchronizes the JFrame with the Engine.
  129.             public void run() {
  130.                 System.out.println("System Synchronization Thread Started!");
  131.                 Timer SyncTimer = new Timer(250, new ActionListener(){//Updates GUI with new info every quarter of a second.
  132.                     public void actionPerformed(ActionEvent e) {
  133.                         cash.setText("You have " + money + "$");
  134.                         machines.setText("Printers Active: " + printers);
  135.                         gameclock.setText("You have played for: " + time + "m");
  136.                         speedratio.setText("Printer Delay: " + printerspeed + "ms");
  137.                         frame.validate();
  138.                         frame.repaint();
  139.                     }
  140.                     });
  141.                 SyncTimer.start();//starts the called thread.
  142.                 }  
  143.             }
  144.            
  145.         );
  146.         syncthread.start();//starts the called thread.
  147.        
  148.        
  149.         Thread cashmachine = new Thread(new Runnable() {//adds a new thread from the EDT which adds a button to the frame.
  150.             public void run() {
  151.                     System.out.println("Tier 1 Printer Thread Started");//Reports that the thread started.
  152.                         panel.add(machine);
  153.                         machine.addActionListener(new ActionListener(){
  154.                             public void actionPerformed(ActionEvent e){
  155.                                 if (money >= 100 ) {//Button click checks if the player has money.
  156.                                     Thread printert1 = new Thread(new Runnable() {//adds a new printer thread.
  157.                                         public void run() {
  158.                                             money -= 100;//Subtracts 100$ for the money printer.
  159.                                             printers += 1;//adds 1 to the printer integer which counts the active printers.
  160.                                             System.out.println("Printer Bought!");
  161.                                             while(running) {//income loop that adds 1$ per second.
  162.                                                 delay(printerspeed);
  163.                                                 money += 1;
  164.                                             }
  165.                                         }
  166.                                        
  167.                                     });
  168.                                     printert1.start();//starts the called thread.
  169.                 }
  170.                 }
  171.             });
  172.         ;}});
  173.         cashmachine.start();//starts the called thread.
  174.        
  175.         Thread cashmachine2 = new Thread(new Runnable() {//adds a new thread from the EDT which adds a button to the frame.
  176.             public void run() {
  177.                     System.out.println("Tier 2 Printer Thread Started");//Reports that the thread started.
  178.                         panel.add(machine2);
  179.                         machine2.addActionListener(new ActionListener(){
  180.                             public void actionPerformed(ActionEvent e){
  181.                                 if (money >= 1000 ) {//Button click checks if the player has money.
  182.                                     Thread printert2 = new Thread(new Runnable() {//adds a new printer thread.
  183.                                         public void run() {
  184.                                             money -= 1000;//Subtracts 1000$ for the money printer.
  185.                                             printers += 1;//adds 1 to the printer integer which counts the active printers.
  186.                                             System.out.println("Printer Bought!");
  187.                                             while(running) {//income loop that adds 10$ per second.
  188.                                                 delay(printerspeed);
  189.                                                 money += 10;
  190.                                             }
  191.                                         }
  192.                                        
  193.                                     });
  194.                                     printert2.start();//starts the called thread.
  195.                 }
  196.                 }
  197.             });
  198.         ;}});
  199.         cashmachine2.start();//starts the called thread.
  200.        
  201.         Thread cashmachine3 = new Thread(new Runnable() {//adds a new thread from the EDT which adds a button to the frame.
  202.             public void run() {
  203.                     System.out.println("Tier 3 Printer Thread Started");//Reports that the thread started.
  204.                         panel.add(machine3);
  205.                         machine3.addActionListener(new ActionListener(){
  206.                             public void actionPerformed(ActionEvent e){
  207.                                 if (money >= 10000 ) {//Button click checks if the player has money.
  208.                                     Thread printert3 = new Thread(new Runnable() {//adds a new printer thread.
  209.                                         public void run() {
  210.                                             money -= 10000;//Subtracts 1000$ for the money printer.
  211.                                             printers += 1;//adds 1 to the printer integer which counts the active printers.
  212.                                             System.out.println("Printer Bought!");
  213.                                             while(running) {//income loop that adds 10$ per second.
  214.                                                 delay(printerspeed);
  215.                                                 money += 100;
  216.                                             }
  217.                                         }
  218.                                        
  219.                                     });
  220.                                     printert3.start();//starts the called thread.
  221.                 }
  222.                 }
  223.             });
  224.         ;}});
  225.         cashmachine3.start();//starts the called thread.
  226.        
  227.         Thread cashoverclock = new Thread(new Runnable() {//adds a new thread from the EDT which adds a button to the frame.
  228.             public void run() {
  229.                     System.out.println("Printer Upgrade Thread Started!");//Reports that the thread started.
  230.                         panel.add(upgrade);
  231.                         upgrade.addActionListener(new ActionListener(){
  232.                             public void actionPerformed(ActionEvent e){
  233.                                 if (money >= 5000 ) {//Button click checks if the player has money.
  234.                                     Thread printerupgrade = new Thread(new Runnable() {//adds a new printer upgrade thread.
  235.                                         public void run() {
  236.                                             money -= 5000;//Subtracts 1000$ for the money printer upgrade
  237.                                             printerspeed -= 1;//subtracts from thread delay.
  238.                                             System.out.println("Printer Upgrade Bought!");
  239.                                             }
  240.                                         }
  241.                                     );
  242.                                     printerupgrade.start();//starts the called thread.
  243.                 }
  244.                 }
  245.             });
  246.         ;}});
  247.         cashoverclock.start();//starts the called thread.
  248.        
  249.         Thread taxes = new Thread(new Runnable() {
  250.             public void run() {
  251.                 System.out.println("Tax Thread Started!");
  252.                 Timer TaxTimer = new Timer(16000, new ActionListener() {
  253.                     public void actionPerformed(ActionEvent e) {
  254.                         tax = money / 100 * 20;
  255.                         money -= tax;
  256.                         System.out.println("Taxes paid!");
  257.                         System.out.println(tax + "$");
  258.                         tax -= tax;
  259.                     }
  260.                     });
  261.                 TaxTimer.start();
  262.             }
  263.         });
  264.         taxes.start();
  265.        
  266.         Thread printertaxes = new Thread(new Runnable() {
  267.             public void run() {
  268.                 System.out.println("Printer Tax Thread #2 Started!");
  269.                 Timer PrinterTaxTimer = new Timer(28000, new ActionListener() {
  270.                     public void actionPerformed(ActionEvent e) {
  271.                     printertax = printers * 20;
  272.                     money -= printertax;
  273.                     System.out.println("Printer Taxes paid!");
  274.                     System.out.println(printertax + "$");
  275.                     printertax -= printertax;
  276.                     }
  277.                     });
  278.                 PrinterTaxTimer.start();
  279.             }
  280.         });
  281.         printertaxes.start();
  282.        
  283.         Thread cashbutton = new Thread(new Runnable()  {
  284.             public void run() {
  285.                 System.out.println("Cash Button Thread Started!");
  286.                 button.addActionListener(new ActionListener(){
  287.                     public void actionPerformed(ActionEvent e){
  288.                         money += 1;
  289.                     }
  290.                 });
  291.             }  
  292.             }
  293.         );cashbutton.start();
  294.         }
  295.            
  296.    
  297.     public static void delay(int millis) {
  298.         try {
  299.             Thread.sleep(millis);
  300.         } catch (InterruptedException exp) {
  301.            
  302.         }
  303.     }
  304.     public synchronized void start() {
  305.         running = true;
  306.         new Thread(this).start();
  307.     }
  308.     public synchronized void stop() {
  309.         running = false;
  310.     }
  311.  
  312.     public static void main(String[] args) {
  313.     new Engine().start();
  314.     }
  315.  
  316.     public void run() {
  317.         System.out.println("EDT and MT Started! ");
  318.     }
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement