In: Computer Science
Could we turn this java code into a GUI game? Add a orange and a green trophy object.
Orange trophy costs 15 clicks and every time we buy a orange trophy price increases by * 1.09.
Green trophy costs 100 clicks and every time we buy a green trophy price increases by * 1.09.
Example: you would click the button 15 times in order to be allowed to buy the orange trophy then it resets but then the cost becomes 16.35 clicks so its rounded up to 17 clicks to buy again. Same for green but slightly different math.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class ImageClickCount {
public static void main(String[] args)throws Exception {
JLabel count_text= new JLabel("Total Click Count : "); //
creating count_text instance of JLabel
JPanel group; // panel that uses CardLayout
JFrame frame = new JFrame("Image Click Count"); // creating frame
instance of JFrame
BufferedImage img = ImageIO.read(new File("click-here.jpg"));//
reading image file
JPanel image_panel = new JPanel(); // creating image_panel instance
of JPanel
JLabel picture = new JLabel(new ImageIcon(img)); // creating
picture instance of JLabel
image_panel.add(picture); // adding picture to image_panel
// now adding mouse listener to picture
picture.addMouseListener(new MouseAdapter()
{
int count=0; // initializing count variable
public void mouseClicked(MouseEvent e)
{
count++; // incrementing count variable
count_text.setText("Total Click Count : "+count); // setting click
count
}
});
group = new JPanel(new CardLayout()); // creating group instance of
JPanel
group.add(image_panel); // adding image_panle to group panel
JPanel label_panel = new JPanel(); // creating label_panel instance
of JPanel
label_panel.add(count_text); // adding count_text to
label_panel
Container pane = frame.getContentPane(); // creating pane instance
of Container
// adding group panel to apne with BorderLayout.CENTER
pane.add(group, BorderLayout.CENTER);
// adding label_panel to pane with
BorderLayout.PAGE_END
pane.add(label_panel, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // setting
default close operation
frame.setSize(350, 250); // setting width and height for
frame
frame.setVisible(true); // setting frame visibility true
}
}
Solution for the given problem
package test;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ImageClickCount {
static int orangeTrophyPrice = 15;
static int greenTrophyPrice = 100;
public static void main(String[] args)throws Exception {
JLabel count_text= new JLabel("Total Click Count : "); //
creating count_text instance of JLabel
JPanel group; // panel that uses CardLayout
JFrame frame = new JFrame("Image Click Count"); // creating frame
instance of JFrame
BufferedImage img = ImageIO.read(new File("click-here.jpg"));//
reading image file
JPanel image_panel = new JPanel(); // creating image_panel instance
of JPanel
JLabel picture = new JLabel(new ImageIcon(img)); // creating
picture instance of JLabel
image_panel.add(picture); // adding picture to image_panel
// now adding mouse listener to picture
picture.addMouseListener(new MouseAdapter()
{
int count=0; // initializing count variable
public void mouseClicked(MouseEvent e)
{
count++; // incrementing count variable
count_text.setText("Total Click Count : "+count); // setting click
count
if(count == greenTrophyPrice) {
System.out.println("GREEN TROPHY BOUGHT!");
greenTrophyPrice = (int) Math.ceil(greenTrophyPrice * 1.09);
System.out.println("NEW PRICE FOR GREEN : " +
greenTrophyPrice);
}
if(count == orangeTrophyPrice) {
System.out.println("ORANGE TROPHY BOUGHT!");
orangeTrophyPrice = (int) Math.ceil(orangeTrophyPrice *
1.09);
System.out.println("NEW PRICE FOR ORANGE : " +
orangeTrophyPrice);
}
}
});
group = new JPanel(new CardLayout()); // creating group instance
of JPanel
group.add(image_panel); // adding image_panle to group panel
JPanel label_panel = new JPanel(); // creating label_panel instance
of JPanel
label_panel.add(count_text); // adding count_text to
label_panel
Container pane = frame.getContentPane(); // creating pane instance
of Container
// adding group panel to pane with BorderLayout.CENTER
pane.add(group, BorderLayout.CENTER);
// adding label_panel to pane with BorderLayout.PAGE_END
pane.add(label_panel, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // setting
default close operation
frame.setSize(350, 250); // setting width and height for
frame
frame.setVisible(true); // setting frame visibility true
}
}
*********Thank You***********