Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

What is a Java snake cookie clicker?

Asked by Joseph Slater Oct 11, 2022 1.8K views 4 answers
Share

About this question

This is my first graphical application I have made in my life (except HTML and Javascript-Applications, if that counts). It's a simple implementation of Cookie Clicker, the famous browser game. Unfortunately the size of the buttons equals the size of columns. That does not look good. I have seen a solution that uses two layouts. A GridLayout, and an additional FlotLayout. But the code looks so ugly, that I don't want to use this way of programming.


I would be very thankful if you have tips for me on how to improve the code quality! Programming a graphical application is much harder and more complex than writing textual applications.


import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CookieClicker extends JFrame {
    // non graphical variables
    private int cookies = 0;
    private int clicker = 1;
    private int clickerPrice = 20;
    // graphical variables
    int numberOfColumns = 5;
    Container container;
    JLabel cookieLabel;
    JButton increaseCookiesButton;
    JLabel clickerLabel;
    JButton increaseClickerButton;
    // buildings
    Building bakery;
    boolean bakeryUnlocked;
    Building robot;
    boolean robotUnlocked;
    Building factory;
    boolean factoryUnlocked;
    public CookieClicker() {
        container = getContentPane();
        container.setLayout(new GridLayout(5, 1));
        bakery = new Building("Bakery", 0, 1, 20);
        bakeryUnlocked = false;
        robot = new Building("Robot", 0, 5, 100);
        robotUnlocked = false;
        factory = new Building("Factory", 0, 10, 200);
        factoryUnlocked = false;
        // produce cookies by hand
        cookieLabel = new JLabel("Cookies: " + cookies);
        increaseCookiesButton = new JButton("Increase Cookies");
        increaseCookiesButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cookies += clicker;
            }
        });
        // improve clicking production rate
        clickerLabel = new JLabel("Clicker Level: " + clicker);
        increaseClickerButton = new JButton("Improve Clicker (Costs: " + clickerPrice + ")");
        increaseClickerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                increaseClicker();
            }
            private void increaseClicker() {
                if(cookies >= clickerPrice) {
                    clicker++;
                    cookies -= clickerPrice;
                    clickerPrice *= 2;
                    JOptionPane.showMessageDialog(null, "You have improved your clicker!");
                } else {
                    JOptionPane.showMessageDialog(null, "You have not enough money!");
                }
            }
        });
        java.util.Timer actualizeProgress = new java.util.Timer();
        actualizeProgress.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                cookieLabel.setText("Cookies: " + cookies);
                clickerLabel.setText("Clicker Level: " + clicker);
                increaseClickerButton.setText("Improve Clicker (Costs: " + clickerPrice + ")");
            }
        }, 0, 25);
        java.util.Timer getMoreBuildings = new java.util.Timer(); 
        getMoreBuildings.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                if (bakeryUnlocked == false && clicker >= 2) {
                    bakery.unlock();
                    bakeryUnlocked = true;
                }
                if (robotUnlocked == false && bakery.getLevel() >= 2) {
                    robot.unlock();
                    robotUnlocked = true;
                }         
                if (factoryUnlocked == false && robot.getLevel() >= 2) {
                    factory.unlock();
                    factoryUnlocked = true;
                }
            }
        }, 0, 2000);
        java.util.Timer produceWithBuildings = new java.util.Timer();
        produceWithBuildings.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                cookies += bakery.getProductionRate() + robot.getProductionRate() + factory.getProductionRate();
            }
        }, 0, 1000);
        container.add(cookieLabel);
        container.add(increaseCookiesButton);
        container.add(new JLabel("")); // blank label
        container.add(clickerLabel);
        container.add(increaseClickerButton);
    }
    public class Building {
        // non graphical variables
        private String name;
        private int level;
        private int productionRate;
        private int costs;
        // graphical variables
        JLabel label;
        JButton button;
        public Building(String name, int level, int productionRate, int costs) {
            // non graphical variables
            this.name = name;
            this.level = level;
            this.productionRate = productionRate;
            this.costs = costs;
            // graphical variables
            label = new JLabel();
            button = new JButton();
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    improve();
                }
            });
        }
        public int getLevel() {
            return level;
        }
        public void unlock() {
            numberOfColumns += 3;
            container.setLayout(new GridLayout(numberOfColumns, 1));
            container.add(new JLabel(""));
            container.add(label);
            container.add(button);
            setSize(210, getHeight() + 120);
            actualize();
        }
        public void improve() {
            if(cookies >= costs) {
                level++;
                cookies -= costs;
                costs *= 2;
                JOptionPane.showMessageDialog(null, "You have improved the " + name + "!");
            } else {
                JOptionPane.showMessageDialog(null, "You have not enough money!");
            }
            actualize();
        }
        public int getProductionRate() {
            return productionRate * level;
        }
        public void actualize() {
            label.setText(name + " Prod. Rate: " + getProductionRate());
            button.setText("Improve (costs: " + costs + ")");
        }
    }
    public static void main(String[] args) {
        CookieClicker cookieClicker = new CookieClicker();
        cookieClicker.setTitle("Cookie Clicker");
        cookieClicker.setSize(210, 200);
        cookieClicker.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        cookieClicker.setVisible(true);
    }
}


Your answer

4 Answers

Dave Ginger Latest answer

Answered on Jul 27, 2026

here are plenty of casino review sites, but https://lonestar-casino.us.com/ stood out because everything is explained in a straightforward way. I quickly found the details I needed about the platform and its offers.

Was this helpful?

Eurydice

Answered on Mar 26, 2026

You should only gamble at fast payout casinos if you value your time and sanity! Waiting days or even weeks to receive your winnings is a frustrating experience nobody wants. Fast payout casinos prioritize efficiency, ensuring that players get their money quickly—sometimes in just hours, thanks to modern e-wallets and cryptocurrency options. This swift service reflects the casino's commitment to high-quality customer satisfaction and transparency. Fast Payout Casinos https://ictir2016.org/ understand that when you win, immediate access to your funds is crucial; so why settle for anything less? Slow-paying casinos are relics of the past, often burdened with outdated technology or poor customer service practices. Fast payout indicates not just speedy transactions but also typically mirrors superior overall operation, better customer support, and fewer headaches for patrons. Don’t settle for sluggish red-tape payouts—demand only the best gaming experiences where satisfaction reigns supreme through quick financial processes!

Was this helpful?

Amandalawrancee

Answered on Feb 16, 2026

i like the focus on responsible tools, since deposit limits and reality checks are the only things that keep my sessions sensible. ai tips sound interesting, but i’d be wary of people treating them as a shortcut to “beating” the casino. i’ve played a couple rounds via https://xxxtremeroulette.com/parimatch-casino/ and ended up using time limits straight after because the xxxtreme lightning multipliers hit so fast it’s easy to lose track. similar deal when i tried funky time through https://funkytimegame.org/mostbet-casino/ ; the bonus rounds and energy make the reality check pop-ups feel necessary. then jumped to crazy time on https://crazytimegame.org/melbet-casino/ and same thing — those big wheel spins fly by without the timer i’d be gone for hours. overall, the tools matter more than the game math if you want to stay in control.

Was this helpful?

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.