Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package income;
- import java.awt.BorderLayout;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.PrintStream;
- import java.util.Random;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.ScrollPaneConstants;
- import javax.swing.Timer;
- public class Engine extends Canvas implements Runnable {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- public boolean running = false;
- public static int money; //global amount of money.
- public static int printers;//global amount of printers.
- public static int time;//global game time//MUST BE GLOBAL
- public float tax;
- public float printertax;
- public int printerspeed = 1000;
- public Engine(){
- JFrame frame = new JFrame();
- frame.setVisible(true); //To prevent frame disappearing bug.
- JButton button = new JButton("Click me for money!");
- JButton consolebutton = new JButton("Console");
- JPanel panel = new JPanel();
- JLabel cash = new JLabel();
- JLabel machines = new JLabel();
- JLabel gameclock = new JLabel();
- JLabel speedratio = new JLabel();
- JButton machine = new JButton("Tier 1 Printer");
- JButton machine2 = new JButton("Tier 2 Printer");
- JButton machine3 = new JButton("Tier 3 Printer");
- JButton upgrade = new JButton("Upgrade Printers");
- button.setVisible(true);
- frame.setResizable(false);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setTitle("Clicker");
- frame.setSize(400, 400);
- frame.setLayout(new BorderLayout());
- frame.add(this, BorderLayout.CENTER);
- frame.add(panel);
- panel.add(machines);
- panel.add(speedratio);
- panel.add(cash);
- panel.add(button);
- panel.add(gameclock);
- panel.add(consolebutton);
- Thread Console = new Thread(new Runnable() {
- public void run() {
- System.out.println("Console Initialized!");
- System.out.println("Redirecting Stream to Console..");
- JFrame frame = new JFrame();
- frame.setVisible(true); //To prevent frame disappearing bug.
- JTextArea textArea = new JTextArea(50, 10);
- JScrollPane scrollableTextArea = new JScrollPane(textArea);
- PrintStream printStream = new PrintStream(new CustomOutputStream(textArea));//printStream redirect from CustomOutputStream Class
- System.setOut(printStream);
- System.setErr(printStream);
- textArea.setLineWrap(true);//horizontal text scrolling
- scrollableTextArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);//vertical scrollbar
- scrollableTextArea.setPreferredSize(new Dimension(380,360));//Window Size
- JPanel panel = new JPanel();
- frame.setResizable(false);
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.setTitle("Clicker");
- frame.setSize(400, 400);
- frame.setLayout(new BorderLayout());
- frame.add(panel);
- panel.add(scrollableTextArea);
- }
- });
- Thread consolebuttonthread = new Thread(new Runnable() {
- public void run() {
- System.out.println("Cash Button Thread Started!");
- consolebutton.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- Console.start();
- panel.remove(consolebutton);
- }
- });
- }
- }
- );consolebuttonthread.start();
- Thread gameclockthread = new Thread(new Runnable() {
- public void run() {
- System.out.println("Game Clock Thread Started!");
- Timer gameclocktimer = new Timer(60000, new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- time += 1;
- }
- });
- gameclocktimer.start();
- }
- });
- gameclockthread.start();
- Thread syncthread = new Thread(new Runnable() {//adds a thread that synchronizes the JFrame with the Engine.
- public void run() {
- System.out.println("System Synchronization Thread Started!");
- Timer SyncTimer = new Timer(250, new ActionListener(){//Updates GUI with new info every quarter of a second.
- public void actionPerformed(ActionEvent e) {
- cash.setText("You have " + money + "$");
- machines.setText("Printers Active: " + printers);
- gameclock.setText("You have played for: " + time + "m");
- speedratio.setText("Printer Delay: " + printerspeed + "ms");
- frame.validate();
- frame.repaint();
- }
- });
- SyncTimer.start();//starts the called thread.
- }
- }
- );
- syncthread.start();//starts the called thread.
- Thread cashmachine = new Thread(new Runnable() {//adds a new thread from the EDT which adds a button to the frame.
- public void run() {
- System.out.println("Tier 1 Printer Thread Started");//Reports that the thread started.
- panel.add(machine);
- machine.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if (money >= 100 ) {//Button click checks if the player has money.
- Thread printert1 = new Thread(new Runnable() {//adds a new printer thread.
- public void run() {
- money -= 100;//Subtracts 100$ for the money printer.
- printers += 1;//adds 1 to the printer integer which counts the active printers.
- System.out.println("Printer Bought!");
- while(running) {//income loop that adds 1$ per second.
- delay(printerspeed);
- money += 1;
- }
- }
- });
- printert1.start();//starts the called thread.
- }
- }
- });
- ;}});
- cashmachine.start();//starts the called thread.
- Thread cashmachine2 = new Thread(new Runnable() {//adds a new thread from the EDT which adds a button to the frame.
- public void run() {
- System.out.println("Tier 2 Printer Thread Started");//Reports that the thread started.
- panel.add(machine2);
- machine2.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if (money >= 1000 ) {//Button click checks if the player has money.
- Thread printert2 = new Thread(new Runnable() {//adds a new printer thread.
- public void run() {
- money -= 1000;//Subtracts 1000$ for the money printer.
- printers += 1;//adds 1 to the printer integer which counts the active printers.
- System.out.println("Printer Bought!");
- while(running) {//income loop that adds 10$ per second.
- delay(printerspeed);
- money += 10;
- }
- }
- });
- printert2.start();//starts the called thread.
- }
- }
- });
- ;}});
- cashmachine2.start();//starts the called thread.
- Thread cashmachine3 = new Thread(new Runnable() {//adds a new thread from the EDT which adds a button to the frame.
- public void run() {
- System.out.println("Tier 3 Printer Thread Started");//Reports that the thread started.
- panel.add(machine3);
- machine3.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if (money >= 10000 ) {//Button click checks if the player has money.
- Thread printert3 = new Thread(new Runnable() {//adds a new printer thread.
- public void run() {
- money -= 10000;//Subtracts 1000$ for the money printer.
- printers += 1;//adds 1 to the printer integer which counts the active printers.
- System.out.println("Printer Bought!");
- while(running) {//income loop that adds 10$ per second.
- delay(printerspeed);
- money += 100;
- }
- }
- });
- printert3.start();//starts the called thread.
- }
- }
- });
- ;}});
- cashmachine3.start();//starts the called thread.
- Thread cashoverclock = new Thread(new Runnable() {//adds a new thread from the EDT which adds a button to the frame.
- public void run() {
- System.out.println("Printer Upgrade Thread Started!");//Reports that the thread started.
- panel.add(upgrade);
- upgrade.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- if (money >= 5000 ) {//Button click checks if the player has money.
- Thread printerupgrade = new Thread(new Runnable() {//adds a new printer upgrade thread.
- public void run() {
- money -= 5000;//Subtracts 1000$ for the money printer upgrade
- printerspeed -= 1;//subtracts from thread delay.
- System.out.println("Printer Upgrade Bought!");
- }
- }
- );
- printerupgrade.start();//starts the called thread.
- }
- }
- });
- ;}});
- cashoverclock.start();//starts the called thread.
- Thread taxes = new Thread(new Runnable() {
- public void run() {
- System.out.println("Tax Thread Started!");
- Timer TaxTimer = new Timer(16000, new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- tax = money / 100 * 20;
- money -= tax;
- System.out.println("Taxes paid!");
- System.out.println(tax + "$");
- tax -= tax;
- }
- });
- TaxTimer.start();
- }
- });
- taxes.start();
- Thread printertaxes = new Thread(new Runnable() {
- public void run() {
- System.out.println("Printer Tax Thread #2 Started!");
- Timer PrinterTaxTimer = new Timer(28000, new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- printertax = printers * 20;
- money -= printertax;
- System.out.println("Printer Taxes paid!");
- System.out.println(printertax + "$");
- printertax -= printertax;
- }
- });
- PrinterTaxTimer.start();
- }
- });
- printertaxes.start();
- Thread cashbutton = new Thread(new Runnable() {
- public void run() {
- System.out.println("Cash Button Thread Started!");
- button.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- money += 1;
- }
- });
- }
- }
- );cashbutton.start();
- }
- public static void delay(int millis) {
- try {
- Thread.sleep(millis);
- } catch (InterruptedException exp) {
- }
- }
- public synchronized void start() {
- running = true;
- new Thread(this).start();
- }
- public synchronized void stop() {
- running = false;
- }
- public static void main(String[] args) {
- new Engine().start();
- }
- public void run() {
- System.out.println("EDT and MT Started! ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement