Lab 4: Cookie Clicker (requirements) Goal (500 pts.): To create a \"Cookie Click
ID: 3592242 • Letter: L
Question
Lab 4: Cookie Clicker (requirements) Goal (500 pts.): To create a "Cookie Clicker" game variant. Top Level Requirement: All of the buttons provided to you from the GUI template must "work" similar to how http://orteil.dashnet.org/cookieclicker/buttons perform. This will give you experience to how you might add code to existing code by filling stub methods and filling out actions on GUI items. You will get to use the Java API more as well as practice with applying logic and problem solving skills Bonus Points (up to 250 pts.): Add extra features to your clicker game. This can be features that exist in the game but not required or totally new features. Document these additions in a separate document. Hints: Pair up with someone else. If you're stuck, you most likely won't get unstuck by yourself. You will need to add these: Instance Fields (In the correct location within your code). .Getters and Setters for said fields .Good comments!Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.bevel_border.BevelBorder;
import java.util.Date;
import java.util.Hashtable;
public class CookieClicker extends JFrame {
double curr_cookies = 0;
double cookies_second = 0;
CookiePtoducerPanel producer_panel;
public CookieClicker() {
setSize(800, 600);
setTitle("Cookie Clicker Game");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
Container cont = getContentPane();
cont.add(new CookiePanel(this));
cont.add(producer_panel = new CookiePtoducerPanel(this));
cont.add(new CookieShopPanel(this));
setVisible(true);
}
public double getSecondPerCookies() {
return cookies_second;
}
public void addCookies(double cookies) {
cookies_second += cookies;
}
public double getCookies() {
return curr_cookies;
}
public void reduceCookies(double amt) {
curr_cookies -= amt;
}
public void addCookieProducer(String name) {
producer_panel.add(name);
}
public class CookiePanel extends JPanel implements ActionListener {
JLabel cookiesLabel, cookies_secondLabel;
JButton cookieButton;
Date last_date = new Date();
CookieClicker cookie_clicker;
public CookiePanel(CookieClicker parent) {
cookie_clicker = parent;
setPreferredSize(new Dimension(220, 550));
BevelBorder bevel_border = new BevelBorder(BevelBorder.RAISED);
setBorder(bevel_border);
cookiesLabel = new JLabel("0 Cookies", JLabel.CENTER);
cookiesLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 23));
cookiesLabel.setPreferredSize(new Dimension(200, 50));
cookies_secondLabel = new JLabel("per seconds: 0", JLabel.CENTER);
cookies_secondLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
cookies_secondLabel.setPreferredSize(new Dimension(200, 20));
ImageIcon button_icon = new ImageIcon("./img/donut.png");
cookieButton = new JButton(button_icon);
cookieButton.setPreferredSize(new Dimension(200, 200));
cookieButton.addActionListener(this);
add(cookiesLabel);
add(cookies_secondLabel);
add(cookieButton);
int delay_time = 100;
final JPanel self = this;
ActionListener performer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
self.repaint();
}
};
new Timer(delay_time, performer).start();
}
public void paintComponent(Graphics g) {
Date current_date = new Date();
long elapsed_time = current_date.getTime() - last_date.getTime();
last_date = current_date;
curr_cookies += cookie_clicker.getSecondPerCookies() * (elapsed_time / 1000.0);
cookiesLabel.setText(String.format("%d Cookies", (long)curr_cookies));
cookies_secondLabel.setText(String.format("seconds: %.2f", cookies_second));
}
public void actionPerformed(ActionEvent e) {
curr_cookies += 1.0;
repaint();
}
}
public class CookiePtoducerPanel extends JPanel {
Hashtable<String, ItemPanel> cookieInventory = new Hashtable<String, ItemPanel>();
public CookiePtoducerPanel(CookieClicker cookie_clicker) {
setPreferredSize(new Dimension(280, 550));
BevelBorder bevel_border = new BevelBorder(BevelBorder.RAISED);
setBorder(bevel_border);
}
public class ItemPanel extends JPanel {
int itemCount = 0;
String name;
public ItemPanel(String name) {
name = name;
setPreferredSize(new Dimension(260, 50));
BevelBorder bevel_border = new BevelBorder(BevelBorder.RAISED);
setBorder(bevel_border);
}
public void increment() {
ImageIcon button_icon = new ImageIcon(String.format("./resources/%s.png", name));
JLabel _label = new JLabel(button_icon);
add(_label);
}
}
public void add(String name) {
if (!cookieInventory.containsKey(name)) {
ItemPanel _panel = new ItemPanel(name);
_panel.increment();
cookieInventory.put(name, _panel);
add(_panel);
} else {
cookieInventory.get(name).increment();
}
}
}
public class ShopCookie {
final String name;
double cookiePrice;
double totalCookies;
final JButton button;
public ShopCookie(String name, double cookiePrice, double c, final CookieClicker cookie_clicker) {
name = name;
cookiePrice = cookiePrice;
totalCookies = c;
button = new JButton(String.format("%s %d", name, (long)cookiePrice));
button.setPreferredSize(new Dimension(200, 40));
ActionListener performer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (cookiePrice <= cookie_clicker.getCookies()) {
cookie_clicker.addCookies(totalCookies);
cookie_clicker.reduceCookies((long)cookiePrice);
cookiePrice = cookiePrice * 1.3;
totalCookies = totalCookies * 1.05;
button.setText(String.format("%s %d", name, (long)cookiePrice));
cookie_clicker.addCookieProducer(name);
}
}
};
button.addActionListener(performer);
int delay_time = 100;
ActionListener timerTaskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (cookiePrice <= cookie_clicker.getCookies()) {
if (!button.isEnabled()) {
button.setEnabled(true);
button.repaint();
}
} else {
if (button.isEnabled()) {
button.setEnabled(false);
button.repaint();
}
}
}
};
new Timer(delay_time, timerTaskPerformer).start();
}
public long getPrice() {
return (long)cookiePrice;
}
public JButton getButton() {
return button;
}
}
public class CookieShopPanel extends JPanel {
final CookieClicker cookie_clicker;
public CookieShopPanel(CookieClicker _parent) {
cookie_clicker = _parent;
setPreferredSize(new Dimension(280, 550));
BevelBorder bevel_border = new BevelBorder(BevelBorder.RAISED);
setBorder(bevel_border);
JLabel label = new JLabel("Shop", JLabel.CENTER);
label.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 23));
label.setPreferredSize(new Dimension(200, 50));
add(label);
addItem("Cursor", 10, 0.1);
addItem("Grandma", 20, 1);
addItem("Farm", 100, 7);
addItem("Factory", 800, 13);
addItem("Mine", 3000, 40);
}
public void addItem(String name, double cookiePrice, double totalCookies) {
ShopCookie itemCookie = new ShopCookie(name, cookiePrice, totalCookies, cookie_clicker);
add(itemCookie.getButton());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.